easystats / insight

:crystal_ball: Easy access to model information for various model objects
https://easystats.github.io/insight/
GNU General Public License v3.0
394 stars 39 forks source link

get_predicted(): zero-inflation options #413

Closed bwiernik closed 2 years ago

bwiernik commented 3 years ago

Currently, for zero-inflated models, get_predicted() and its downstream functions like modelbased::estimate_expectation() always return the equivalent to type = "conditional" (predicted values assuming non-zero). It would be good to allow users to specify other methods, such as predicting unconditional response predictions (incorporating both parts of the model) or just the zero-inflation parts.

It would also be good I think to make the default equivalent to type = "response" (incorporating both model parts).

See the type argument in predict.glmmTMB():

type
Denoting mu as the mean of the conditional distribution and p as the zero-inflation probability, the possible choices are:

"link" conditional mean on the scale of the link function, or equivalently the linear predictor of the conditional model "response" expected value; this is mu*(1-p) for zero-inflated models and mu otherwise "conditional" mean of the conditional response; mu for all models (i.e., synonymous with "response" in the absence of zero-inflation "zprob" the probability of a structural zero (gives an error for non-zero-inflated models) "zlink" predicted zero-inflation probability on the scale of the logit link functio> "disp" dispersion parameter however it is defined for that particular family as described in sigma.glmmTMB

DominiqueMakowski commented 3 years ago

do you have a reproducible example with a model that has these components so I can play around?

bwiernik commented 3 years ago

Away from computer. The second example in ?glmmTMB

DominiqueMakowski commented 3 years ago
library(glmmTMB)

m <- glmmTMB(count ~ spp + mined + (1|site),
             zi=~spp + mined,
             family=nbinom2, data=Salamanders)

head(insight::get_predicted(m))
#> [1] 0.5387752 1.0768783 0.3554236 2.4701755 2.4950498 2.1819828
head(insight::get_predicted(m, type = "zprob"))
#> [1] 2.040119 2.040119 2.040119 1.174339 1.174339 1.174339

Created on 2021-08-04 by the reprex package (v2.0.0)

custom types should work after the latest commit.

So now it becomes a question or wether we want to change / add to the behaviour of our main predict argument (this is also what would drive https://github.com/easystats/modelbased/issues/136). We could have predict = "dispersion" or something like that, but then again I'm not very familiar with these models so I don't know

bwiernik commented 3 years ago

Yes, I think the predict argument should have options for zero inflated and dispersion parameters

bwiernik commented 3 years ago

I think we should revert the type argument. Our predict argument fills the same role, and it's confusing to have two.

Instead, I think we add options to predict to include these:

Existing

Existing labels, need adjusted behavior

New labels

For predict = "prediction", we should include the dispersion parameter in the prediction intervals. @DominiqueMakowski If you could add a placeholder for that, I can fill in the necessary extractions from glmmTMB objects. How do we currently handle dispersion for things like Poisson models where there is a variance term in the model?

DominiqueMakowski commented 3 years ago

The needed steps to add/edit I believe are:

https://github.com/easystats/insight/blob/1540c06176fb8c9994cc3348fc1eb0b09c971732/R/get_predicted.R#L275

https://github.com/easystats/insight/blob/1540c06176fb8c9994cc3348fc1eb0b09c971732/R/get_predicted.R#L645-L652

https://github.com/easystats/insight/blob/1540c06176fb8c9994cc3348fc1eb0b09c971732/R/get_predicted.R#L633-L643

(essentially since we have one "master" argument predict, it is then passed to the .get_predicted_args() helper that assigns the traditional arguments)

strengejacke commented 3 years ago

@DominiqueMakowski can insight be submitted, or is there anything that needs to be addressed for modelbased?

DominiqueMakowski commented 3 years ago

I wouldn't say that this issue of adding more options for glmmTMB is urgent so probably not a blocker for a CRAN update

strengejacke commented 2 years ago

I think this will be closed in #501

strengejacke commented 2 years ago

should be resolved in #501 to #503