Skip to content

Commit

Permalink
Merge pull request #100 from florianhartig/0.2.0.3
Browse files Browse the repository at this point in the history
0.2.0.3
  • Loading branch information
florianhartig authored Jan 13, 2019
2 parents d10ea90 + 9bfb96d commit ac381d0
Show file tree
Hide file tree
Showing 35 changed files with 432 additions and 243 deletions.
8 changes: 8 additions & 0 deletions Code/DHARMaCRAN/Checklist.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Submission Checklist






https://cran.r-project.org/web/checks/check_results_DHARMa.html
62 changes: 19 additions & 43 deletions Code/DHARMaPackageSupport/glmmTMB/glmmTMB.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,18 @@ output:
html_document:
keep_md: yes
toc: yes
editor_options:
chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = T)
```

```{r}
devtools::install_github("glmmTMB/glmmTMB/glmmTMB")
library(DHARMa)
library(glmmTMB)
```

# Testing general package behavior general problems

```{r}
testData = createData(sampleSize = 200, overdispersion = 0.5, family = poisson())
m1 <- glmmTMB(observedResponse ~ Environment1 + (1|group),
family = "poisson", data = testData)
predict(m1)
simulationOutput <- simulateResiduals(fittedModel = m1)
```

# Testing general package behavior / general problems


## Pearson residuals don't work with zi terms (not implemented)
Expand All @@ -60,30 +42,6 @@ boxplot(Salamanders$mined, predict(m1, type = c("response")))
```


```{r}
testData = createData(sampleSize = 200, randomEffectVariance = 2, family = poisson())
m1 <- glmmTMB(observedResponse ~ Environment1 + (1|group),
family = "poisson", data = testData)
summary(m1)
res <- simulateResiduals(fittedModel = m1)
# this plot shows the residual pattern, despite the model being correct
plot(res)
# recalculate predictions by hand - see help ?predict.glmmTMB
# To compute population-level predictions for a given grouping variable (i.e., setting all random effects for that grouping variable to zero), set the group value to NA.
newdata=testData
newdata$group = NA
pred = predict(m1, newdata = newdata)
# Now perform the plot
plotResiduals(pred, res$scaledResiduals)
```


# DHARMa with glmmTMB example cases

