vincentarelbundock / marginaleffects

R package to compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and ML models. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference
https://marginaleffects.com
Other
466 stars 47 forks source link

Warning with lme4 or glmmTMB models #1156

Closed TurnipWu closed 4 months ago

TurnipWu commented 4 months ago

Dear @vincentarelbundock, I am reaching out with a question regarding the use of the marginaleffects package for computing marginal effects in mixed-effects models. As I was calculating the marginal effects for a mixed-effects model, I encountered a Warning message when using the slopes function with the argument re.form = NULL. The warning indicates that marginaleffects only takes into account the uncertainty in fixed-effect parameters. This warning has left me slightly confused. From my understanding, when using the predict.merMod function in the lme4 package with re.form = NULL, the output includes all random effects. Therefore, it seems to me that the uncertainty in the random effects should also be considered. I would greatly appreciate it if you could clarify this matter for me. Your insights would be most helpful, and any assistance you can provide will be warmly welcomed. Thank you very much for your time and consideration.

vincentarelbundock commented 4 months ago

Yes, this is an important warning and many users will want something different than what is offered here.

Accounting for random effects while marginalizing is a difficult problem, and as far as I know, there is currently no software in R which can do that.

One alternative is to switch to a Bayesian approach using brms. You can compare the "Mixed effects" vignette to the "Bayes" vignettes on marginaleffects.com