From 39b6b1395714969f2f9270534799444252a71e82 Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Sun, 23 Jul 2023 23:22:07 +0300 Subject: [PATCH] detrend without qqplotr --- R/plot.check_normality.R | 66 ++++++++++++++++++++------------- man/plot.see_check_normality.Rd | 6 +-- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/R/plot.check_normality.R b/R/plot.check_normality.R index b4ff8750b..d3f6a3ffc 100644 --- a/R/plot.check_normality.R +++ b/R/plot.check_normality.R @@ -12,8 +12,8 @@ #' @param alpha Numeric value specifying alpha level of the confidence bands. #' @param colors Character vector of length two, indicating the colors (in #' hex-format) for points and line. -#' @param detrend Logical that decides if Q-Q and P-P plots should be de-trended. -#' Defaults to `TRUE` if the *qqplotr* package is installed and `FALSE` otherwise. +#' @param detrend Logical that decides if Q-Q and P-P plots should be de-trended +#' (also known as _worm plots_). #' @param method The method used for estimating the qq/pp bands. Default to #' `"ell"` (equal local levels / simultaneous testing - recommended). Can also #' be one of `"pointwise"` or `"boot"` for pointwise confidence bands, or @@ -43,7 +43,7 @@ plot.see_check_normality <- function(x, alpha = 0.2, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), - detrend = requireNamespace("qqplotr", quietly = TRUE), + detrend = TRUE, method = "ell", ...) { type <- match.arg(type) @@ -209,24 +209,34 @@ plot.see_check_normality <- function(x, detrend = detrend ) ) - y_lab <- "Sample Quantiles" + if (detrend) { + y_lab <- "Sample Quantile Deviations" + } else { + y_lab <- "Sample Quantiles" + } } else { - insight::format_alert( - paste0( - "For confidence bands", - if (isTRUE(detrend)) " and detrending", - ", please install `qqplotr`." - ) - ) + insight::format_alert("For confidence bands, please install `qqplotr`.") gg_init <- ggplot2::ggplot(x, ggplot2::aes(sample = .data$y)) + qq_stuff <- list( - ggplot2::geom_qq_line( - linewidth = size_line, - colour = colors[1], - na.rm = TRUE - ), + if (detrend) { + ggplot2::geom_hline( + yintercept = 0, + linewidth = size_line, + colour = colors[1], + na.rm = TRUE + ) + } else { + ggplot2::geom_qq_line( + linewidth = size_line, + colour = colors[1], + na.rm = TRUE + ) + } + , ggplot2::geom_qq( + mapping = if (detrend) ggplot2::aes(y = ggplot2::after_stat(sample) - ggplot2::after_stat(theoretical)), shape = 16, na.rm = TRUE, stroke = 0, @@ -234,7 +244,12 @@ plot.see_check_normality <- function(x, colour = colors[2] # "#2c3e50" ) ) - y_lab <- "Sample Quantiles" + + if (detrend) { + y_lab <- "Sample Quantile Deviations" + } else { + y_lab <- "Sample Quantiles" + } } if (!isTRUE(show_dots)) { @@ -284,11 +299,7 @@ plot.see_check_normality <- function(x, detrend = detrend ) } else if (requireNamespace("MASS", quietly = TRUE)) { - message( - "For confidence bands", - if (isTRUE(detrend)) " and detrending", - ", please install `qqplotr`." - ) + insight::format_alert("For confidence bands, please install `qqplotr`.") x$probs <- stats::ppoints(x$res) dparms <- MASS::fitdistr(x$res, densfun = "normal") @@ -296,24 +307,29 @@ plot.see_check_normality <- function(x, p_plot <- ggplot2::ggplot(x, ggplot2::aes(x = .data$probs, y = .data$y)) + ggplot2::geom_abline( - slope = 1, + slope = if (detrend) 0 else 1, linewidth = size_line, colour = colors[1] ) + geom_point2( + mapping = if (detrend) ggplot2::aes(y = .data$y - .data$probs), colour = colors[2], size = size_point, alpha = dot_alpha_level ) # "#2c3e50" } else { - stop("Package 'qqplotr' OR 'MASS' required for P-P plots. Please install one of them.", call. = FALSE) + insight::format_error("Package 'qqplotr' OR 'MASS' required for P-P plots. Please install one of them.") } + + y_lab <- "Sample Cummulative Probability" + if (detrend) y_lab <- paste0(y_lab, " Deviations") + p_plot + ggplot2::labs( title = "Normality of Residuals", subtitle = "Dots should fall along the line", - y = "Sample Cummulative Probability", + y = y_lab, x = "Standard Normal Cumulative Probability" ) + theme_style( diff --git a/man/plot.see_check_normality.Rd b/man/plot.see_check_normality.Rd index 56c788bef..43ece749d 100644 --- a/man/plot.see_check_normality.Rd +++ b/man/plot.see_check_normality.Rd @@ -13,7 +13,7 @@ alpha = 0.2, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), - detrend = requireNamespace("qqplotr", quietly = TRUE), + detrend = TRUE, method = "ell", ... ) @@ -40,8 +40,8 @@ statistical model.} \item{colors}{Character vector of length two, indicating the colors (in hex-format) for points and line.} -\item{detrend}{Logical that decides if Q-Q and P-P plots should be de-trended. -Defaults to \code{TRUE} if the \emph{qqplotr} package is installed and \code{FALSE} otherwise.} +\item{detrend}{Logical that decides if Q-Q and P-P plots should be de-trended +(also known as \emph{worm plots}).} \item{method}{The method used for estimating the qq/pp bands. Default to \code{"ell"} (equal local levels / simultaneous testing - recommended). Can also