Skip to content
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

Closed
florianhartig opened this issue Jan 30, 2017 · 11 comments
Closed

Support for glmmTMB in DHARMa #16

florianhartig opened this issue Jan 30, 2017 · 11 comments

Comments

@florianhartig
Copy link
Owner

florianhartig commented Jan 30, 2017

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

Status of this request (updated)

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

@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
Copy link
Owner Author

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
Copy link
Owner Author

Merged into master with ed836dd

@florianhartig
Copy link
Owner Author

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 residual checks with DHARMa  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 Include random effects when plotting residuals vs. predicted? #43

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

  • Check / created tests for structured RE behavior

@florianhartig
Copy link
Owner Author

added instructions about how to calculate unconditional predictions by hand

@mebrooks
Copy link

mebrooks commented Oct 10, 2019

Any chance you could test the re.form in predict sometime soon? Will this resolve the warning from simulateResiduals which refers to this issue?

@florianhartig
Copy link
Owner Author

Hi Mollie, yes, I have tested this in https://github.com/florianhartig/DHARMa/tree/2.4.2-glmmTMBfix and it works like a charm, at least as far as the integration in DHARMa and the problem I had with #16 is concerned. As soon as you push this on CRAN, I will do an update to DHARMa!

@florianhartig
Copy link
Owner Author

Notes to users: if you want to make use of these recent changes

@florianhartig
Copy link
Owner Author

florianhartig commented Feb 4, 2020

Limitations (old)

This problem was fixed with 32cbadc and glmmTMB 1.0

====

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

NOTES TO USERS (Dez 2019): this issue is fixed in glmmTMB, this fix is not yet on CRAN. You can

Alternative way to bypass this problem: calculate 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)

@florianhartig
Copy link
Owner Author

Since DHARMa 0.2.7 and glmmTMB 1.0, it seems all problems are solved, and I will close this now!

@mebrooks
Copy link

mebrooks commented Feb 4, 2020

Hurray!

@florianhartig
Copy link
Owner Author

What remains open is to ability to condition on REs when simulating, see glmmTMB/glmmTMB#888

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

No branches or pull requests

2 participants