Skip to content

Commit bd3e77a

Browse files
authored
check_autocorrelation() gets methods for DHARMa objects and objects from simulate_residuals(). (#864)
1 parent 3aa80c7 commit bd3e77a

File tree

6 files changed

+99
-5
lines changed

6 files changed

+99
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: performance
33
Title: Assessment of Regression Models Performance
4-
Version: 0.15.2.1
4+
Version: 0.15.2.2
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ S3method(as.data.frame,r2_nakagawa)
2828
S3method(as.double,check_outliers)
2929
S3method(as.double,item_omega)
3030
S3method(as.double,performance_roc)
31+
S3method(check_autocorrelation,DHARMa)
3132
S3method(check_autocorrelation,default)
33+
S3method(check_autocorrelation,performance_simres)
3234
S3method(check_collinearity,BFBayesFactor)
3335
S3method(check_collinearity,MixMod)
3436
S3method(check_collinearity,afex_aov)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Changes
44

5+
* `check_autocorrelation()` gets methods for `DHARMa` objects and objects from
6+
`simulate_residuals()`.
7+
58
* Improved documentation for printing-methods.
69

710
# performance 0.15.2

R/check_autocorrelation.R

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
#' @description Check model for independence of residuals, i.e. for autocorrelation
55
#' of error terms.
66
#'
7-
#' @param x A model object.
7+
#' @param x A model object, or an object returned by `simulate_residuals()`.
88
#' @param nsim Number of simulations for the Durbin-Watson-Test.
9-
#' @param ... Currently not used.
9+
#' @param time A vector with time values to specify the temporal order of the data.
10+
#' Only used if `x` is an object returned by `simulate_residuals()` or by `DHARMa`.
11+
#' @param ... Currently not used for models. For simulated residuals, arguments are
12+
#' passed to `DHARMa::testTemporalAutocorrelation()`.
1013
#'
1114
#' @return Invisibly returns the p-value of the test statistics. A p-value < 0.05
1215
#' indicates autocorrelated residuals.
@@ -48,6 +51,32 @@ check_autocorrelation.default <- function(x, nsim = 1000, ...) {
4851
p.val
4952
}
5053

54+
#' @rdname check_autocorrelation
55+
#' @export
56+
check_autocorrelation.performance_simres <- function(x, time = NULL, ...) {
57+
insight::check_if_installed("DHARMa")
58+
59+
if (is.null(time)) {
60+
insight::format_warning(
61+
"Data are assumed to be ordered by time. If this is not the case, please provide a `time` argument."
62+
)
63+
time <- seq_along(x$scaledResiduals)
64+
}
65+
66+
# Use DHARMa's temporal autocorrelation test
67+
# This requires the residuals to be ordered by time
68+
# DHARMa::testTemporalAutocorrelation expects a DHARMa object
69+
result <- DHARMa::testTemporalAutocorrelation(x, time = time, plot = FALSE, ...)
70+
71+
# Extract p-value from the result
72+
p.val <- result$p.value
73+
74+
class(p.val) <- c("check_autocorrelation", "see_check_autocorrelation", class(p.val))
75+
p.val
76+
}
77+
78+
#' @export
79+
check_autocorrelation.DHARMa <- check_autocorrelation.performance_simres
5180

5281
# methods ------------------------------
5382

man/check_autocorrelation.Rd

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
test_that("check_autocorrelation works with simulated residuals", {
2+
skip_if_not_installed("DHARMa")
3+
skip_if_not_installed("glmmTMB")
4+
skip_if_not(getRversion() >= "4.0.0")
5+
6+
data(Salamanders, package = "glmmTMB")
7+
8+
# Test with a simple Poisson GLM
9+
m <- glm(count ~ spp + mined, family = poisson, data = Salamanders)
10+
11+
# Simulate residuals
12+
set.seed(123)
13+
simres <- simulate_residuals(m)
14+
15+
# Check autocorrelation
16+
set.seed(123)
17+
expect_warning(
18+
{
19+
out <- check_autocorrelation(simres)
20+
},
21+
regex = "Data are assumed"
22+
)
23+
24+
# Should return a p-value
25+
expect_type(out, "double")
26+
expect_s3_class(out, "check_autocorrelation")
27+
28+
expect_equal(as.vector(out), 0.2211415, tolerance = 1e-3, ignore_attr = TRUE)
29+
})
30+
31+
32+
test_that("check_autocorrelation.DHARMa works", {
33+
skip_if_not_installed("DHARMa")
34+
35+
# Test that the DHARMa method works
36+
data(mtcars)
37+
m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
38+
39+
set.seed(123)
40+
simres <- DHARMa::simulateResiduals(m, plot = FALSE)
41+
42+
expect_warning(check_autocorrelation(simres), regex = "Data are assumed")
43+
set.seed(123)
44+
expect_silent({
45+
out <- check_autocorrelation(simres, time = seq_along(simres$scaledResiduals))
46+
})
47+
48+
# Should return a p-value
49+
expect_type(out, "double")
50+
expect_s3_class(out, "check_autocorrelation")
51+
52+
expect_equal(as.vector(out), 0.4163168, tolerance = 1e-3, ignore_attr = TRUE)
53+
})

0 commit comments

Comments
 (0)