Skip to content

Commit bce60d7

Browse files
Copilotstrengejacke
andcommitted
Add tests for autocorrelation with simulated residuals and update overdispersion test
Co-authored-by: strengejacke <26301769+strengejacke@users.noreply.github.com>
1 parent cdd951b commit bce60d7

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
out <- check_autocorrelation(simres)
18+
19+
# Should return a p-value
20+
expect_type(out, "double")
21+
expect_s3_class(out, "check_autocorrelation")
22+
23+
# P-value should be between 0 and 1
24+
expect_true(out >= 0 && out <= 1)
25+
})
26+
27+
28+
test_that("check_autocorrelation.DHARMa works", {
29+
skip_if_not_installed("DHARMa")
30+
31+
# Test that the DHARMa method works
32+
data(mtcars)
33+
m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
34+
35+
set.seed(123)
36+
simres <- DHARMa::simulateResiduals(m, plot = FALSE)
37+
38+
set.seed(123)
39+
out <- check_autocorrelation(simres)
40+
41+
# Should return a p-value
42+
expect_type(out, "double")
43+
expect_s3_class(out, "check_autocorrelation")
44+
45+
# P-value should be between 0 and 1
46+
expect_true(out >= 0 && out <= 1)
47+
})

tests/testthat/test-check_overdispersion.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ test_that("check_overdispersion, glmmTMB-poisson", {
5050

5151
test_that("check_overdispersion, glmmTMB-poisson mixed", {
5252
skip_if_not_installed("glmmTMB")
53+
skip_if_not_installed("DHARMa")
5354
skip_if_not(getRversion() >= "4.0.0")
5455
data(Salamanders, package = "glmmTMB")
5556

@@ -58,19 +59,20 @@ test_that("check_overdispersion, glmmTMB-poisson mixed", {
5859
family = poisson,
5960
data = Salamanders
6061
)
62+
# Poisson mixed models now use simulated residuals (see #595, #643)
63+
set.seed(123)
64+
out <- check_overdispersion(m2)
6165
expect_equal(
62-
check_overdispersion(m2),
66+
out,
6367
structure(
6468
list(
65-
chisq_statistic = 1475.87512547128,
66-
dispersion_ratio = 2.32421279601777,
67-
residual_df = 635L,
68-
p_value = 8.41489530177729e-69
69+
dispersion_ratio = 3.30,
70+
p_value = 0
6971
),
70-
class = c("check_overdisp", "see_check_overdisp"),
71-
object_name = "m2"
72+
class = c("check_overdisp", "see_check_overdisp")
7273
),
73-
tolerance = 1e-3
74+
tolerance = 0.1,
75+
ignore_attr = TRUE
7476
)
7577
})
7678

0 commit comments

Comments
 (0)