From 6ebf7f5d15f755a84110cac6afc6a83b37b39a7d Mon Sep 17 00:00:00 2001 From: hanneoberman Date: Mon, 7 Aug 2023 15:22:04 +0200 Subject: [PATCH] Revert `plot_variance()` to concept Move the function to separate branch for further development (as discussed in the MICE meeting) before submission to CRAN --- NAMESPACE | 1 - NEWS.md | 1 - R/plot_variance.R | 84 ----------------------------- man/plot_variance.Rd | 29 ---------- tests/testthat/test-plot_variance.R | 13 ----- vignettes/ggmice.Rmd | 8 --- 6 files changed, 136 deletions(-) delete mode 100644 R/plot_variance.R delete mode 100644 man/plot_variance.Rd delete mode 100644 tests/testthat/test-plot_variance.R diff --git a/NAMESPACE b/NAMESPACE index d50d40f8..5a976a40 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,6 @@ export(plot_flux) export(plot_pattern) export(plot_pred) export(plot_trace) -export(plot_variance) export(stripplot) export(xyplot) importFrom(magrittr,"%>%") diff --git a/NEWS.md b/NEWS.md index 5a26c576..caa4e407 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,7 +7,6 @@ ## New features -* New plotting function `plot_variance()` visualizes post-imputation variability (#56) * New optional argument `plot_pred()` shows methods vector with predictor matrix plot (#71) * New optional argument `plot_pattern()` hides less frequent patterns (#77) * New optional argument `plot_pattern()` hides legend caption (#111) diff --git a/R/plot_variance.R b/R/plot_variance.R deleted file mode 100644 index ac0096f5..00000000 --- a/R/plot_variance.R +++ /dev/null @@ -1,84 +0,0 @@ -#' Plot the scaled between imputation variance for every cell as a heatmap -#' -#' This function plots the cell-level between imputation variance. The function -#' scales the variances column-wise, without centering cf. `base::scale(center = FALSE)` -#' and plots the data image as a heatmap. Darker red cells indicate more variance, -#' lighter cells indicate less variance. White cells represent observed cells or unobserved cells with zero between -#' imputation variance. -#' -#' @param data A multiply imputed object of class [`mice::mids`]. -#' @param grid Logical indicating whether grid lines should be displayed. -#' @param caption Logical indicating whether the figure caption should be displayed. -#' -#' @return An object of class `ggplot`. -#' @examples -#' imp <- mice::mice(mice::nhanes, printFlag = FALSE) -#' plot_variance(imp) -#' @export -plot_variance <- function(data, grid = TRUE, caption = TRUE) { - verify_data(data, imp = TRUE) - if (data$m < 2) { - cli::cli_abort( - c( - "The between imputation variance cannot be computed if there are fewer than two imputations (m < 2).", - "i" = "Please provide an object with 2 or more imputations." - ) - ) - } - if (grid) { - gridcol <- "black" - } else { - gridcol <- NA - } - if (caption) { - lab_fill <- "Imputation variability* - " - lab_cap <- "*scaled cell-level between imputation variance" - } else { - lab_fill <- "Imputation variability" - lab_cap <- NULL - } - - gg <- mice::complete(data, "long") %>% - dplyr::mutate(dplyr::across(where(is.factor), as.numeric)) %>% - dplyr::select(-.imp) %>% - dplyr::group_by(.id) %>% - dplyr::summarise(dplyr::across(dplyr::everything(), stats::var)) %>% - dplyr::ungroup() %>% - dplyr::mutate(dplyr::across(.cols = -.id, ~ scale_above_zero(.))) %>% - tidyr::pivot_longer(cols = -.id) %>% - ggplot2::ggplot(ggplot2::aes(name, .id, fill = value)) + - ggplot2::geom_tile(color = gridcol) + - ggplot2::scale_fill_gradient(low = "white", high = mice::mdc(2)) + - ggplot2::labs( - x = "Column name", - y = "Row number", - fill = lab_fill, - caption = lab_cap - ) + - ggplot2::scale_x_discrete(position = "top", expand = c(0, 0)) + - ggplot2::scale_y_continuous(trans = "reverse", expand = c(0, 0)) + - theme_minimice() - - if (!grid) { - gg <- - gg + ggplot2::theme(panel.border = ggplot2::element_rect(fill = NA)) - } - - # return the ggplot object - return(gg) -} - -#' Utils function to scale only non-zero values without centering -#' -#' @param x An object of class 'matrix' or 'data.frame' -#' -#' @return A matrix with the scaled data -#' -#' @keywords internal -#' @noRd -scale_above_zero <- function(x) { - x <- as.matrix(x) - x[x != 0] <- scale(x[x != 0], center = FALSE) - return(x) -} diff --git a/man/plot_variance.Rd b/man/plot_variance.Rd deleted file mode 100644 index bb03db57..00000000 --- a/man/plot_variance.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/plot_variance.R -\name{plot_variance} -\alias{plot_variance} -\title{Plot the scaled between imputation variance for every cell as a heatmap} -\usage{ -plot_variance(data, grid = TRUE, caption = TRUE) -} -\arguments{ -\item{data}{A multiply imputed object of class \code{\link[mice:mids-class]{mice::mids}}.} - -\item{grid}{Logical indicating whether grid lines should be displayed.} - -\item{caption}{Logical indicating whether the figure caption should be displayed.} -} -\value{ -An object of class \code{ggplot}. -} -\description{ -This function plots the cell-level between imputation variance. The function -scales the variances column-wise, without centering cf. \code{base::scale(center = FALSE)} -and plots the data image as a heatmap. Darker red cells indicate more variance, -lighter cells indicate less variance. White cells represent observed cells or unobserved cells with zero between -imputation variance. -} -\examples{ -imp <- mice::mice(mice::nhanes, printFlag = FALSE) -plot_variance(imp) -} diff --git a/tests/testthat/test-plot_variance.R b/tests/testthat/test-plot_variance.R deleted file mode 100644 index 672eca98..00000000 --- a/tests/testthat/test-plot_variance.R +++ /dev/null @@ -1,13 +0,0 @@ -# create test objects -dat <- mice::nhanes -imp <- mice::mice(dat, printFlag = FALSE) - -# tests -test_that("plot_variance produces ggplot object", { - expect_s3_class(plot_variance(imp), "ggplot") - expect_s3_class(plot_variance(imp, grid = FALSE), "ggplot") -}) - -test_that("plot_variance returns error with incorrect arguments", { - expect_error(plot_variance(dat)) -}) diff --git a/vignettes/ggmice.Rmd b/vignettes/ggmice.Rmd index d0fc27f9..10a1abc9 100644 --- a/vignettes/ggmice.Rmd +++ b/vignettes/ggmice.Rmd @@ -281,14 +281,6 @@ The function `plot_trace()` plots the trace lines of the MICE algorithm for conv plot_trace(imp, "hgt") ``` -## Between-imputation variance - -The function `plot_variance()` visualizes the cell-level between-imputation variance as a heatmap. The color of the tiles in the plot represent the scaled between-imputation variance for the imputed cells in the data. The `data` argument requires an object of class `mice::mids`. - -```{r variance} -# create heatmap of between-imputation variance -plot_variance(imp) -``` ---