Skip to content

Commit d3cc977

Browse files
committed
make residuals compatible, add tests
1 parent 1e03a33 commit d3cc977

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

R/check_model_diagnostics.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
d <- data.frame(Predicted = predicted)
302302

303303
# residuals based on simulated residuals - but we want normally distributed residuals
304-
d$Residuals <- stats::residuals(simres, quantileFunction = stats::qnorm, ...)
304+
d$Residuals <- stats::residuals(simres, quantile_function = stats::qnorm, ...)
305305
d$Res2 <- d$Residuals^2
306306
d$StdRes <- insight::get_residuals(model, type = "pearson")
307307

R/simulate_residuals.R

+13
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,24 @@ plot.performance_simres <- function(x, ...) {
110110
#' @rdname simulate_residuals
111111
#' @export
112112
residuals.performance_simres <- function(object, quantile_function = NULL, outlier_values = NULL, ...) {
113+
# check for DHARMa argument names
114+
dots <- list(...)
115+
if (!is.null(dots$quantileFunction)) {
116+
quantile_function <- dots$quantileFunction
117+
}
118+
if (!is.null(dots$outlierValues)) {
119+
outlier_values <- dots$outlierValues
120+
}
121+
113122
if (is.null(quantile_function)) {
114123
res <- object$scaledResiduals
115124
} else {
116125
res <- quantile_function(object$scaledResiduals)
117126
if (!is.null(outlier_values)) {
127+
# check for correct length of outlier_values
128+
if (length(outlier_values) != 2) {
129+
insight::format_error("`outlier_values` must be a vector of length 2.")
130+
}
118131
res[res == -Inf] <- outlier_values[1]
119132
res[res == Inf] <- outlier_values[2]
120133
}

tests/testthat/test-check_residuals.R

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
test_that("check_residuals", {
1+
test_that("check_residuals and simulate_residuals", {
22
skip_on_cran()
33
skip_if_not_installed("DHARMa")
44
set.seed(123)
@@ -16,7 +16,46 @@ test_that("check_residuals", {
1616
" particular models."
1717
)
1818
)
19+
# check raw residuals
20+
expect_equal(
21+
head(residuals(res)),
22+
c(0.55349, 0.44012, 0.39826, 0.9825, 0.90753, 0.05809),
23+
tolerance = 1e-4,
24+
ignore_attr = TRUE
25+
)
26+
expect_equal(
27+
head(residuals(res, quantile_function = stats::qnorm)),
28+
c(0.13448, -0.15068, -0.25785, 2.10826, 1.3257, -1.57097),
29+
tolerance = 1e-4,
30+
ignore_attr = TRUE
31+
)
32+
# compare to DHARMa
33+
res_d <- DHARMa::simulateResiduals(m, n = 250, plot = FALSE)
34+
expect_equal(
35+
head(residuals(res)),
36+
head(residuals(res_d)),
37+
tolerance = 1e-4,
38+
ignore_attr = TRUE
39+
)
40+
expect_equal(
41+
head(residuals(res, quantile_function = stats::qnorm)),
42+
head(residuals(res_d, quantileFunction = stats::qnorm)),
43+
tolerance = 1e-4,
44+
ignore_attr = TRUE
45+
)
46+
# DHARMa args work in residuals.permormance_simres
47+
expect_equal(
48+
residuals(res, quantileFunction = stats::qnorm, outlierValues = c(-3, 3)),
49+
residuals(res_d, quantileFunction = stats::qnorm, outlierValues = c(-3, 3)),
50+
tolerance = 1e-4,
51+
ignore_attr = TRUE
52+
)
53+
# outlier_values works
54+
expect_identical(sum(is.infinite(residuals(res, quantile_function = stats::qnorm))), 3L)
55+
expect_identical(sum(is.infinite(residuals(res, quantile_function = stats::qnorm, outlier_values = c(-100, 100)))), 0L)
56+
expect_error(residuals(res, quantile_function = stats::qnorm, outlier_values = 1:3), regex = "`outlier_values` must be")
1957

58+
# check_residuals
2059
out <- check_residuals(res)
2160
expect_equal(out, 0.01884602, ignore_attr = TRUE, tolerance = 1e-4)
2261
expect_identical(

0 commit comments

Comments
 (0)