How to implement predictions
See original GitHub issueI would like to implement a Model.predict() method. My original idea was to replicate predict() in R, where we pass a data frame and obtain predictions for the mean response. However, @aloctavodia helped me to realize we should consider both predictions for the mean response and predictions for the response variable (like what is being done with Model.posterior_predictive() but also available for out-of-sample data).
So, what I have in mind is something as follows
predictions = model.predict(idata, kind, xdata=None)
where kind = "mean" returns predictions in terms of the mean response (i.e. g^-1(x’b)) and kind="response" returns the posterior predictive distribution. If xdata is a data frame, predictions are made using the new data set (out of sample predictions). If not, predictions are made using the original sample (in sample predictions).
What I would like to discuss
- We already have a
Model.posterior_predictive()method that samples the posterior predictive distribution, using the observed values for the predictors. There’s overlap between this method and the one I want to implement. Should we keep both? Only one? I don’t want to have two methods doing something similar (or the same thing), but we have to be aware droppingModel.posterior_predictive()is another API change and can break existing code. Maybe a deprecation warning? - Argument names and default behavior.
- Anything you would like to add to this discussion.
Linking #356, where this feature is requested.
Issue Analytics
- State:
- Created 2 years ago
- Comments:15

Top Related StackOverflow Question
Hi @ioannis12
This is an example of how you could do it for mixed-effects models. I used simulated data so it may differ from yours.
My simulated data
Same model
I just grab some samples from my original data to act as a test data, here you will have your test data.
Take the posterior from the inference data result, and stack chains and draws.
And now, perhaps, the most tricky part. In mixed-effects models, you have something like
$y = X\beta + Zu$, so we need to construct both X and Z, for the common and group-specific parts respectively. Here we use a method from the internal design object in the model. This is what we want to avoid people from doing with a higher-level implementation of predictions. But since it’s not available yet, here we go.
The coefficients beta and u are taken from the posterior.
And finally, compute the linear predictor and pass it to the
expit()function (to convert from log-odds to probability scale). You can summarize the posterior for each individual with the mean, or any other function you want. Or you can just keep the posterior!Edit: This is something anyone can do with their models if in need of predictions right now. In the nearby future, this will be replaced with a method call such as
Model.predict().@ioannis12 @gregorystrubel the PR #372 implements predictions for Bambi. Feel free to leave your suggestions if you want. Thanks!