From 7b107d9441a46e3357478f574fab5c850545bed6 Mon Sep 17 00:00:00 2001 From: hanneoberman Date: Thu, 21 Dec 2023 15:40:00 +0100 Subject: [PATCH] Remove `plot_variance()` in line with `main` --- NAMESPACE | 1 - R/plot_variance.R | 71 -------------------------------------------- man/plot_variance.Rd | 29 ------------------ 3 files changed, 101 deletions(-) delete mode 100644 R/plot_variance.R delete mode 100644 man/plot_variance.Rd diff --git a/NAMESPACE b/NAMESPACE index 12244cea..8cf346e8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,7 +10,6 @@ export(plot_miss) export(plot_pattern) export(plot_pred) export(plot_trace) -export(plot_variance) export(stripplot) export(xyplot) importFrom(magrittr,"%>%") diff --git a/R/plot_variance.R b/R/plot_variance.R deleted file mode 100644 index ff63b6ab..00000000 --- a/R/plot_variance.R +++ /dev/null @@ -1,71 +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 package `mice` generated multiply imputed data set of class -#' `mids`. Non-`mids` objects that have not been generated with `mice::mice()` -#' can be converted through a pipeline with `mice::as.mids()`. -#' @param grid Logical indicating whether grid lines 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) { - 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 - } - - 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 = "Imputation variability* - ", - caption = "*scaled cell-level between imputation variance" - ) + # "Cell-level between imputation\nvariance (scaled)\n\n" - 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) -} - -# function to scale only non-zero values without centering -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 ade26899..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) -} -\arguments{ -\item{data}{A package \code{mice} generated multiply imputed data set of class -\code{mids}. Non-\code{mids} objects that have not been generated with \code{mice::mice()} -can be converted through a pipeline with \code{mice::as.mids()}.} - -\item{grid}{Logical indicating whether grid lines 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) -}