New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for glmmTMB in DHARMa #16

Open
florianhartig opened this Issue Jan 30, 2017 · 4 comments

Comments

Projects
None yet
1 participant
@florianhartig
Copy link
Owner

florianhartig commented Jan 30, 2017

A user requested support for https://github.com/glmmTMB/glmmTMB

Status of this request (updated)

glmmTMB is supported by DHARMA since https://github.com/florianhartig/DHARMa/releases/tag/v0.1.6.2. A simple example is

m <- glmmTMB(count~ mined, family=poisson, data=Salamanders)
summary(m)

res = simulateResiduals(m)
plot(res, asFactor = T)

image

More examples in the vignette or in https://github.com/florianhartig/DHARMa/tree/master/Code/DHARMaPackageSupport/glmmTMB

Limitations

The main limitation is currently that glmmTMB doesn't support the reform argument in either predict or simulate. Predict() is conditional on all random effects, corresponding to lme4 re.form = NULL. Simulate() is unconditional, i.e. all random effects will be re-simulated, corresponding to lme4 re.form = 0. That means that all predictions and simulations are conditional on REs, which can sometimes create a positive correlation between res and predicted , see #43

You can bypass this problem by calculating unconditional predictions by hand - follow this example

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)

Other than that, it seems to me that the glmmTMB interacts fine with DHARMa.

@florianhartig florianhartig changed the title Add support for glmmTMB Support for glmmTMB in DHARMa Jan 30, 2017

@florianhartig florianhartig added this to the 0.1.7 release milestone Mar 28, 2018

@florianhartig

This comment has been minimized.

Copy link
Owner

florianhartig commented Mar 30, 2018

glmmTMB has now implemented a simulate function, and I have added this to DHARMa. You can test this by installing the development version of DHARMa (see https://github.com/florianhartig/DHARMa).

Note: I had problems with the CRAN version of glmmTMB (crashed with some models). I would therefore recommend installing the development version of glmmTMB before installing DHARMa via

devtools::install_github("glmmTMB/glmmTMB/glmmTMB")

Main limitation is currently that glmmTMB doesn't support the reform argument in either predict or simulate. Predict() is conditional on all random effects, corresponding to lme4 re.form = NULL. Simulate() is unconditional, i.e. all random effects will be re-simulated, corresponding to lme4 re.form = 0. That means that all predictions and simulations are conditional on REs, which can sometimes create a positive correlation between res and predicted , see #43

Other than that, it seems to me that the glmmTMB interacts fine with DHARMa. A simple example is

m <- glmmTMB(count~ mined, family=poisson, data=Salamanders)
summary(m)

res = simulateResiduals(m)
plot(res, asFactor = T)

image

More examples in the vignette

@florianhartig

This comment has been minimized.

Copy link
Owner

florianhartig commented Apr 5, 2018

Merged into master with ed836dd

@florianhartig

This comment has been minimized.

Copy link
Owner

florianhartig commented Apr 5, 2018

Things that are solved

  • works with the development version of glmmTMB, but not yet with the CRAN version [SOLVED new CRAN version glmmTMB 0.2.1 works fully with DHARMa]

  • pearson residuals don't work with zi terms (not implemented), which limits some of the overdispersion tests - solved, simply not fix this (probably not a real problem, see glmmTMB/glmmTMB#313)

Things that are still unsolved

  • glmmTMB doesn't support the reform argument in either predict or simulate. Predict() is conditional on all random effects, corresponding to lme4 re.form = NULL. Simulate() is unconditional, i.e. all random effects will be re-simulated, corresponding to lme4 re.form = 0. That means that all predictions and simulations are conditional on REs, which can sometimes create a positive correlation between res and predicted , see #43

  • glmmTMB residual function fails with factor response (already fixed in glmmTMB develoment branch)

  • Check / created tests for structured RE behavior

@florianhartig

This comment has been minimized.

Copy link
Owner

florianhartig commented Dec 16, 2018

added instructions about how to calculate unconditional predictions by hand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment