Old friends

Create the ggmice equivalent of mice plots

How to re-create the output of the plotting functions from mice with ggmice. In alphabetical order of the mice functions.

First load the ggmice package, some incomplete data and a mice::mids object into your workspace.

# load packages
library(ggmice)
# load incomplete dataset 
dat <- mice::boys
# generate imputations
imp <- mice::mice(dat, method = "pmm", printFlag = FALSE)

bwplot

Box-and-whisker plot of observed and imputed data.

# original plot
mice::bwplot(imp, bmi ~ .imp)

# ggmice equivalent
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
      ggplot2::geom_boxplot() +
      ggplot2::labs(x = "Imputation number")

# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
  ggplot2::stat_boxplot(geom = 'errorbar', linetype = "dashed") +
  ggplot2::geom_boxplot(outlier.colour = "grey", outlier.shape = 1) +
  ggplot2::labs(x = "Imputation number") +
  ggplot2::theme(legend.position = "none")

densityplot

Density plot of observed and imputed data.

# original plot
mice::densityplot(imp, ~bmi)

# ggmice equivalent
ggmice(imp, ggplot2::aes(x = bmi, group = .imp)) +
      ggplot2::geom_density() 

# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(x = bmi, group = .imp, size = .where)) +
  ggplot2::geom_density() +
  ggplot2::scale_size_manual(values = c("observed" = 1, "imputed" = 0.5),
                             guide = "none") +
  ggplot2::theme(legend.position = "none")

fluxplot

Influx and outflux plot of multivariate missing data patterns.

# original plot
mice::fluxplot(dat)

# ggmice equivalent
plot_flux(dat)

md.pattern

Missing data pattern plot.

# original plot
md <- mice::md.pattern(dat)

# ggmice equivalent
plot_pattern(dat)

# extended reproduction with ggmice
plot_pattern(dat, square = TRUE) +
  ggplot2::theme(legend.position = "none",
                 axis.title = ggplot2::element_blank(),
                 axis.title.x.top = ggplot2::element_blank(),
                 axis.title.y.right = ggplot2::element_blank())

plot.mids

Plot the trace lines of the MICE algorithm.

# original plot
plot(imp, bmi ~ .it | .ms)

# ggmice equivalent
plot_trace(imp, "bmi")

stripplot

Stripplot of observed and imputed data.

# original plot
mice::stripplot(imp, bmi ~ .imp)

# ggmice equivalent
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
  ggplot2::geom_jitter(width = 0.25) +
  ggplot2::labs(x = "Imputation number")

# extended reproduction with ggmice (not recommended)
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
  ggplot2::geom_jitter(
    shape = 1,
    width = 0.1,
    na.rm = TRUE,
    data = data.frame(
      bmi = dat$bmi,
      .imp = factor(rep(1:imp$m, each = nrow(dat))),
      .where = "observed"
    )
  ) +
  ggplot2::geom_jitter(shape = 1, width = 0.1) +
  ggplot2::labs(x = "Imputation number") +
  ggplot2::theme(legend.position = "none")

xyplot

Scatterplot of observed and imputed data.

# original plot
mice::xyplot(imp, bmi ~ age)

# ggmice equivalent
ggmice(imp, ggplot2::aes(age, bmi)) +
  ggplot2::geom_point()

# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(age, bmi)) +
  ggplot2::geom_point(size = 2, shape = 1) +
  ggplot2::theme(legend.position = "none")


# this vignette was generated with R session
sessionInfo()
#> R version 4.1.2 (2021-11-01)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19042)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=C                       LC_CTYPE=Dutch_Netherlands.1252   
#> [3] LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C                      
#> [5] LC_TIME=Dutch_Netherlands.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggmice_0.0.1       ggplot2_3.3.5.9000 mice_3.14.0       
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.8.2     highr_0.9        bslib_0.3.1      compiler_4.1.2  
#>  [5] pillar_1.7.0     jquerylib_0.1.4  tools_4.1.2      digest_0.6.29   
#>  [9] gtable_0.3.0     lattice_0.20-45  jsonlite_1.8.0   evaluate_0.15   
#> [13] lifecycle_1.0.1  tibble_3.1.6     pkgconfig_2.0.3  rlang_1.0.2     
#> [17] cli_3.2.0        DBI_1.1.2        rstudioapi_0.13  yaml_2.3.5      
#> [21] xfun_0.30        fastmap_1.1.0    withr_2.5.0      dplyr_1.0.8     
#> [25] stringr_1.4.0    knitr_1.37       generics_0.1.2   vctrs_0.3.8     
#> [29] sass_0.4.0       grid_4.1.2       tidyselect_1.1.2 glue_1.6.2      
#> [33] R6_2.5.1         fansi_1.0.2      rmarkdown_2.13   farver_2.1.0    
#> [37] tidyr_1.2.0      purrr_0.3.4      magrittr_2.0.2   MASS_7.3-54     
#> [41] scales_1.1.1     backports_1.4.1  htmltools_0.5.2  ellipsis_0.3.2  
#> [45] assertthat_0.2.1 colorspace_2.0-3 labeling_0.4.2   utf8_1.2.2      
#> [49] stringi_1.7.6    munsell_0.5.0    broom_0.7.12     crayon_1.5.0