From d515869a0b517f44f6bf3600ba28d2a1a9d9f10a Mon Sep 17 00:00:00 2001 From: Michael McCarthy <51542091+mccarthy-m-g@users.noreply.github.com> Date: Fri, 3 Nov 2023 13:58:35 -0700 Subject: [PATCH] add print method for `check_residuals()` --- NAMESPACE | 1 + R/check_residuals.R | 45 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 33c6de753..521d979c0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -285,6 +285,7 @@ S3method(print,check_outliers) S3method(print,check_outliers_metafor) S3method(print,check_outliers_metagen) S3method(print,check_overdisp) +S3method(print,check_residuals) S3method(print,check_sphericity) S3method(print,check_symmetry) S3method(print,check_zi) diff --git a/R/check_residuals.R b/R/check_residuals.R index c0968fb28..86d918777 100644 --- a/R/check_residuals.R +++ b/R/check_residuals.R @@ -30,7 +30,7 @@ check_residuals <- function(x, ...) { #' @export check_residuals.default <- function(x, ...) { - insight::format_error("`check_residuals()` only works with objects returned by `simulate_residuals()` by `DHARMa::simulateResiduals()`.") # nolint + insight::format_error("`check_residuals()` only works with objects returned by `simulate_residuals()` or `DHARMa::simulateResiduals()`.") # nolint } #' @rdname check_residuals @@ -39,12 +39,22 @@ check_residuals.performance_simres <- function(x, alternative = c("two.sided", "less", "greater"), ...) { alternative <- match.arg(alternative) - stats::ks.test( - stats::residuals(simulated_residuals), - "punif", - alternative = alternative, - ... + ts <- suppressWarnings( + stats::ks.test( + stats::residuals(simulated_residuals), + "punif", + alternative = alternative, + ... + ) ) + + p.val <- ts$p.value + + attr(p.val, "data") <- x + attr(p.val, "object_name") <- insight::safe_deparse_symbol(substitute(x)) + class(p.val) <- unique(c("check_residuals", "see_check_residuals", class(p.val))) + + p.val } #' @export @@ -53,4 +63,25 @@ check_residuals.DHARMa <- check_residuals.performance_simres # methods ------------------------------ -# TODO: Add print method +#' @export +print.check_residuals <- function(x, ...) { + pstring <- insight::format_p(x) + + if (x < 0.05) { + insight::print_color( + sprintf( + "Warning: Non-uniformity of simulated residuals detected (%s).\n", pstring + ), + "red" + ) + } else { + insight::print_color( + sprintf( + "OK: Simulated residuals appear as uniformly distributed (%s).\n", pstring + ), + "green" + ) + } + + invisible(x) +}