Skip to content

Commit 1a05873

Browse files
committed
created specific tests for models without Pearson
given that the getPearsonResiduals.brms function will always give an error, I created a runEverything function excluding the Pearson residuals dispersion tests in testModelTypes. Used this for brms, phylolm/phyloglm, GLMMadaptive. Otherwise the tests wouldn't work
1 parent 4e6e9e6 commit 1a05873

2 files changed

Lines changed: 78 additions & 11 deletions

File tree

DHARMa/man/DHARMa-package.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DHARMa/tests/testthat/testModelTypes.R

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,73 @@ runEverything = function(fittedModel, testData, DHARMaData = T, phy = NULL,
9292

9393

9494

95+
}
96+
97+
runEverythingExcPearson = function(fittedModel, testData, DHARMaData = T, phy = NULL,
98+
expectOverdispersion = F, doRefit = TRUE){
99+
100+
t = getObservedResponse(fittedModel)
101+
expect_true(is.vector(t))
102+
expect_true(is.numeric(t))
103+
104+
x = getSimulations(fittedModel, 1)
105+
expect_true(is.matrix(x))
106+
expect_true(ncol(x) == 1)
107+
108+
x = getSimulations(fittedModel, 2)
109+
expect_true(is.numeric(x))
110+
expect_true(is.matrix(x))
111+
expect_true(ncol(x) == 2)
112+
113+
x = getSimulations(fittedModel, 1, type = "refit")
114+
expect_true(is.data.frame(x))
115+
116+
x = getSimulations(fittedModel, 2, type = "refit")
117+
expect_true(is.data.frame(x))
118+
119+
fittedModel2 = getRefit(fittedModel,x[[1]])
120+
# expect_false(any(getFixedEffects(fittedModel) -
121+
# getFixedEffects(fittedModel2) > 0.5)) # doesn't work for some models
122+
123+
simulationOutput <- simulateResiduals(fittedModel = fittedModel, n = 200)
124+
125+
checkOutput(simulationOutput)
126+
127+
if(doPlots) plot(simulationOutput, quantreg = F)
128+
129+
expect_gt(testOutliers(simulationOutput, plot = doPlots)$p.value, 0.001)
130+
expect_gt(testDispersion(simulationOutput, plot = doPlots)$p.value, 0.001)
131+
expect_gt(testUniformity(simulationOutput = simulationOutput,
132+
plot = doPlots)$p.value, 0.001)
133+
expect_gt(testZeroInflation(simulationOutput = simulationOutput,
134+
plot = doPlots)$p.value, 0.001)
135+
expect_gt(testTemporalAutocorrelation(simulationOutput = simulationOutput,
136+
time = testData$time,
137+
plot = doPlots)$p.value, 0.001)
138+
expect_gt(testSpatialAutocorrelation(simulationOutput = simulationOutput,
139+
x = testData$x, y = testData$y,
140+
plot = F)$p.value, 0.001)
141+
142+
simulationOutput <- recalculateResiduals(simulationOutput, group = testData$group)
143+
expect_gt(testDispersion(simulationOutput, plot = doPlots)$p.value, 0.001)
144+
145+
if(doRefit == TRUE) {
146+
simulationOutput2 <- simulateResiduals(fittedModel = fittedModel,
147+
refit = T, n = 100)
148+
checkOutput(simulationOutput2)
149+
if(doPlots) plot(simulationOutput2, quantreg = F)
150+
151+
# note that the pearson test is biased, therefore have to test greater
152+
#expect_gt(testDispersion(simulationOutput2, plot = doPlots, alternative = "greater")$p.value, 0.001)
153+
#x = testDispersion(simulationOutput2, plot = doPlots)
154+
155+
simulationOutput3 <- recalculateResiduals(simulationOutput2, group = testData$group)
156+
#expect_gt(testDispersion(simulationOutput3, plot = doPlots, alternative = "greater")$p.value, 0.001)
157+
#x = testDispersion(simulationOutput3, plot = doPlots)
158+
}
159+
160+
161+
95162
}
96163

