-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create separate vignette for 'old friends':
ggmice
equivalents for …
…the `mice` plotting functions
- Loading branch information
1 parent
ecd8e8b
commit b1fb93a
Showing
13 changed files
with
211 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' Plot correlations between (incomplete) variables | ||
#' | ||
#' @param dat A dataset of class `data.frame`, `tibble`, or `matrix`. | ||
#' @param vrb String or vector with variable name(s), default is "all". | ||
#' @param label Logical indicating whether correlation values should be displayed. | ||
#' @param square Logical indicating whether the plot tiles should be squares. | ||
#' @param diagonal Logical indicating whether the correlation of each variable with itself should be displayed. | ||
#' @param rotate Logical indicating whether the variable name labels should be rotated 90 degrees. | ||
#' | ||
#' @return An object of class `ggplot`. | ||
#' | ||
#' @examples | ||
#' plot_corr(mice::nhanes, label = TRUE) | ||
#' @export | ||
plot_corr <- function(dat, vrb = "all", label = FALSE, square = TRUE, diagonal = FALSE, rotate = FALSE) { | ||
if (!is.data.frame(dat) & !is.matrix(dat)) { | ||
stop("Dataset should be a 'data.frame' or 'matrix'.") | ||
} | ||
if (vrb[1] == "all") { | ||
vrb <- names(dat) | ||
} | ||
p <- length(vrb) | ||
corrs <- data.frame( | ||
vrb = rep(vrb, each = p), | ||
prd = vrb, | ||
corr = matrix(round(stats::cov2cor(stats::cov(data.matrix(dat[, vrb]), use = "pairwise.complete.obs")), 2), nrow = p * p, byrow = TRUE) | ||
) | ||
if (!diagonal) { | ||
corrs[corrs$vrb == corrs$prd, "corr"] <- NA | ||
} | ||
gg <- ggplot2::ggplot(corrs, ggplot2::aes(x = .data$prd, y = .data$vrb, label = .data$corr, fill = .data$corr)) + | ||
ggplot2::geom_tile(color = "black") + | ||
ggplot2::scale_x_discrete(limits = vrb, position = "top") + | ||
ggplot2::scale_y_discrete(limits = rev(vrb)) + | ||
ggplot2::scale_fill_gradient2(low = "deepskyblue", mid = "lightyellow", high = "orangered", na.value = "white", limits = c(-1, 1)) + | ||
ggplot2::labs( | ||
x = "Imputation model predictor", | ||
y = "Variable to impute", | ||
fill = "Correlation*", | ||
caption = "*pairwise complete observations" | ||
) + | ||
theme_minimice() | ||
if (label) { | ||
gg <- gg + ggplot2::geom_text(color = "black", show.legend = FALSE) | ||
} | ||
if (square) { | ||
gg <- gg + ggplot2::coord_fixed() | ||
} | ||
if (rotate) { | ||
gg <- gg + ggplot2::theme(axis.text.x.top = ggplot2::element_text(angle = 90)) | ||
} | ||
return(gg) | ||
} | ||
|
||
# TODO: add plot for missingness indicators predictors | ||
# TODO: maybe add model.matrix argument to correlation plot? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# plot_flux <- function(dat) { | ||
# # escape function if dataset is complete | ||
# # if(!any(is.na(dat))){return(plot_a_mouse())} | ||
# # plot in and outflux | ||
# flx <- mice::flux(dat) %>% cbind(variable = rownames(.)) | ||
# gg <- flx %>% | ||
# ggplot2::ggplot(ggplot2::aes(x = influx, | ||
# y = outflux, | ||
# label = variable)) + | ||
# ggplot2::geom_abline(intercept = 1, | ||
# slope = -1, | ||
# linetype = "dashed") + | ||
# ggplot2::geom_text(position = ggplot2::position_jitter(width = 0.01, height = 0.01)) + | ||
# ggplot2::lims(x = c(-0.01, 1.01), y = c(-0.01, 1.01)) + | ||
# ggplot2::theme_classic() | ||
# # output | ||
# return(gg) | ||
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,5 @@ plot_pattern <- function(dat, square = FALSE, rotate = FALSE) { | |
|
||
return(gg) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
--- | ||
title: "old_friends" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{old_friends} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
```{r setup} | ||
library(ggmice) | ||
imp <- mice::mice(mice::boys, method = "pmm", printFlag = FALSE) | ||
``` | ||
|
||
How to re-create the plotting functions from `mice` with `ggmice`, in alphabetical order. | ||
|
||
# `mice::bwplot()` | ||
|
||
Box-and-whisker plot of observed and imputed data. | ||
|
||
```{r bwplot} | ||
# 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") | ||
``` | ||
|
||
# `mice::densityplot()` | ||
|
||
Density plot of observed and imputed data. | ||
|
||
```{r densityplot} | ||
# 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") | ||
``` | ||
|
||
# `mice::flux()` | ||
|
||
[To be added] | ||
|
||
# `mice::md.pattern()` | ||
|
||
Missing data pattern plot. | ||
|
||
```{r md.pattern} | ||
# original plot | ||
mice::md.pattern(imp$data) | ||
# ggmice equivalent | ||
plot_pattern(imp$data) | ||
``` | ||
|
||
# `mice::plot.mids()` | ||
|
||
Plot the trace lines of the MICE algorithm. | ||
|
||
```{r plot.mids} | ||
# original plot | ||
plot(imp, bmi ~ .it | .ms) | ||
# ggmice equivalent | ||
plot_trace(imp, "bmi") | ||
``` | ||
|
||
# `mice::stripplot()` | ||
|
||
Stripplot of observed and imputed data. | ||
|
||
```{r stripplot} | ||
# 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 | ||
``` | ||
|
||
# `mice::xyplot()` | ||
|
||
Scatterplot of observed and imputed data. | ||
|
||
```{r} | ||
# 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(shape = 1) | ||
``` | ||
|