strengejacke/ggeffects

test_predictions not working with gamm4 model output

dancrear opened this issue · 4 comments

Hi,

I was able to to get marginal means calculated for my gamm4 model using predict_response. When I did this I also did not specify the output component, either gam or mer. Then when I put the my predict_response object into test_predictions to actually do some pairwise comparisons I receive the error...

Error: Objects of class list are not yet supported.

If I put the gam component of the model into predict_response, I get the same marginal mean values without issue. Then when I put that output into test_predictions I get the long error below which tells me models of class gam are not supported but in the list of supported classes, gam is listed, so I'm not sure what is going on. Thanks!

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

gamm4 is not yet supported by the marginaleffects package, which is by default used internally to calculate pairwise comparisons. However, you could switch to engine = "ggeffects":

library(ggeffects)
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))
predict_response(m1, "x1 [0.1, 0.5, 0.8]") |> test_predictions(engine = "ggeffects")
#> # Pairwise comparisons
#> 
#> x1      | Contrast |       95% CI |      p
#> ------------------------------------------
#> 0.1-0.5 |    -2.51 | -3.52, -1.49 | < .001
#> 0.1-0.8 |    -4.39 | -5.43, -3.35 | < .001
#> 0.5-0.8 |    -1.88 | -2.88, -0.88 | < .001

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

You need to wait until #518 is merged and binaries for ggeffects 1.5.2.26 are built on R-universe, then you can update as described here: https://github.com/strengejacke/ggeffects?tab=readme-ov-file#installation

You need to wait until binaries for ggeffects 1.5.2.26 are built on R-universe, then you can update as described here: https://github.com/strengejacke/ggeffects?tab=readme-ov-file#installation

Try install.packages("ggeffects", repos = "https://strengejacke.r-universe.dev")