## Test 1 - Simple poisson model
Expand Down Expand Up @@ -354,5 +312,23 @@ sessionInfo()
```


Hi guys, I'm testing glmmTMB for DHARMA, and I'm encountering a problem with the development versin on GitHub, the following code runs with the CRAN version, but not with the GitHub version

https://github.com/glmmTMB/glmmTMB/commit/b0084337f92736dfe84fd0f6386c1180a6103181


```{r}
library(DHARMa)
library(glmmTMB)
testData = createData(sampleSize = 500, intercept = 2, fixedEffects = c(1), overdispersion = 0, family = poisson(), quadraticFixedEffects = c(-3), randomEffectVariance = 0, pZeroInflation = 0.6)
fittedModel <- glmmTMB(observedResponse ~ Environment1 + I(Environment1^2) + (1|group), ziformula = ~1 , family = "poisson", data = testData)
summary(fittedModel)
predict(fittedModel, type = "response")
simulationOutput <- simulateResiduals(fittedModel = fittedModel)
plot(simulationOutput)
```




49 changes: 16 additions & 33 deletions Code/DHARMaPackageSupport/spaMM/spaMM-test.R
Original file line number Diff line number Diff line change
@@ -1,67 +1,50 @@
library(spaMM)

data("Loaloa") ## from spaMM package
loafit <- corrHLfit(cbind(npos,ntot-npos)~maxNDVI1+seNDVI
fittedModel <- corrHLfit(cbind(npos,ntot-npos)~maxNDVI1+seNDVI
+Matern(1|longitude+latitude),
data=Loaloa[1:30,],family=binomial(),
init.corrHLfit=list(Nugget=0.1),ranFix=list(nu=0.5))

testModel(fittedModel)

########################

fittedModel = loafit

family(fittedModel)
class(fittedModel)[1]
nobs(fittedModel)
getResponse(fittedModel)

getResponse.HLfit <- function(object, ...){
response(object, ...)
}
DHARMa:::getFixedEffects(fittedModel)

simulate.HLfit(fittedModel, nsim = 2)
predict.HLfit(fittedModel)
coef(fittedModel)
ranef(fittedModel)
fixef(fittedModel)
res = simulateResiduals(fittedModel)
plot(res)

DHARMa:::getFixedEffects(fittedModel)
testData = createData(sampleSize = 10000, overdispersion = 0.0, randomEffectVariance = 1, family = poisson())

fittedModel <- HLfit(observedResponse ~ Environment1 + (1|group) , family = "poisson", data = testData)
res = simulateResiduals(fittedModel)
plot(res)

coef(fittedModel)
fixef(fittedModel)
fittedModel <- glmer(observedResponse ~ Environment1 + (1|group) , family = "poisson", data = testData)
summary(fittedModel)
res = simulateResiduals(fittedModel, re.form = ~0)
plot(res)



testData = createData(sampleSize = 200, overdispersion = 0.0, randomEffectVariance = 1, family = poisson())
fittedModel <- HLfit(observedResponse ~ Environment1 + (1|group) , family = "poisson", data = testData)
res = simulateResiduals(fittedModel)
res = simulateResiduals(fittedModel, refit = T)
plot(res)
testUniformity(res)$p.value < 0.001



testData = createData(sampleSize = 200, overdispersion = 0, randomEffectVariance = 0, family = binomial(), binomialTrials = 20)
fittedModel <- HLfit(cbind(observedResponse1,observedResponse0) ~ Environment1 + (1|group) , family = "binomial", data = testData)
res = simulateResiduals(fittedModel)
plot(res)
testUniformity(res)$p.value < 0.001


testData = createData(sampleSize = 200, fixedEffects = c(1,0), overdispersion = 0, randomEffectVariance = 0, family = binomial())
fittedModel <- HLfit(observedResponse ~ Environment1 + (1|group) , family = "binomial", data = testData)
res = simulateResiduals(fittedModel)
plot(res)
testUniformity(res)$p.value < 0.001


########################


simsCond = simulate(nsim=250, loafit, type="residual") # I think that this is what I call a conditional simulat, i.e. simulations are conditinal on the fitted REs
simsUncond = simulate(nsim=250, loafit, type="marginal") # I think that this is what I call a unconditional simulation, i.e. random effects and glm distribution are re-siulated?
testResiduals(res)

createDHARMa(simulatedResponse = sims, observedResponse = loafit$y,
fittedPredictedResponse = predict(loafit, re.form=~0)[,1L], integer = T)


13 changes: 0 additions & 13 deletions Code/DHARMaPackageSupport/test-template.R

This file was deleted.

11 changes: 6 additions & 5 deletions DHARMa/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: DHARMa
Title: Residual Diagnostics for Hierarchical (Multi-Level / Mixed) Regression Models
Version: 0.2.0
Date: 2018-06-06
Version: 0.2.1
Date: 2019-01-14
Authors@R: c(person("Florian", "Hartig", email = "[email protected]", role = c("aut", "cre"), comment = "Theoretical Ecology, University of Regensburg, Regensburg, Germany"))
Description: The 'DHARMa' package uses a simulation-based approach to create
readily interpretable scaled (quantile) residuals for fitted (generalized) linear mixed
models. Currently supported are (generalized) linear mixed models from 'lme4'
(classes 'lmerMod', 'glmerMod') and 'glmmTMB', generalized additive models ('gam' from 'mgcv'),
'glm' (including 'negbin' from 'MASS', but excluding quasi-distributions) and 'lm' model
(classes 'lmerMod', 'glmerMod'), 'glmmTMB' and 'spaMM', generalized additive models ('gam' from
'mgcv'), 'glm' (including 'negbin' from 'MASS', but excluding quasi-distributions) and 'lm' model
classes. Moreover, externally created simulations, e.g. posterior predictive simulations
from Bayesian software such as 'JAGS', 'STAN', or 'BUGS' can be processed as well.
The resulting residuals are standardized to values between 0 and 1 and can be interpreted
Expand All @@ -32,7 +32,8 @@ Imports:
MASS,
lme4,
mgcv,
glmmTMB (>= 0.2.1)
glmmTMB (>= 0.2.1),
spaMM (>= 2.6.0)
Suggests:
knitr,
testthat
Expand Down
12 changes: 12 additions & 0 deletions DHARMa/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# Generated by roxygen2: do not edit by hand

S3method(fitted,gam)
S3method(getResponse,HLfit)
S3method(getResponse,default)
S3method(getSimulations,HLfit)
S3method(getSimulations,default)
S3method(hist,DHARMa)
S3method(plot,DHARMa)
S3method(print,DHARMa)
S3method(refit,HLfit)
S3method(refit,glmmTMB)
S3method(refit,lm)
S3method(residuals,DHARMa)
export(createDHARMa)
export(createData)
export(getRandomState)
export(getResponse)
export(getSimulations)
export(plotConventionalResiduals)
export(plotQQunif)
export(plotResiduals)
Expand All @@ -19,6 +26,7 @@ export(runBenchmarks)
export(simulateResiduals)
export(testDispersion)
export(testGeneric)
export(testModel)
export(testOutliers)
export(testOverdispersion)
export(testOverdispersionParametric)
Expand All @@ -33,4 +41,8 @@ import(graphics)
import(stats)
import(utils)
importFrom(foreach,"%dopar%")
importFrom(lme4,fixef)
importFrom(lme4,ranef)
importFrom(lme4,refit)
importFrom(spaMM,response)
importFrom(spaMM,update_resp)
2 changes: 1 addition & 1 deletion DHARMa/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DHARMa 0.2.1
New features

- Outlier highlighting (in plots) and formal outlier test, implemented in https://github.com/florianhartig/DHARMa/pull/99
- Support for the SPAMM package
- Supporting now also models fit with the spaMM package

Major changes

Expand Down
2 changes: 1 addition & 1 deletion DHARMa/R/DHARMa.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title DHARMa - Residual Diagnostics for HierArchical (Multi-level / Mixed) Regression Models
#' @name DHARMa
#' @docType package
#' @description The 'DHARMa' package uses a simulation-based approach to create readily interpretable scaled (quantile) residuals for fitted generalized linear mixed models. Currently supported are generalized linear mixed models from 'lme4' (classes 'lmerMod', 'glmerMod') and 'glmmTMB', generalized additive models ('gam' from 'mgcv'), 'glm' (including 'negbin' from 'MASS', but excluding quasi-distributions) and 'lm' model classes. Alternatively, externally created simulations, e.g. posterior predictive simulations from Bayesian software such as 'JAGS', 'STAN', or 'BUGS' can be processed as well. The resulting residuals are standardized to values between 0 and 1 and can be interpreted as intuitively as residuals from a linear regression. The package also provides a number of plot and test functions for typical model misspecification problems, such as over/underdispersion, zero-inflation, and residual spatial and temporal autocorrelation.
#' @description The 'DHARMa' package uses a simulation-based approach to create readily interpretable scaled (quantile) residuals for fitted (generalized) linear mixed models. Currently supported are (generalized) linear mixed models from 'lme4' (classes 'lmerMod', 'glmerMod'), 'glmmTMB' and 'spaMM', generalized additive models ('gam' from 'mgcv'), 'glm' (including 'negbin' from 'MASS', but excluding quasi-distributions) and 'lm' model classes. Moreover, externally created simulations, e.g. posterior predictive simulations from Bayesian software such as 'JAGS', 'STAN', or 'BUGS' can be processed as well. The resulting residuals are standardized to values between 0 and 1 and can be interpreted as intuitively as residuals from a linear regression. The package also provides a number of plot and test functions for typical model misspecification problems, such as over/underdispersion, zero-inflation, and residual spatial and temporal autocorrelation.
#' @details See index / vignette for details
#' @seealso \code{\link{simulateResiduals}}
#' @examples
Expand Down
Loading

0 comments on commit ac381d0

Please sign in to comment.