From e1e8aa3675e4e3f1aa2893469ae8c7d2d2f03682 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 20 Mar 2024 21:57:18 +0100 Subject: [PATCH 1/4] Improve and add plots for (simulated) residuals --- DESCRIPTION | 2 +- NEWS.md | 6 ++++-- R/plot.check_normality.R | 24 +++++++++++++++++++++++- R/plot.check_predictions.R | 2 +- R/plot.performance_simres.R | 29 +++++++++++++++++++---------- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e10870119..caae07f31 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: see Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2' -Version: 0.8.2.6 +Version: 0.8.2.7 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NEWS.md b/NEWS.md index 110f7f04e..3b7cb0076 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,9 +2,11 @@ ## Major changes -- New `plot()` method for simulated residuals (implemented in the _performance_ package). +- New `plot()` method for simulated residuals (implemented in the _performance_ + package). -- `plot()` for `check_model()` was revised and now includes more accurate Q-Q plots for non-Gaussian models. +- `plot()` for `check_model()` was revised and now includes more accurate Q-Q + plots for non-Gaussian models. ## Minor Changes diff --git a/R/plot.check_normality.R b/R/plot.check_normality.R index 5279a6fc3..d44b5f5a0 100644 --- a/R/plot.check_normality.R +++ b/R/plot.check_normality.R @@ -80,6 +80,21 @@ plot.see_check_normality <- function(x, res_ <- abs(stats::rstandard(model, type = "deviance")) fitted_ <- stats::qnorm((stats::ppoints(length(res_)) + 1) / 2)[order(order(res_))] dat <- stats::na.omit(data.frame(x = fitted_, y = res_)) + } else if (inherits(model, "performance_simres")) { + return(plot.see_performance_simres( + model, + size_line = size_line, + size_point = size_point, + alpha = alpha, + dot_alpha = dot_alpha, + colors = colors, + detrend = detrend, + transform = stats::qnorm, + ... + )) + } else if (is.numeric(model)) { + res_ <- sort(model[!is.infinite(model)]) + dat <- stats::na.omit(data.frame(y = res_)) } else { res_ <- sort(stats::rstudent(model), na.last = NA) dat <- stats::na.omit(data.frame(y = res_)) @@ -97,7 +112,14 @@ plot.see_check_normality <- function(x, model_class = class(model)[1] ) } else if (type == "density") { - r <- suppressMessages(stats::residuals(model)) + if (inherits(model, "performance_simres")) { + r <- stats::residuals(model, quantile_function = stats::qnorm) + r <- r[!is.infinite(r)] + } else if (is.numeric(model)) { + r <- model[!is.infinite(model) & !is.na(model)] + } else { + r <- suppressMessages(stats::residuals(model)) + } dat <- as.data.frame(bayestestR::estimate_density(r)) dat$curve <- stats::dnorm( seq(min(dat$x), max(dat$x), length.out = nrow(dat)), diff --git a/R/plot.check_predictions.R b/R/plot.check_predictions.R index 0e2ffcea5..36f1e9913 100644 --- a/R/plot.check_predictions.R +++ b/R/plot.check_predictions.R @@ -327,7 +327,7 @@ plot.see_performance_pp_check <- function(x, color = .data$key ), position = ggplot2::position_nudge(x = 0.2), - size = size_line, + size = size_point, linewidth = size_line, stroke = 0, shape = 16 diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index 24ac0853f..4ed7f9d6c 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -40,8 +40,6 @@ plot.see_performance_simres <- function(x, transform = NULL, style = theme_lucid, ...) { - dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) - # need DHARMa to be installed insight::check_if_installed("DHARMa") @@ -50,10 +48,21 @@ plot.see_performance_simres <- function(x, x <- attributes(x)$data } + dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) + dp_band <- list(min = 0, max = 1) + dfun <- "unif" + + # prepare arguments, based on transformation if (is.null(transform)) { res <- stats::residuals(x) } else { res <- stats::residuals(x, quantileFunction = transform) + res <- res[!is.infinite(res)] + if (identical(transform, stats::qnorm)) { + dp <- list(mean = 0, sd = 1) + dp_band <- list(mean = 0, sd = 1) + dfun <- "norm" + } } # base plot information @@ -66,20 +75,20 @@ plot.see_performance_simres <- function(x, if (requireNamespace("qqplotr", quietly = TRUE)) { qq_stuff <- list( qqplotr::stat_qq_band( - distribution = "unif", - dparams = list(min = 0, max = 1), + distribution = dfun, + dparams = dp_band, alpha = alpha, detrend = detrend ), qqplotr::stat_qq_line( - distribution = "unif", + distribution = dfun, dparams = dp, size = size_line, colour = colors[1], detrend = detrend ), qqplotr::stat_qq_point( - distribution = "unif", + distribution = dfun, dparams = dp, size = size_point, alpha = dot_alpha, @@ -98,7 +107,7 @@ plot.see_performance_simres <- function(x, ggplot2::geom_qq( shape = 16, stroke = 0, - distribution = stats::qunif, + distribution = dfun, dparams = dp, size = size_point, colour = colors[2] @@ -107,7 +116,7 @@ plot.see_performance_simres <- function(x, linewidth = size_line, colour = colors[1], na.rm = TRUE, - distribution = stats::qunif, + distribution = dfun, dparams = dp ) ) @@ -117,9 +126,9 @@ plot.see_performance_simres <- function(x, gg_init + qq_stuff + ggplot2::labs( - title = "Uniformity of Residuals", + title = ifelse(is.null(transform), "Uniformity of Residuals", "Residuals Check"), subtitle = "Dots should fall along the line", - x = "Standard Uniform Distribution Quantiles", + x = ifelse(is.null(transform), "Standard Uniform Distribution Quantiles", "Distribution of Quantiles"), y = y_lab ) + style( From b801dfc1607d32d9a8a572c42036c7eee394d5ac Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 20 Mar 2024 22:00:07 +0100 Subject: [PATCH 2/4] better titles --- R/plot.performance_simres.R | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index 4ed7f9d6c..889ecad70 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -123,12 +123,23 @@ plot.see_performance_simres <- function(x, y_lab <- "Sample Quantiles" } + if (is.null(transform)) { + p_title <- "Uniformity of Residuals" + p_x <- "Standard Uniform Distribution Quantiles" + } else if (identical(transform, stats::qnorm)) { + p_title <- "Normality of Residuals" + p_x <- "Standard Normal Distribution Quantiles" + } else { + p_title <- "Residuals Check" + p_x <- "Distribution of Quantiles" + } + gg_init + qq_stuff + ggplot2::labs( - title = ifelse(is.null(transform), "Uniformity of Residuals", "Residuals Check"), + title = p_title, subtitle = "Dots should fall along the line", - x = ifelse(is.null(transform), "Standard Uniform Distribution Quantiles", "Distribution of Quantiles"), + x = p_x, y = y_lab ) + style( From ba35bc187f471787cd51bcf49055576d790c89cc Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 20 Mar 2024 22:01:23 +0100 Subject: [PATCH 3/4] minor --- R/plot.check_normality.R | 2 +- R/plot.performance_simres.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/plot.check_normality.R b/R/plot.check_normality.R index d44b5f5a0..6d494420c 100644 --- a/R/plot.check_normality.R +++ b/R/plot.check_normality.R @@ -39,7 +39,7 @@ plot.see_check_normality <- function(x, type = c("qq", "pp", "density"), data = NULL, size_line = 0.8, - size_point = 2, + size_point = 1.5, alpha = 0.2, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index 889ecad70..c613ae0ad 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -32,7 +32,7 @@ #' @export plot.see_performance_simres <- function(x, size_line = 0.8, - size_point = 1, + size_point = 1.5, alpha = 0.2, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), From 0672f6dd9da67062f3c056dfd712132127f28365 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 20 Mar 2024 22:13:44 +0100 Subject: [PATCH 4/4] update RD --- man/plot.see_check_normality.Rd | 2 +- man/plot.see_performance_simres.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/man/plot.see_check_normality.Rd b/man/plot.see_check_normality.Rd index 43ece749d..5e6587dfc 100644 --- a/man/plot.see_check_normality.Rd +++ b/man/plot.see_check_normality.Rd @@ -9,7 +9,7 @@ type = c("qq", "pp", "density"), data = NULL, size_line = 0.8, - size_point = 2, + size_point = 1.5, alpha = 0.2, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), diff --git a/man/plot.see_performance_simres.Rd b/man/plot.see_performance_simres.Rd index 6520442bc..ce4bb25c8 100644 --- a/man/plot.see_performance_simres.Rd +++ b/man/plot.see_performance_simres.Rd @@ -7,7 +7,7 @@ \method{plot}{see_performance_simres}( x, size_line = 0.8, - size_point = 1, + size_point = 1.5, alpha = 0.2, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"),