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
392 stars 43 forks source link

`gamm4` not supported, resp. `gam` part not supported, despite being listed #1119

Closed strengejacke closed 1 month ago

strengejacke commented 1 month ago

Models of class gamm4, respectively the $gam part is not supported, although gam is listed as supported class. What could be the issue here?

set.seed(123)
dat <- mgcv::gamSim(1, n = 400, scale = 2) ## simulate 4 term additive truth
#> Gu & Wahba 4 term additive model
dat$fac <- fac <- as.factor(sample(1:20, 400, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1) %*% rnorm(20) * 0.5

set.seed(123)
m1 <- gamm4::gamm4(y ~ s(x0) + x1 + s(x2), data = dat, random = ~ (1 | fac))
marginaleffects::predictions(m1, variables = "x1", hypothesis = "pairwise")
#> Error: Models of class "list" are not supported. Supported model classes
#>   include:
#>   
#>   afex_aov, amest, bart, betareg, bglmerMod, bigglm, biglm, blmerMod,
#>   bracl, brglmFit, brmsfit, brnb, clm, clmm2, clogit, coxph, crch, fixest,
#>   flac, flexsurvreg, flic, gam, Gam, gamlss, geeglm, glimML, glm,
#>   glmerMod, glmmPQL, glmmTMB, glmrob, glmx, gls, Gls, hetprob, hurdle,
#>   hxlr, iv_robust, ivpml, ivreg, Learner, lm, lm_robust, lme, lmerMod,
#>   lmerModLmerTest, lmrob, lmRob, loess, logistf, lrm, mblogit, mclogit,
#>   MCMCglmm, mhurdle, mira, mlogit, model_fit, multinom, mvgam, negbin,
#>   nls, ols, oohbchoice, orm, phyloglm, phylolm, plm, polr, Rchoice,
#>   rendo.base, rlmerMod, rq, scam, selection, speedglm, speedlm, stanreg,
#>   survreg, svyolr, tobit, tobit1, truncreg, workflow, zeroinfl
#>   
#>   New modeling packages can usually be supported by `marginaleffects` if
#>   they include a working `predict()` method. If you believe that this is
#>   the case, please file a feature request on Github:
#>   https://github.com/vincentarelbundock/marginaleffects/issues
marginaleffects::predictions(m1$gam, variables = "x1", hypothesis = "pairwise")
#> Error: Models of class "gam" are not supported. Supported model classes
#>   include:
#>   
#>   afex_aov, amest, bart, betareg, bglmerMod, bigglm, biglm, blmerMod,
#>   bracl, brglmFit, brmsfit, brnb, clm, clmm2, clogit, coxph, crch, fixest,
#>   flac, flexsurvreg, flic, gam, Gam, gamlss, geeglm, glimML, glm,
#>   glmerMod, glmmPQL, glmmTMB, glmrob, glmx, gls, Gls, hetprob, hurdle,
#>   hxlr, iv_robust, ivpml, ivreg, Learner, lm, lm_robust, lme, lmerMod,
#>   lmerModLmerTest, lmrob, lmRob, loess, logistf, lrm, mblogit, mclogit,
#>   MCMCglmm, mhurdle, mira, mlogit, model_fit, multinom, mvgam, negbin,
#>   nls, ols, oohbchoice, orm, phyloglm, phylolm, plm, polr, Rchoice,
#>   rendo.base, rlmerMod, rq, scam, selection, speedglm, speedlm, stanreg,
#>   survreg, svyolr, tobit, tobit1, truncreg, workflow, zeroinfl
#>   
#>   New modeling packages can usually be supported by `marginaleffects` if
#>   they include a working `predict()` method. If you believe that this is
#>   the case, please file a feature request on Github:
#>   https://github.com/vincentarelbundock/marginaleffects/issues

Created on 2024-05-06 with reprex v2.1.0

vincentarelbundock commented 1 month ago

Why not call?

predictions(m1$gam, variables = "x1", hypothesis = "pairwise")
vincentarelbundock commented 1 month ago

I just checked, and it's not quite the same model class. Internally, we check for class c("gam", "glm", "lm") produced by the mgcv package.

There's already a thread about adding support for gamm4 models. I am still interested in this, and it should be possible. I'm not sure I'll have time in the near future, but this should be pretty easy.

The only tricky aspect might be class inheritance conflicts because gamm4 uses the imprecise gam class.

There's an existing issue to add support for gamm4. It is listed in the "master" Support New Models issue, and I'll link here and close: https://github.com/vincentarelbundock/marginaleffects/issues/947

strengejacke commented 1 month ago

Ok, thanks for clarification!

strengejacke commented 1 month ago

Why not call?

predictions(m1$gam, variables = "x1", hypothesis = "pairwise")

That's what I did in the second example, which also failed.