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, mice, and
ggplot2 packages, some incomplete data and a
mids object into your workspace.
bwplotBox-and-whisker plot of observed and imputed data.
# ggmice equivalent
ggmice(imp, aes(x = .imp, y = hgt)) +
geom_boxplot() +
labs(x = "Imputation number")# extended reproduction with ggmice
ggmice(imp, aes(x = .imp, y = hgt)) +
stat_boxplot(geom = "errorbar", linetype = "dashed") +
geom_boxplot(outlier.colour = "grey", outlier.shape = 1) +
labs(x = "Imputation number") +
theme(legend.position = "none")densityplotDensity plot of observed and imputed data.
# extended reproduction with ggmice
ggmice(imp, aes(x = hgt, group = .imp, size = .where)) +
geom_density() +
scale_size_manual(
values = c("observed" = 1, "imputed" = 0.5),
guide = "none"
) +
theme(legend.position = "none")fluxplotInflux and outflux plot of multivariate missing data patterns.
md.patternMissing data pattern plot.
# extended reproduction with ggmice
plot_pattern(dat, square = TRUE) +
theme(
legend.position = "none",
axis.title = element_blank(),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank()
)plot.midsPlot the trace lines of the MICE algorithm.
stripplotStripplot of observed and imputed data.
# ggmice equivalent
ggmice(imp, aes(x = .imp, y = hgt)) +
geom_jitter(width = 0.25) +
labs(x = "Imputation number")# extended reproduction with ggmice (not recommended)
ggmice(imp, aes(x = .imp, y = hgt)) +
geom_jitter(
shape = 1,
width = 0.1,
na.rm = TRUE,
data = data.frame(
hgt = dat$hgt,
.imp = factor(rep(1:imp$m, each = nrow(dat))),
.where = "observed"
)
) +
geom_jitter(shape = 1, width = 0.1) +
labs(x = "Imputation number") +
theme(legend.position = "none")xyplotScatterplot of observed and imputed data.
# extended reproduction with ggmice
ggmice(imp, aes(age, hgt)) +
geom_point(size = 2, shape = 1) +
theme(legend.position = "none")To make ggmice visualizations interactive, the
plotly package can be used. For example, an interactive
influx and outflux plot may be more legible than a static one.
You may want to create a plot visualizing the imputations of multiple
variables as one object. To visualize multiple variables at once, the
variable names are saved in a vector. This vector is used together with
the functional programming package purrr and visualization
package patchwork to map() over the variables
and subsequently wrap_plots to create a single figure.
# load packages
library(purrr)
library(patchwork)
# create vector with variable names
vrb <- names(dat)Display box-and-whisker plots for all variables.