strengejacke / ggeffects

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

Predicting to zero-inflated truncated poisson model does not work for some prediction types #231

Open Peter-Brot opened 3 years ago

Peter-Brot commented 3 years ago

Hi,

[I am new to GitHub and also a beginner (but interested) in statistics and would kindly ask you to excuse possible flaws in my way presenting the issue]

For my count data of attack events by bark beetles on small bark windows, I fitted a zero-inflated GLMM with truncated poisson family using the glmmTMB package.

I then wanted to plot the predictions with 95% CI to obtain conditional plots.

This worked perfectly with the ggpredict()-function for the conditional part of the model (type = "fe"). I also obtained the probabilities from the zi part of the model (using type = "zi_prob").

However, when I try to predict to the full model (type = "zi_random" or "zero_inflated" or "zero_inflated_random") - all other parameters to specify the predictions of the ggpredict-function remain the same - I receive the following error message:

"Error in get_predictions_glmmTMB(model, data_grid, ci.lvl, linv, type, : 'list' object cannot be coerced to type 'double'"

My R skills are not profound enough to find out myself why the input from the model causes a problem at this point, but not for the other types of predictions. I am not sure whether it might be a bug or whether it is a problem of my model (or my understanding from it).

I would very much appreciate any hint or councel to solve this.

Many thanks in advance

Tobias

P.S.: Of course I can provide code and data supporting the issue if this helps solving it

strengejacke commented 3 years ago

Do you have a small reproducible example?

Peter-Brot commented 3 years ago

I played around a bit more: the error only appears when I specify the level of the categorical predictor under the "condition"-parameter of the ggpredict()-function; otherwise the prediction for type = "fe.zi" works fine.

See code below:

ExampleData.csv

library(glmmTMB)
library(ggeffects)
exampleData <- read.csv("ExampleData.csv")

model <- glmmTMB(response ~ (Treatment + Pred_continuous)^2 + Pred.categorical + (1|Block/Plot), zi = ~ Pred_continuous + Pred.categorical, data = exampleData, family = truncated_poisson())

ggpredict(model, type = "fe.zi", terms = c("Treatment", "Pred_continuous"))
   # -> I obtain predictions

ggpredict(model, type = "fe.zi", terms = c("Treatment", "Pred_continuous"), condition = c('Pred.categorical' = "low"))
  # -> once I specify a condition to the categorical predictor, I receive the error message

ggpredict(model, type = "fe", terms = c("Treatment", "Pred_continuous"), condition = c('Pred.categorical' = "low"))
  # -> When I change type to "fe", predictions can be obtained again

Is this a missfunction or is there a statistical/logical explanation for why I can't predict to a specific level of a categorical predictor that appears also in the zi-part of the model? Might be that its my limited knowledge about these models and not a problem of the function afterall...

Thanks for your time and help

Best

strengejacke commented 2 years ago

For the "full" model (i.e. if you take both the count and zero-inflated components into account), predictions are based on simulated draws that include the vcov- and model.matrix (see https://strengejacke.github.io/ggeffects/articles/introduction_randomeffects.html#marginal-effects-for-zero-inflated-mixed-models). In order to do these simulations, you cannot condition (i.e. "filter") on a single factor level, thus the function fails.

So, there's nothing wrong with your model specification, it's just a limitation of the current implemented code when calculating predictions (including confidence intervals) on the response scale for models with zero-inflation component. At the moment, I'm not sure how to solve this issue.