strengejacke / ggeffects

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

Errors with new ggeffects version & zero-inflated negative binomial model (glmmTMB) #468

Closed jml-science closed 7 months ago

jml-science commented 7 months ago

Greetings,

I had written working code with ggemmeans() and ggpredict() for zero-inflated mixed models produced with glmmTMB a while back, which is now throwing errors (with updated packages). I recreated the code with the predict_response() and receive the same error messages.

My data is quite exhaustive which is why I don't have a reproducible examples quickly, but I can provide one if needed. I just thought maybe it is a silly thing/change that I'm missing and someone knows what's up.

Here is my model:

formula_interaction = gramsperday_int ~ 1 + (1 | state) + age_cat + sex_recode + education_summary + mentalhealth.gmc + RestrictiveSumScore_scaled.gmc + age_cat:RestrictiveSumScore_scaled.gmc

mod <- glmmTMB(as.formula(formula_interaction), data=data.subset, ziformula=~., family=nbinom2, na.action=na.exclude, REML = FALSE, control = glmmTMBControl(profile = TRUE, optCtrl=list(iter.max=2e3,eval.max=2e3)))

And here are the predictions/marginal effects that I try to produce and the error messages that I receive:

predict_response(model = mod, terms = c("RestrictiveSumScore_scaled.gmc [0,3]", "age_cat"), margin = "mean_mode", type = "fixed")

> Error: Error in fun == "weighted.mean" && !is.null(weights) : 'length = 2' in coercion to 'logical(1)'

predict_response(mod, "education_summary", margin = "marginalmeans", type = "zero_inflated")

> Error: Error in exp(prdat.sim$cond) : Non-numerical argument for mathematical function

predict_response(mod, "education_summary", margin = "mean_reference", type = "zero_inflated")

> Error in vapply(prdat.sim, nrow, numeric(1)) : Values must have length of 1, Result from FUN(X[[1]]) has length of 0

Choosing predict_response(margin = "empirical") seems to be working (but takes forever for my models).

I'd greatly appreciate any hint! Julia

strengejacke commented 7 months ago

Thanks for reporting! Do you have a reproducible example?

jml-science commented 7 months ago

Thank you! While creating a reproducible example with the Owls vs. my data, I realised that the errors occurred because my grouping variable (state) and other categorial variables (sex) were a character object instead of a factor object.

The issue was resolved when mutating those variables into factor variables, and re-running the models.

strengejacke commented 7 months ago

That's great! So I can close this issue?

jml-science commented 7 months ago

Yes, thanks!