Skip to content

Commit

Permalink
add print method for check_residuals()
Browse files Browse the repository at this point in the history
  • Loading branch information
mccarthy-m-g committed Nov 3, 2023
1 parent e1979a4 commit d515869
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 38 additions & 7 deletions R/check_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit d515869

Please sign in to comment.