Calls uniformity, dispersion and outliers tests.
Arguments
- simulationOutput
an object of class DHARMa, either created via simulateResiduals for supported models or by createDHARMa for simulations created outside DHARMa, or a supported model. Providing a supported model directly is discouraged, because simulation settings cannot be changed in this case.
- plot
if TRUE, plot functions of the tests are called.
Details
This function is a wrapper for the various test functions implemented in DHARMa. Currently, this function calls the functions testUniformity, testDispersion, and testOutliers. All other tests (see list below) have to be called by hand.
Examples
testData = createData(sampleSize = 100, overdispersion = 0.5, randomEffectVariance = 0)
fittedModel <- glm(observedResponse ~ Environment1 , family = "poisson", data = testData)
simulationOutput <- simulateResiduals(fittedModel = fittedModel)
# the plot function shows 2 plots and runs 4 tests
# i) KS test i) Dispersion test iii) Outlier test iv) quantile test
plot(simulationOutput, quantreg = TRUE)
# testResiduals tests distribution, dispersion and outliers
testResiduals(simulationOutput)
#> $uniformity
#>
#> Asymptotic one-sample Kolmogorov-Smirnov test
#>
#> data: simulationOutput$scaledResiduals
#> D = 0.10441, p-value = 0.2257
#> alternative hypothesis: two-sided
#>
#>
#> $dispersion
#>
#> DHARMa nonparametric dispersion test via sd of residuals fitted vs.
#> simulated
#>
#> data: simulationOutput
#> dispersion = 1.5293, p-value = 0.016
#> alternative hypothesis: two.sided
#>
#>
#> $outliers
#>
#> DHARMa bootstrapped outlier test
#>
#> data: simulationOutput
#> outliers at both margin(s) = 1, observations = 100, p-value = 0.56
#> alternative hypothesis: two.sided
#> percent confidence interval:
#> 0.00 0.02
#> sample estimates:
#> outlier frequency (expected: 0.0034 )
#> 0.01
#>
#>
####### Individual tests #######
# KS test for correct distribution of residuals
testUniformity(simulationOutput)
#>
#> Asymptotic one-sample Kolmogorov-Smirnov test
#>
#> data: simulationOutput$scaledResiduals
#> D = 0.10441, p-value = 0.2257
#> alternative hypothesis: two-sided
#>
# KS test for correct distribution within and between groups
# group specified as formula (recommended)
testCategorical(simulationOutput, ~group)
#> $uniformity
#> $uniformity$details
#> catPred: 1
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.30897, p-value = 0.2407
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 2
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.2276, p-value = 0.6015
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 3
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.1561, p-value = 0.9377
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 4
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.38333, p-value = 0.07859
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 5
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.29332, p-value = 0.2944
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 6
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.14441, p-value = 0.9663
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 7
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.36777, p-value = 0.1014
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 8
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.41214, p-value = 0.04744
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 9
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.35843, p-value = 0.1176
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 10
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.19795, p-value = 0.7592
#> alternative hypothesis: two-sided
#>
#>
#> $uniformity$p.value
#> [1] 0.24068344 0.60150293 0.93770609 0.07858603 0.29435934 0.96632143
#> [7] 0.10144218 0.04743551 0.11761657 0.75924076
#>
#> $uniformity$p.value.cor
#> [1] 1.0000000 1.0000000 1.0000000 0.7072742 1.0000000 1.0000000 0.8115374
#> [8] 0.4743551 0.8233160 1.0000000
#>
#>
#> $homogeneity
#> Levene's Test for Homogeneity of Variance (center = median)
#> Df F value Pr(>F)
#> group 9 0.6732 0.7311
#> 90
#>
# group specified as variable in your environment
testCategorical(simulationOutput, testData$group)
#> $uniformity
#> $uniformity$details
#> catPred: 1
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.30897, p-value = 0.2407
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 2
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.2276, p-value = 0.6015
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 3
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.1561, p-value = 0.9377
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 4
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.38333, p-value = 0.07859
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 5
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.29332, p-value = 0.2944
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 6
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.14441, p-value = 0.9663
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 7
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.36777, p-value = 0.1014
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 8
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.41214, p-value = 0.04744
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 9
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.35843, p-value = 0.1176
#> alternative hypothesis: two-sided
#>
#> ------------------------------------------------------------
#> catPred: 10
#>
#> Exact one-sample Kolmogorov-Smirnov test
#>
#> data: dd[x, ]
#> D = 0.19795, p-value = 0.7592
#> alternative hypothesis: two-sided
#>
#>
#> $uniformity$p.value
#> [1] 0.24068344 0.60150293 0.93770609 0.07858603 0.29435934 0.96632143
#> [7] 0.10144218 0.04743551 0.11761657 0.75924076
#>
#> $uniformity$p.value.cor
#> [1] 1.0000000 1.0000000 1.0000000 0.7072742 1.0000000 1.0000000 0.8115374
#> [8] 0.4743551 0.8233160 1.0000000
#>
#>
#> $homogeneity
#> Levene's Test for Homogeneity of Variance (center = median)
#> Df F value Pr(>F)
#> group 9 0.6732 0.7311
#> 90
#>
# Dispersion test - for details see ?testDispersion
testDispersion(simulationOutput) # tests under and overdispersion
#>
#> DHARMa nonparametric dispersion test via sd of residuals fitted vs.
#> simulated
#>
#> data: simulationOutput
#> dispersion = 1.5293, p-value = 0.016
#> alternative hypothesis: two.sided
#>
# Outlier test (number of observations outside simulation envelope)
# Use type = "boostrap" for exact values, see ?testOutliers
testOutliers(simulationOutput, type = "binomial")
#>
#> DHARMa outlier test based on exact binomial test with approximate
#> expectations
#>
#> data: simulationOutput
#> outliers at both margin(s) = 2, observations = 100, p-value = 0.1898
#> alternative hypothesis: true probability of success is not equal to 0.007968127
#> 95 percent confidence interval:
#> 0.002431337 0.070383932
#> sample estimates:
#> frequency of outliers (expected: 0.00796812749003984 )
#> 0.02
#>
# testing zero inflation
testZeroInflation(simulationOutput)
#>
#> DHARMa zero-inflation test via comparison to expected zeros with
#> simulation under H0 = fitted model
#>
#> data: simulationOutput
#> ratioObsSim = 1.1645, p-value = 0.208
#> alternative hypothesis: two.sided
#>
# testing generic summaries
countOnes <- function(x) sum(x == 1) # testing for number of 1s
testGeneric(simulationOutput, summary = countOnes) # 1-inflation
#>
#> DHARMa generic simulation test
#>
#> data: simulationOutput
#> ratioObsSim = 0.87464, p-value = 0.528
#> alternative hypothesis: two.sided
#>
testGeneric(simulationOutput, summary = countOnes, alternative = "less") # 1-deficit
#>
#> DHARMa generic simulation test
#>
#> data: simulationOutput
#> ratioObsSim = 0.87464, p-value = 0.264
#> alternative hypothesis: less
#>
means <- function(x) mean(x) # testing if mean prediction fits
testGeneric(simulationOutput, summary = means)
#>
#> DHARMa generic simulation test
#>
#> data: simulationOutput
#> ratioObsSim = 1.0023, p-value = 1
#> alternative hypothesis: two.sided
#>
spread <- function(x) sd(x) # testing if mean sd fits
testGeneric(simulationOutput, summary = spread)
#>
#> DHARMa generic simulation test
#>
#> data: simulationOutput
#> ratioObsSim = 1.1421, p-value = 0.12
#> alternative hypothesis: two.sided
#>