Skip to content

Commit

Permalink
Merge pull request #67 from pepijnvink/main this closes #64
Browse files Browse the repository at this point in the history
added npat to plot_pattern
  • Loading branch information
hanneoberman authored Dec 8, 2022
2 parents 8c4a2cd + 35b98f1 commit dbb7616
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ Config/testthat/edition: 3
Copyright: 'ggmice' authors
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.2
16 changes: 15 additions & 1 deletion R/pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#' @param square Logical indicating whether the plot tiles should be squares.
#' @param rotate Logical indicating whether the variable name labels should be rotated 90 degrees.
#' @param cluster Optional character string specifying which variable should be used for clustering (e.g., for multilevel data).
#' @param npat Optional integer specifying the number of missing data patterns to be visualized.
#'
#' @return An object of class [ggplot2::ggplot].
#'
#' @examples
#' plot_pattern(mice::nhanes)
#' @export
plot_pattern <- function(data, vrb = "all", square = FALSE, rotate = FALSE, cluster = NULL) {
plot_pattern <- function(data, vrb = "all", square = FALSE, rotate = FALSE, cluster = NULL, npat = NULL) {
if (!is.data.frame(data) & !is.matrix(data)) {
stop("Dataset should be a 'data.frame' or 'matrix'.")
}
Expand All @@ -28,6 +29,19 @@ plot_pattern <- function(data, vrb = "all", square = FALSE, rotate = FALSE, clus
}
# get missing data pattern and extract info
pat <- mice::md.pattern(data[, vrb], plot = FALSE)

if(!is.null(npat)){
if(!is.integer(npat) | npat < 1){
stop("The number of patterns must be a positive integer.")
}
if(npat > nrow(pat)){
warning("Number of patterns specified is more that the total number of patterns. All missing data patterns are shown.")
npat <- nrow(pat)
}
names.common.patterns <- rownames(pat[order(as.numeric(row.names(pat)), decreasing = T),][1:npat,])
pat <- pat[rownames(pat) %in% names.common.patterns,]
}

rws <- nrow(pat)
cls <- ncol(pat)
vrb <- colnames(pat)[-cls]
Expand Down
11 changes: 10 additions & 1 deletion man/plot_pattern.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plot_variance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dbb7616

Please sign in to comment.