97164

@@ -470,14 +537,14 @@ test_that("GLMMadaptive works",
470537
random = ~ 1 | group,
471538
data = testData$binomial_10,
472539
family = binomial())
473-
runEverything(fittedModel, testData$binomial_10)
540+
runEverythingExcPearson(fittedModel, testData$binomial_10)
474541

475542
fittedModel <- GLMMadaptive::mixed_model(fixed = observedResponse ~
476543
Environment1,
477544
random = ~ 1 | group,
478545
data = testData$binomial_yn,
479546
family = binomial())
480-
runEverything(fittedModel, testData$binomial_yn)
547+
runEverythingExcPearson(fittedModel, testData$binomial_yn)
481548

482549
fittedModel <- GLMMadaptive::mixed_model(fixed =
483550
cbind(observedResponse1,
@@ -499,7 +566,7 @@ test_that("GLMMadaptive works",
499566
random = ~ 1 | group,
500567
data = testData$poisson1,
501568
family = poisson())
502-
runEverything(fittedModel, testData$poisson1)
569+
runEverythingExcPearson(fittedModel, testData$poisson1)
503570

504571
# GLMMadaptive requires weights according to groups
505572
weights = rep(c(1,1.1), each = 5)
@@ -519,29 +586,29 @@ test_that("GLMMadaptive works",
519586
test_that("brms works",
520587
{
521588
fittedModel <- brms::brm(observedResponse ~ Environment1 + (1|group), data = testData$lmm)
522-
runEverything(fittedModel, testData$lmm, doRefit = FALSE)
589+
runEverythingExcPearson(fittedModel, testData$lmm, doRefit = FALSE)
523590

524591
fittedModel <- brms::brm(observedResponse|trials(1) ~ Environment1 +
525592
(1|group), family = "binomial",
526593
data = testData$binomial_10)
527-
runEverything(fittedModel, testData$binomial_10, doRefit = FALSE)
594+
runEverythingExcPearson(fittedModel, testData$binomial_10, doRefit = FALSE)
528595

529596
fittedModel <- brms::brm(observedResponse ~ Environment1 +
530597
(1|group), family = "bernoulli",
531598
data = testData$binomial_yn)
532-
runEverything(fittedModel, testData$binomial_yn, doRefit = FALSE)
599+
runEverythingExcPearson(fittedModel, testData$binomial_yn, doRefit = FALSE)
533600

534601
fittedModel <- brms::brm(observedResponse1|trials(observedResponse1 + observedResponse0)~
535602
Environment1 + (1|group),
536603
family = "binomial",
537604
data = testData$binomial_nk_matrix)
538-
runEverything(fittedModel, testData$binomial_nk_matrix, doRefit = FALSE)
605+
runEverythingExcPearson(fittedModel, testData$binomial_nk_matrix, doRefit = FALSE)
539606

540607
fittedModel <- brms::brm(observedResponse~
541608
Environment1 + (1|group),
542609
family = "poisson",
543610
data = testData$poisson1)
544-
runEverything(fittedModel, testData$poisson1, doRefit = FALSE)
611+
runEverythingExcPearson(fittedModel, testData$poisson1, doRefit = FALSE)
545612
}
546613
)
547614

@@ -574,7 +641,7 @@ test_that("phylolm works",
574641
fittedModel = phylolm::phylolm(trait ~ predictor, data = testData,
575642
phy = tre1, model = "lambda")
576643

577-
runEverything(fittedModel, testData = testData, phy = tre1)
644+
runEverythingExcPearson(fittedModel, testData = testData, phy = tre1)
578645
})
579646

580647
test_that("phyloglm works",
@@ -591,7 +658,7 @@ test_that("phyloglm works",
591658
fittedModel = phylolm::phyloglm(trait ~ predictor,
592659
phy = tre, data = testData)
593660

594-
runEverything(fittedModel, testData = testData, phy = tre)
661+
runEverythingExcPearson(fittedModel, testData = testData, phy = tre)
595662
})
596663

597664

0 commit comments

Comments
 (0)