Skip to content

Commit d515869

Browse files
committed
add print method for check_residuals()
1 parent e1979a4 commit d515869

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ S3method(print,check_outliers)
285285
S3method(print,check_outliers_metafor)
286286
S3method(print,check_outliers_metagen)
287287
S3method(print,check_overdisp)
288+
S3method(print,check_residuals)
288289
S3method(print,check_sphericity)
289290
S3method(print,check_symmetry)
290291
S3method(print,check_zi)

R/check_residuals.R

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ check_residuals <- function(x, ...) {
3030

3131
#' @export
3232
check_residuals.default <- function(x, ...) {
33-
insight::format_error("`check_residuals()` only works with objects returned by `simulate_residuals()` by `DHARMa::simulateResiduals()`.") # nolint
33+
insight::format_error("`check_residuals()` only works with objects returned by `simulate_residuals()` or `DHARMa::simulateResiduals()`.") # nolint
3434
}
3535

3636
#' @rdname check_residuals
@@ -39,12 +39,22 @@ check_residuals.performance_simres <- function(x,
3939
alternative = c("two.sided", "less", "greater"),
4040
...) {
4141
alternative <- match.arg(alternative)
42-
stats::ks.test(
43-
stats::residuals(simulated_residuals),
44-
"punif",
45-
alternative = alternative,
46-
...
42+
ts <- suppressWarnings(
43+
stats::ks.test(
44+
stats::residuals(simulated_residuals),
45+
"punif",
46+
alternative = alternative,
47+
...
48+
)
4749
)
50+
51+
p.val <- ts$p.value
52+
53+
attr(p.val, "data") <- x
54+
attr(p.val, "object_name") <- insight::safe_deparse_symbol(substitute(x))
55+
class(p.val) <- unique(c("check_residuals", "see_check_residuals", class(p.val)))
56+
57+
p.val
4858
}
4959

5060
#' @export
@@ -53,4 +63,25 @@ check_residuals.DHARMa <- check_residuals.performance_simres
5363

5464
# methods ------------------------------
5565

56-
# TODO: Add print method
66+
#' @export
67+
print.check_residuals <- function(x, ...) {
68+
pstring <- insight::format_p(x)
69+
70+
if (x < 0.05) {
71+
insight::print_color(
72+
sprintf(
73+
"Warning: Non-uniformity of simulated residuals detected (%s).\n", pstring
74+
),
75+
"red"
76+
)
77+
} else {
78+
insight::print_color(
79+
sprintf(
80+
"OK: Simulated residuals appear as uniformly distributed (%s).\n", pstring
81+
),
82+
"green"
83+
)
84+
}
85+
86+
invisible(x)
87+
}

0 commit comments

Comments
 (0)