strengejacke / ggeffects

Estimated Marginal Means and Marginal Effects from Regression Models for ggplot2
https://strengejacke.github.io/ggeffects
Other
535 stars 34 forks source link

test_predictions not working with gamm4 model output #516

Closed dancrear closed 2 months ago

dancrear commented 2 months ago

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

strengejacke commented 2 months ago

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

dancrear commented 2 months ago

Hi,

I appreciate the quick reply. I will try the workout you suggested.

I just tried to run the code you provided and I receive the below error message. I'm currently using ggeffects v 1.5.2 and R v 4.4.0.

Error in match.arg(engine, c("marginaleffects", "emmeans")) : 'arg' should be one of “marginaleffects”, “emmeans”

Standing by. Thanks!

On Mon, May 6, 2024 at 5:26 AM Daniel @.***> wrote:

Closed #516 https://github.com/strengejacke/ggeffects/issues/516 as completed via #518 https://github.com/strengejacke/ggeffects/pull/518.

— Reply to this email directly, view it on GitHub https://github.com/strengejacke/ggeffects/issues/516#event-12714311360, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWCCN22KAF6QMGSSS5BJAWLZA5ZH3AVCNFSM6AAAAABHIGXP22VHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJSG4YTIMZRGEZTMMA . You are receiving this because you authored the thread.Message ID: @.***>

-- Dan Crear, PhD

strengejacke commented 2 months ago

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

strengejacke commented 2 months ago

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