Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Peter-Brot opened this issue Aug 17, 2021 · 3 comments
Labels
3 investigators ❔❓ help us 👀 Help is needed to implement something wontfix 🚫 This will not be worked on

Comments

@Peter-Brot
Copy link

Peter-Brot commented Aug 17, 2021

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
Copy link
Owner

Do you have a small reproducible example?

@strengejacke strengejacke added the reprex 💾 An example (and data) to reproduce the issue is needed to label Aug 17, 2021
@Peter-Brot
Copy link
Author

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
Copy link
Owner

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.

@strengejacke strengejacke added 3 investigators ❔❓ help us 👀 Help is needed to implement something wontfix 🚫 This will not be worked on and removed reprex 💾 An example (and data) to reproduce the issue is needed to labels Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 investigators ❔❓ help us 👀 Help is needed to implement something wontfix 🚫 This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants