Skip to content

Commit

Permalink
bubfixes test / moved to 0.0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
florianhartig committed Aug 12, 2016
1 parent b999a6e commit 45c0503
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions DHARMa/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: DHARMa
Title: Residual Diagnostics for HierArchical (Multi-level / Mixed) Regression Models
Version: 0.0.2.0
Date: 11.8.2016
Version: 0.0.2.1
Date: 12.8.2016
Authors@R: c(person("Florian", "Hartig", email = "florian.hartig@biologie.uni-regensburg.de", role = c("aut", "cre")))
Author: Florian Hartig [aut, cre],
Maintainer: Florian Hartig <florian.hartig@biologie.uni-regensburg.de>
Expand Down
4 changes: 2 additions & 2 deletions DHARMa/R/testsResiduals.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @seealso \code{\link{testUniformDistribution}}, \code{\link{testZeroInflation}}, \code{\link{testTemporalAutocorrelation}}, \code{\link{testSpatialAutocorrelation}}
testSimulatedResiduals <- function(simulationOutput){

out <- testUniformDistribution(simulationOutput$scaledResiduals)
out$pValueUnivariate <- testUniformDistribution(simulationOutput)

return(out)
}
Expand All @@ -20,7 +20,7 @@ testSimulatedResiduals <- function(simulationOutput){
#' @export
testUniformDistribution <- function(simulationOutput, print = T){

out <- suppressWarnings(ks.test(simulationOutput, 'punif'))
out <- suppressWarnings(ks.test(simulationOutput$scaledResiduals, 'punif'))
if(print == T) out
return(out)
}
Expand Down
6 changes: 3 additions & 3 deletions DHARMa/R/zz-testTypeI.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Simulated p-values for internal testing
#' @return list of various named objects. Within each, order is dispersion_glmer, overdisp_fun, DHARMa
simulatePvaluesDispersion <- function(overdispersion, n = 20, alpha = 0.05){
simulatePvaluesDispersion <- function(overdispersion = 0, n = 20, alpha = 0.05){

out = matrix(nrow = n, ncol = 3)

Expand All @@ -16,8 +16,8 @@ simulatePvaluesDispersion <- function(overdispersion, n = 20, alpha = 0.05){


simulationOutput <- simulateResiduals(fittedModel = fittedModel)
x = testSimulatedResiduals(simulationOutput)
out[i,3] = x$pValueUnivariate$p.value
x = testUniformDistribution(simulationOutput)
out[i,3] = x$p.value
}


Expand Down
2 changes: 1 addition & 1 deletion DHARMa/man/simulatePvaluesDispersion.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions DHARMa/vignettes/DHARMa.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set.seed(123)


```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=7, fig.height=4.5, warning=FALSE, message=FALSE, cache = T)
knitr::opts_chunk$set(fig.width=7, fig.height=4.5, warning=FALSE, message=FALSE, cache = F)
```

***Summary: The DHARMa package creates readily interpretable residuals for generalized linear (mixed) models that are standardized to values between 0 and 1. This is achieved by a simulation-based approach, similar to the Bayesian p-value or the parametric bootstrap: 1) simulate new data from the fitted model 2) from this simulated data, calculate the cummulative density function 3) residual is the value of the empirical density function at the value of the observed data.* **
Expand Down Expand Up @@ -121,13 +121,13 @@ The DHARMa package provides a number of additional tests, which, however, should
You can run a hypothesis test on the residuals, which runs a KS test on the uniformity of the simulated residuals

```{r}
testSimulatedResiduals(simulationOutput = simulationOutput)
testUniformDistribution(simulationOutput = simulationOutput)
```

Note, however, that simulations show that this test is less powerfull than parametric tests on the likelihood that are currently run by many people. On the other hand, the parametric tests

```{r}
dispersionTest(fittedModel, type = 1)
parametricDispersionTest(fittedModel, type = 1)
```

### Test for zero-inflation
Expand Down Expand Up @@ -204,7 +204,7 @@ summary(fittedModel)
simulationOutput <- simulateResiduals(fittedModel = fittedModel)
plotSimulatedResiduals(simulationOutput = simulationOutput)
testSimulatedResiduals(simulationOutput = simulationOutput)
testUniformDistribution(simulationOutput = simulationOutput)
```

Here, we get too many residuals around 0.5, which means that we are not getting as many residuals as we would expect in the tail of the distribution that is epected with the fitted model.
Expand Down Expand Up @@ -251,7 +251,7 @@ fittedModel <- glmer(observedResponse ~ Environment1 + (1|group), family = "pois
simulationOutput <- simulateResiduals(fittedModel = fittedModel)
plotSimulatedResiduals(simulationOutput = simulationOutput)
testSimulatedResiduals(simulationOutput = simulationOutput)
testUniformDistribution(simulationOutput = simulationOutput)
```

Adding a simple overdispersion correction will try to find a compromise between the different levels of dispersion in the model. The qq plot looks better now, but there is still a pattern in the residuals
Expand All @@ -264,7 +264,7 @@ fittedModel <- glmer(observedResponse ~ Environment1 + (1|group) + (1|ID), famil
simulationOutput <- simulateResiduals(fittedModel = fittedModel)
plotSimulatedResiduals(simulationOutput = simulationOutput)
testSimulatedResiduals(simulationOutput = simulationOutput)
testUniformDistribution(simulationOutput = simulationOutput)
```

To remove this patter, you would need to make the dispersion parameter dependent on a predictor (e.g. in JAGS), or apply a transformation.
Expand All @@ -287,7 +287,7 @@ fittedModel <- glmer(observedResponse ~ Environment1 + Environment2 + (1|group)
simulationOutput <- simulateResiduals(fittedModel = fittedModel)
# plotConventionalResiduals(fittedModel)
plotSimulatedResiduals(simulationOutput = simulationOutput, quantreg = T)
testSimulatedResiduals(simulationOutput = simulationOutput)
testUniformDistribution(simulationOutput = simulationOutput)
```

Difficult to see with the overall pattern, but it becomes clear if we plot against the environment
Expand Down Expand Up @@ -354,7 +354,7 @@ plotSimulatedResiduals(simulationOutput = simulationOutput)
The residuals look clearly overdispersed. We can confirm that with a

```{r}
testSimulatedResiduals(simulationOutput = simulationOutput)
testUniformDistribution(simulationOutput = simulationOutput)
```


Expand Down

0 comments on commit 45c0503

Please sign in to comment.