ggmice
equivalent of mice
plotsHow 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
<- mice::boys
dat # generate imputations
<- mice::mice(dat, method = "pmm", printFlag = FALSE) imp
bwplot
Box-and-whisker plot of observed and imputed data.
# original plot
::bwplot(imp, bmi ~ .imp) mice
# ggmice equivalent
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
::geom_boxplot() +
ggplot2::labs(x = "Imputation number") ggplot2
# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
::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") ggplot2
densityplot
Density plot of observed and imputed data.
# original plot
::densityplot(imp, ~bmi) mice
# ggmice equivalent
ggmice(imp, ggplot2::aes(x = bmi, group = .imp)) +
::geom_density() ggplot2
# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(x = bmi, group = .imp, size = .where)) +
::geom_density() +
ggplot2::scale_size_manual(values = c("observed" = 1, "imputed" = 0.5),
ggplot2guide = "none") +
::theme(legend.position = "none") ggplot2
fluxplot
Influx and outflux plot of multivariate missing data patterns.
# original plot
::fluxplot(dat) mice
# ggmice equivalent
plot_flux(dat)
md.pattern
Missing data pattern plot.
# original plot
<- mice::md.pattern(dat) md
# ggmice equivalent
plot_pattern(dat)
# extended reproduction with ggmice
plot_pattern(dat, square = TRUE) +
::theme(legend.position = "none",
ggplot2axis.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
::stripplot(imp, bmi ~ .imp) mice
# ggmice equivalent
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
::geom_jitter(width = 0.25) +
ggplot2::labs(x = "Imputation number") ggplot2
# extended reproduction with ggmice (not recommended)
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
::geom_jitter(
ggplot2shape = 1,
width = 0.1,
na.rm = TRUE,
data = data.frame(
bmi = dat$bmi,
.imp = factor(rep(1:imp$m, each = nrow(dat))),
.where = "observed"
)+
) ::geom_jitter(shape = 1, width = 0.1) +
ggplot2::labs(x = "Imputation number") +
ggplot2::theme(legend.position = "none") ggplot2
xyplot
Scatterplot of observed and imputed data.
# original plot
::xyplot(imp, bmi ~ age) mice
# ggmice equivalent
ggmice(imp, ggplot2::aes(age, bmi)) +
::geom_point() ggplot2
# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(age, bmi)) +
::geom_point(size = 2, shape = 1) +
ggplot2::theme(legend.position = "none") ggplot2
# 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