From 2bebe138aaf4600338387f4a73d265585bf349c8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 12:01:02 +0100 Subject: [PATCH 01/13] Draft plotting simulated residuals --- R/data_plot.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/data_plot.R b/R/data_plot.R index c88c21d5a..d7ce90069 100644 --- a/R/data_plot.R +++ b/R/data_plot.R @@ -148,6 +148,11 @@ add_plot_attributes <- function(x) { obj_name <- attr(x, "object_name", exact = TRUE) dat <- NULL + # for simulated residuals, we save all necessary information in the object + if (inherits(x, "performance_simres")) { + return(x$fittedModel) + } + if (!is.null(obj_name)) { # first try, parent frame dat <- tryCatch(get(obj_name, envir = parent.frame()), error = function(e) NULL) From 642fab6c5dc7507a97467f1b2c0517d31cf762ab Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 12:28:24 +0100 Subject: [PATCH 02/13] draft plot for check_residuals --- NAMESPACE | 2 + R/plot.performance_simres.R | 108 +++++++++++++++++++++++++++++ man/geom_violindot.Rd | 29 ++------ man/geom_violinhalf.Rd | 63 ++++------------- man/plot.see_performance_simres.Rd | 63 +++++++++++++++++ 5 files changed, 190 insertions(+), 75 deletions(-) create mode 100644 R/plot.performance_simres.R create mode 100644 man/plot.see_performance_simres.Rd diff --git a/NAMESPACE b/NAMESPACE index c4027f24d..4efe799b3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -38,6 +38,7 @@ S3method(plot,see_check_model) S3method(plot,see_check_normality) S3method(plot,see_check_outliers) S3method(plot,see_check_overdisp) +S3method(plot,see_check_residuals) S3method(plot,see_compare_parameters) S3method(plot,see_compare_performance) S3method(plot,see_effectsize_table) @@ -64,6 +65,7 @@ S3method(plot,see_parameters_sem) S3method(plot,see_parameters_simulate) S3method(plot,see_performance_pp_check) S3method(plot,see_performance_roc) +S3method(plot,see_performance_simres) S3method(plot,see_point_estimate) S3method(plot,see_rope) S3method(plot,see_si) diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R new file mode 100644 index 000000000..2046358e2 --- /dev/null +++ b/R/plot.performance_simres.R @@ -0,0 +1,108 @@ +#' Plot method for check model for (non-)normality of residuals +#' +#' The `plot()` method for the `performance::check_residuals()` resp. +#' `performance::simulate_residuals()` function. +#' +#' @inheritParams plot.see_check_normality +#' +#' @return A ggplot2-object. +#' +#' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html). +#' +#' @examplesIf require("performance") && require("glmmTMB") && require("qqplotr") +#' data(Salamanders, package = "glmmTMB") +#' model <- glmmTMB::glmmTMB( +#' count ~ mined + spp + (1 | site), +#' family = poisson(), +#' data = Salamanders +#' ) +#' simulated_residuals <- performance::simulate_residuals(model) +#' plot(simulated_residuals) +#' +#' # or +#' simulated_residuals <- performance::simulate_residuals(model) +#' result <- performance::check_residuals(simulated_residuals) +#' plot(result) +#' +#' @export +plot.see_performance_simres <- function(x, + size_line = 0.8, + size_point = 1, + alpha = 0.2, + dot_alpha = 0.8, + colors = c("#3aaf85", "#1b6ca8"), + detrend = FALSE, + ...) { + dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) + + # base plot information + gg_init <- ggplot2::ggplot( + data.frame(scaled_residuals = stats::residuals(x)), + ggplot2::aes(sample = scaled_residuals) + ) + + # when we have package qqplotr, we can add confidence bands + if (requireNamespace("qqplotr", quietly = TRUE)) { + qq_stuff <- list( + qqplotr::stat_qq_band( + distribution = "unif", + dparams = list(min = 0, max = 1), + alpha = alpha, + detrend = detrend + ), + qqplotr::stat_qq_line( + distribution = "unif", + dparams = dp, + size = size_line, + colour = colors[1], + detrend = detrend + ), + qqplotr::stat_qq_point( + distribution = "unif", + dparams = dp, + size = size_point, + alpha = dot_alpha, + colour = colors[2], + detrend = detrend + ) + ) + if (detrend) { + y_lab <- "Sample Quantile Deviations" + } else { + y_lab <- "Sample Quantiles" + } + } else { + insight::format_alert("For confidence bands, please install `qqplotr`.") + qq_stuff <- list( + ggplot2::geom_point( + shape = 16, + stroke = 0, + size = size_point, + colour = colors[2] + ), + ggplot2::geom_qq_line( + linewidth = size_line, + colour = colors[1], + na.rm = TRUE + ) + ) + y_lab <- "Sample Quantiles" + } + + gg_init + + qq_stuff + + ggplot2::labs( + title = "Uniformity of Residuals", + subtitle = "Dots should fall along the line", + x = "Standard Uniform Distribution Quantiles", + y = y_lab + ) + + theme_lucid( + base_size = 10, + plot.title.space = 3, + axis.title.space = 5 + ) +} + +#' @export +plot.see_check_residuals <- plot.see_performance_simres diff --git a/man/geom_violindot.Rd b/man/geom_violindot.Rd index b7cdeee8c..b02b4b4cf 100644 --- a/man/geom_violindot.Rd +++ b/man/geom_violindot.Rd @@ -68,31 +68,10 @@ Defaults to 1/30 of the range of the data} \item{position_dots}{Position adjustment for dots, either as a string, or the result of a call to a position adjustment function.} -\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}'s \code{params} argument. These -arguments broadly fall into one of 4 categories below. Notably, further -arguments to the \code{position} argument, or aesthetics that are required -can \emph{not} be passed through \code{...}. Unknown arguments that are not part -of the 4 categories below are ignored. -\itemize{ -\item Static aesthetics that are not mapped to a scale, but are at a fixed -value and apply to the layer as a whole. For example, \code{colour = "red"} -or \code{linewidth = 3}. The geom's documentation has an \strong{Aesthetics} -section that lists the available options. The 'required' aesthetics -cannot be passed on to the \code{params}. -\item When constructing a layer using -a \verb{stat_*()} function, the \code{...} argument can be used to pass on -parameters to the \code{geom} part of the layer. An example of this is -\code{stat_density(geom = "area", outline.type = "both")}. The geom's -documentation lists which parameters it can accept. -\item Inversely, when constructing a layer using a -\verb{geom_*()} function, the \code{...} argument can be used to pass on parameters -to the \code{stat} part of the layer. An example of this is -\code{geom_area(stat = "density", adjust = 0.5)}. The stat's documentation -lists which parameters it can accept. -\item The \code{key_glyph} argument of \code{\link[ggplot2:layer]{layer()}} may also be passed on through -\code{...}. This can be one of the functions described as -\link[ggplot2:draw_key]{key glyphs}, to change the display of the layer in the legend. -}} +\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}. These are +often aesthetics, used to set an aesthetic to a fixed value, like +\code{colour = "red"} or \code{size = 3}. They may also be parameters +to the paired geom/stat.} \item{size_dots, dots_size}{Size adjustment for dots.} diff --git a/man/geom_violinhalf.Rd b/man/geom_violinhalf.Rd index 8bf3734d2..82b68ae8e 100644 --- a/man/geom_violinhalf.Rd +++ b/man/geom_violinhalf.Rd @@ -38,31 +38,15 @@ the plot data. The return value must be a \code{data.frame}, and will be used as the layer data. A \code{function} can be created from a \code{formula} (e.g. \code{~ head(.x, 10)}).} -\item{stat}{The statistical transformation to use on the data for this layer. -When using a \verb{geom_*()} function to construct a layer, the \code{stat} -argument can be used the override the default coupling between geoms and -stats. The \code{stat} argument accepts the following: -\itemize{ -\item A \code{Stat} ggproto subclass, for example \code{StatCount}. -\item A string naming the stat. To give the stat as a string, strip the -function name of the \code{stat_} prefix. For example, to use \code{stat_count()}, -give the stat as \code{"count"}. -\item For more information and other ways to specify the stat, see the -\link[ggplot2:layer_stats]{layer stat} documentation. -}} - -\item{position}{A position adjustment to use on the data for this layer. This -can be used in various ways, including to prevent overplotting and -improving the display. The \code{position} argument accepts the following: -\itemize{ -\item The result of calling a position function, such as \code{position_jitter()}. -This method allows for passing extra arguments to the position. -\item A string naming the position adjustment. To give the position as a -string, strip the function name of the \code{position_} prefix. For example, -to use \code{position_jitter()}, give the position as \code{"jitter"}. -\item For more information and other ways to specify the position, see the -\link[ggplot2:layer_positions]{layer position} documentation. -}} +\item{stat}{The statistical transformation to use on the data for this +layer, either as a \code{ggproto} \code{Geom} subclass or as a string naming the +stat stripped of the \code{stat_} prefix (e.g. \code{"count"} rather than +\code{"stat_count"})} + +\item{position}{Position adjustment, either as a string naming the adjustment +(e.g. \code{"jitter"} to use \code{position_jitter}), or the result of a call to a +position adjustment function. Use the latter if you need to change the +settings of the adjustment.} \item{trim}{If \code{TRUE} (default), trim the tails of the violins to the range of the data. If \code{FALSE}, don't trim the tails.} @@ -88,31 +72,10 @@ rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. \code{\link[ggplot2:borders]{borders()}}.} -\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}'s \code{params} argument. These -arguments broadly fall into one of 4 categories below. Notably, further -arguments to the \code{position} argument, or aesthetics that are required -can \emph{not} be passed through \code{...}. Unknown arguments that are not part -of the 4 categories below are ignored. -\itemize{ -\item Static aesthetics that are not mapped to a scale, but are at a fixed -value and apply to the layer as a whole. For example, \code{colour = "red"} -or \code{linewidth = 3}. The geom's documentation has an \strong{Aesthetics} -section that lists the available options. The 'required' aesthetics -cannot be passed on to the \code{params}. -\item When constructing a layer using -a \verb{stat_*()} function, the \code{...} argument can be used to pass on -parameters to the \code{geom} part of the layer. An example of this is -\code{stat_density(geom = "area", outline.type = "both")}. The geom's -documentation lists which parameters it can accept. -\item Inversely, when constructing a layer using a -\verb{geom_*()} function, the \code{...} argument can be used to pass on parameters -to the \code{stat} part of the layer. An example of this is -\code{geom_area(stat = "density", adjust = 0.5)}. The stat's documentation -lists which parameters it can accept. -\item The \code{key_glyph} argument of \code{\link[ggplot2:layer]{layer()}} may also be passed on through -\code{...}. This can be one of the functions described as -\link[ggplot2:draw_key]{key glyphs}, to change the display of the layer in the legend. -}} +\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}. These are +often aesthetics, used to set an aesthetic to a fixed value, like +\code{colour = "red"} or \code{size = 3}. They may also be parameters +to the paired geom/stat.} } \description{ Create a half-violin plot. diff --git a/man/plot.see_performance_simres.Rd b/man/plot.see_performance_simres.Rd new file mode 100644 index 000000000..16d33454e --- /dev/null +++ b/man/plot.see_performance_simres.Rd @@ -0,0 +1,63 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot.performance_simres.R +\name{plot.see_performance_simres} +\alias{plot.see_performance_simres} +\title{Plot method for check model for (non-)normality of residuals} +\usage{ +\method{plot}{see_performance_simres}( + x, + size_line = 0.8, + size_point = 1, + alpha = 0.2, + dot_alpha = 0.8, + colors = c("#3aaf85", "#1b6ca8"), + detrend = TRUE, + ... +) +} +\arguments{ +\item{x}{An object.} + +\item{size_line}{Numeric value specifying size of line geoms.} + +\item{size_point}{Numeric specifying size of point-geoms.} + +\item{alpha}{Numeric value specifying alpha level of the confidence bands.} + +\item{dot_alpha}{Numeric value specifying alpha level of the point geoms.} + +\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 +(also known as \emph{worm plots}).} + +\item{...}{Arguments passed to or from other methods.} +} +\value{ +A ggplot2-object. +} +\description{ +The \code{plot()} method for the \code{performance::check_residuals()} resp. +\code{performance::simulate_residuals()} function. +} +\examples{ +\dontshow{if (require("performance") && require("glmmTMB") && require("qqplotr")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +data(Salamanders, package = "glmmTMB") +model <- glmmTMB::glmmTMB( + count ~ mined + spp + (1 | site), + family = poisson(), + data = Salamanders +) +simulated_residuals <- performance::simulate_residuals(model) +plot(simulated_residuals) + +# or +simulated_residuals <- performance::simulate_residuals(model) +result <- performance::check_residuals(simulated_residuals) +plot(result) +\dontshow{\}) # examplesIf} +} +\seealso{ +See also the vignette about \href{https://easystats.github.io/performance/articles/check_model.html}{\code{check_model()}}. +} From c1611fdc57938602925adce70d5d3a673250fb0f Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 12:56:14 +0100 Subject: [PATCH 03/13] fix --- R/plot.performance_simres.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index 2046358e2..5b2b0c010 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -38,7 +38,7 @@ plot.see_performance_simres <- function(x, # base plot information gg_init <- ggplot2::ggplot( data.frame(scaled_residuals = stats::residuals(x)), - ggplot2::aes(sample = scaled_residuals) + ggplot2::aes(sample = .data$scaled_residuals) ) # when we have package qqplotr, we can add confidence bands From 020d799a521344bdbc97c8484bdaaf6d2e41a35d Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 12:57:40 +0100 Subject: [PATCH 04/13] docs --- man/plot.see_performance_simres.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/plot.see_performance_simres.Rd b/man/plot.see_performance_simres.Rd index 16d33454e..f6f7a2461 100644 --- a/man/plot.see_performance_simres.Rd +++ b/man/plot.see_performance_simres.Rd @@ -11,7 +11,7 @@ alpha = 0.2, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), - detrend = TRUE, + detrend = FALSE, ... ) } From ff6f1380a823a1f290b9689b4fede0c9a355bcbf Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 13:06:42 +0100 Subject: [PATCH 05/13] include in check_model --- R/plot.check_model.R | 40 ++++++++++++++++++++---------- R/plot.performance_simres.R | 4 ++- man/plot.see_performance_simres.Rd | 3 +++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/R/plot.check_model.R b/R/plot.check_model.R index 97acb31a7..95436aaf2 100644 --- a/R/plot.check_model.R +++ b/R/plot.check_model.R @@ -165,19 +165,33 @@ plot.see_check_model <- function(x, } if ("QQ" %in% names(x) && !is.null(x$QQ) && any(c("qq", "all") %in% check)) { - p$QQ <- .plot_diag_qq( - x$QQ, - size_point, - size_line, - alpha_level = alpha_level, - detrend = detrend, - theme_style = style, - colors = colors, - dot_alpha_level = dot_alpha_level, - show_dots = TRUE, # qq-plots w/o dots makes no sense - model_info = model_info, - model_class = model_class - ) + if (inherits(x$QQ, "performance_simres")) { + p$QQ <- plot( + x$QQ, + size_line = size_line, + size_point = size_point, + alpha = alpha_level, + dot_alpha = dot_alpha_level, + colors = colors, + detrend = detrend, + style = style + + ) + } else { + p$QQ <- .plot_diag_qq( + x$QQ, + size_point, + size_line, + alpha_level = alpha_level, + detrend = detrend, + theme_style = style, + colors = colors, + dot_alpha_level = dot_alpha_level, + show_dots = TRUE, # qq-plots w/o dots makes no sense + model_info = model_info, + model_class = model_class + ) + } } if ("NORM" %in% names(x) && !is.null(x$NORM) && any(c("normality", "all") %in% check)) { diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index 5b2b0c010..2ad34cceb 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -4,6 +4,7 @@ #' `performance::simulate_residuals()` function. #' #' @inheritParams plot.see_check_normality +#' @inheritParams plot.see_check_model #' #' @return A ggplot2-object. #' @@ -32,6 +33,7 @@ plot.see_performance_simres <- function(x, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), detrend = FALSE, + style = theme_lucid, ...) { dp <- list(min = 0, max = 1, lower.tail = TRUE, log.p = FALSE) @@ -97,7 +99,7 @@ plot.see_performance_simres <- function(x, x = "Standard Uniform Distribution Quantiles", y = y_lab ) + - theme_lucid( + style( base_size = 10, plot.title.space = 3, axis.title.space = 5 diff --git a/man/plot.see_performance_simres.Rd b/man/plot.see_performance_simres.Rd index f6f7a2461..9b9dd0f43 100644 --- a/man/plot.see_performance_simres.Rd +++ b/man/plot.see_performance_simres.Rd @@ -12,6 +12,7 @@ dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), detrend = FALSE, + style = theme_lucid, ... ) } @@ -32,6 +33,8 @@ hex-format) for points and line.} \item{detrend}{Logical that decides if Q-Q and P-P plots should be de-trended (also known as \emph{worm plots}).} +\item{style}{A ggplot2-theme.} + \item{...}{Arguments passed to or from other methods.} } \value{ From 731a3927cf0f1a759c0f3b908d449984463480b3 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 13:06:54 +0100 Subject: [PATCH 06/13] version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index b2d51ad1c..d7c303456 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.4 +Version: 0.8.2.5 Authors@R: c(person(given = "Daniel", family = "Lüdecke", From b9a8e8aa249407b036fa4813fc94986775f4826e Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 13:09:59 +0100 Subject: [PATCH 07/13] lintr --- R/plot.parameters_model.R | 44 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/R/plot.parameters_model.R b/R/plot.parameters_model.R index 7cbba6ecb..6206c4066 100644 --- a/R/plot.parameters_model.R +++ b/R/plot.parameters_model.R @@ -595,28 +595,28 @@ plot.see_parameters_model <- function(x, .meta_measure <- function(meta_measure) { switch(meta_measure, - "MD" = "Raw Mean Difference", - "SMDH" = , - "SMD" = "Standardized Mean Difference", - "ROM" = "Log transformed Ratio of Means", - "D2ORL" = , - "D2ORN" = "Transformed Standardized Mean Difference", - "UCOR" = , - "COR" = "Raw Correlation Coefficient", - "ZCOR" = "Z transformed Correlation Coefficient", - "PHI" = "Phi Coefficient", - "RR" = "Log Risk Ratio", - "OR" = "Log Odds Ratio", - "RD" = "Risk Difference", - "AS" = "Root transformed Risk Difference", - "PETO" = "Peto's Log Odds Ratio", - "PBIT" = "Standardized Mean Difference (Probit-transformed)", - "OR2DL" = , - "OR2DN" = "Standardized Mean Difference (Odds Ratio-transformed)", - "IRR" = "Log Incidence Rate Ratio", - "IRD" = "Incidence Rate Difference", - "IRSD" = "Square Root transformed Incidence Rate Difference", - "GEN" = "Generic Estimate", + MD = "Raw Mean Difference", + SMDH = , + SMD = "Standardized Mean Difference", + ROM = "Log transformed Ratio of Means", + D2ORL = , + D2ORN = "Transformed Standardized Mean Difference", + UCOR = , + COR = "Raw Correlation Coefficient", + ZCOR = "Z transformed Correlation Coefficient", + PHI = "Phi Coefficient", + RR = "Log Risk Ratio", + OR = "Log Odds Ratio", + RD = "Risk Difference", + AS = "Root transformed Risk Difference", + PETO = "Peto's Log Odds Ratio", + PBIT = "Standardized Mean Difference (Probit-transformed)", + OR2DL = , + OR2DN = "Standardized Mean Difference (Odds Ratio-transformed)", + IRR = "Log Incidence Rate Ratio", + IRD = "Incidence Rate Difference", + IRSD = "Square Root transformed Incidence Rate Difference", + GEN = "Generic Estimate", "Estimate" ) } From 958fa4820f4ae9947b92406fd565d1bb3864681f Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 15 Mar 2024 18:40:32 +0100 Subject: [PATCH 08/13] fix --- R/plot.performance_simres.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index 2ad34cceb..a0d2fe909 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -85,7 +85,9 @@ plot.see_performance_simres <- function(x, ggplot2::geom_qq_line( linewidth = size_line, colour = colors[1], - na.rm = TRUE + na.rm = TRUE, + distribution = stats::qunif, + dparams = dp ) ) y_lab <- "Sample Quantiles" From bfa9221802bb19e3526d19b3a437e2c50e70e2f8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 16 Mar 2024 10:26:49 +0100 Subject: [PATCH 09/13] add transform argument --- DESCRIPTION | 1 + R/plot.performance_simres.R | 18 ++++++++++++++++-- man/plot.see_performance_simres.Rd | 7 ++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d7c303456..e399380d5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -75,6 +75,7 @@ Imports: Suggests: brms, curl, + DHARMa, emmeans, factoextra, ggdist, diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index a0d2fe909..a282a1d67 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -3,6 +3,10 @@ #' The `plot()` method for the `performance::check_residuals()` resp. #' `performance::simulate_residuals()` function. #' +#' @param transform Function to transform the residuals. If `NULL` (default), +#' no transformation is applied and uniformly distributed residuals are expected. +#' See argument `quantileFuntion` in `?DHARMa:::residuals.DHARMa` for more details. +#' #' @inheritParams plot.see_check_normality #' @inheritParams plot.see_check_model #' @@ -10,7 +14,7 @@ #' #' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html). #' -#' @examplesIf require("performance") && require("glmmTMB") && require("qqplotr") +#' @examplesIf require("performance") && require("glmmTMB") && require("qqplotr") && require("DHARMa") #' data(Salamanders, package = "glmmTMB") #' model <- glmmTMB::glmmTMB( #' count ~ mined + spp + (1 | site), @@ -33,13 +37,23 @@ plot.see_performance_simres <- function(x, dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), detrend = FALSE, + 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") + + if (is.null(transform)) { + res <- stats::residuals(x) + } else { + res <- stats::residuals(x, quantileFunction = transform) + } + # base plot information gg_init <- ggplot2::ggplot( - data.frame(scaled_residuals = stats::residuals(x)), + data.frame(scaled_residuals = res), ggplot2::aes(sample = .data$scaled_residuals) ) diff --git a/man/plot.see_performance_simres.Rd b/man/plot.see_performance_simres.Rd index 9b9dd0f43..8ee287a57 100644 --- a/man/plot.see_performance_simres.Rd +++ b/man/plot.see_performance_simres.Rd @@ -12,6 +12,7 @@ dot_alpha = 0.8, colors = c("#3aaf85", "#1b6ca8"), detrend = FALSE, + transform = NULL, style = theme_lucid, ... ) @@ -33,6 +34,10 @@ hex-format) for points and line.} \item{detrend}{Logical that decides if Q-Q and P-P plots should be de-trended (also known as \emph{worm plots}).} +\item{transform}{Function to transform the residuals. If \code{NULL} (default), +no transformation is applied and uniformly distributed residuals are expected. +See argument \code{quantileFuntion} in \code{?DHARMa:::residuals.DHARMa} for more details.} + \item{style}{A ggplot2-theme.} \item{...}{Arguments passed to or from other methods.} @@ -45,7 +50,7 @@ The \code{plot()} method for the \code{performance::check_residuals()} resp. \code{performance::simulate_residuals()} function. } \examples{ -\dontshow{if (require("performance") && require("glmmTMB") && require("qqplotr")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (require("performance") && require("glmmTMB") && require("qqplotr") && require("DHARMa")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} data(Salamanders, package = "glmmTMB") model <- glmmTMB::glmmTMB( count ~ mined + spp + (1 | site), From 418a4aa5f1aee272b1df23b7297902201deffbb0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 16 Mar 2024 10:29:03 +0100 Subject: [PATCH 10/13] use latest performance --- DESCRIPTION | 1 + R/plot.performance_simres.R | 2 +- man/plot.see_performance_simres.Rd | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e399380d5..f3924493d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -119,3 +119,4 @@ Config/Needs/website: r-lib/pkgdown, easystats/easystatstemplate Config/rcmdcheck/ignore-inconsequential-notes: true +Remotes: easystats/performance@643 diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index a282a1d67..bb5b93a5a 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -14,7 +14,7 @@ #' #' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html). #' -#' @examplesIf require("performance") && require("glmmTMB") && require("qqplotr") && require("DHARMa") +#' @examplesIf insight::check_if_installed("performance", "0.10.9.7") && require("glmmTMB") && require("qqplotr") && require("DHARMa") #' data(Salamanders, package = "glmmTMB") #' model <- glmmTMB::glmmTMB( #' count ~ mined + spp + (1 | site), diff --git a/man/plot.see_performance_simres.Rd b/man/plot.see_performance_simres.Rd index 8ee287a57..6520442bc 100644 --- a/man/plot.see_performance_simres.Rd +++ b/man/plot.see_performance_simres.Rd @@ -50,7 +50,7 @@ The \code{plot()} method for the \code{performance::check_residuals()} resp. \code{performance::simulate_residuals()} function. } \examples{ -\dontshow{if (require("performance") && require("glmmTMB") && require("qqplotr") && require("DHARMa")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (insight::check_if_installed("performance", "0.10.9.7") && require("glmmTMB") && require("qqplotr") && require("DHARMa")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} data(Salamanders, package = "glmmTMB") model <- glmmTMB::glmmTMB( count ~ mined + spp + (1 | site), From ad574143ce9bb591a0702e5708879e761794ab23 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 16 Mar 2024 10:29:58 +0100 Subject: [PATCH 11/13] fix --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f3924493d..6d071261a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -119,4 +119,4 @@ Config/Needs/website: r-lib/pkgdown, easystats/easystatstemplate Config/rcmdcheck/ignore-inconsequential-notes: true -Remotes: easystats/performance@643 +Remotes: easystats/performance#643 From b5461179cbf5b481ba51d8935f67788256d2995d Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 16 Mar 2024 11:36:44 +0100 Subject: [PATCH 12/13] fix --- R/plot.performance_simres.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index bb5b93a5a..afb16ce39 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -45,6 +45,11 @@ plot.see_performance_simres <- function(x, # need DHARMa to be installed insight::check_if_installed("DHARMa") + # extract data, if from check_residuals + if (inherits(x, "see_check_residuals")) { + x <- attributes(x)$data + } + if (is.null(transform)) { res <- stats::residuals(x) } else { From cade33eebd0d190e120525e2b7f74d91a008d025 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 16 Mar 2024 12:35:11 +0100 Subject: [PATCH 13/13] fix if qqplotr is missing --- R/plot.performance_simres.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/plot.performance_simres.R b/R/plot.performance_simres.R index afb16ce39..24ac0853f 100644 --- a/R/plot.performance_simres.R +++ b/R/plot.performance_simres.R @@ -95,9 +95,11 @@ plot.see_performance_simres <- function(x, } else { insight::format_alert("For confidence bands, please install `qqplotr`.") qq_stuff <- list( - ggplot2::geom_point( + ggplot2::geom_qq( shape = 16, stroke = 0, + distribution = stats::qunif, + dparams = dp, size = size_point, colour = colors[2] ),