# Making Predictions from Stan models in R

May 29, 2018 · 6 min read

# Background

Stan (http://mc-stan.org) is a probabilistic programming language for estimating flexible statistical models. If you’re interested in Bayesian modeling, you usually don’t have to look further than Stan. It has almost everything you’ll need to define arbitrarily complex models, explicitly specify prior distributions, and diagnose model performance.

One area where Stan is lacking, however, is reusing estimated models for predictions on new data. Often we fit a model `y ∼ x` and need to save the model for use as new `x` become available. This might be for monthly report, a production system where real-time predictions are necessary, or a competition where judgments are based on predictions from unseen new data.

Though Stan does not yet have a robust workflow for this process, there are a couple of workarounds that can get the job done. In this post I’ll highlight three:

1. Fit-and-predict: This approach involves specifying the predictive model in Stan’s `generated quantities`block and re-estimating the model every time you need to make new predictions. It can be computationally intensive and slow, but it is robust and ensures predictions are always based on the most recently available data. The Stan development team recommends this approach.
2. Predict outside of Stan: This approach involves estimating a model in Stan, then extracting posterior distributions of parameters and rebuilding the predictive structure in another language, such as R or Python.
3. Predict with Stan: This approach involves writing another Stan program with only `data` and `generated quantities` blocks, where`data` block contains posterior distributions for the original program and independent variables.

# Approaches

We’ll start by creating some fake data for this example by simulating data from a logistic regression model to estimate with Stan. Stan is not necessary for estimating this simple model, but the example if useful for illustrating the three approaches to making predictions with Stan. The data generating process is:

y ∼ Bernoulli(π);

π = inv_logit(α+β∗x)

We’ll want to estimate α and β so that we can make predictions for unseen `y` based on new data `x` as it becomes available.

# Fit-and-predict

The fit-and-predict approach uses Stan’s `generated quantities` block to make predictions from `x_test` in the same program that we used to estimate the relationship between `x_train` and `y_train`. From the Stan manual (http://mc-stan.org/users/documentation/index.html):

The generated quantities program block is rather different than the other blocks. Nothing in the generated quantities block affects the sampled parameter values. The block is executed only after a sample has been generated. Among the applications of posterior inference that can be coded in the generated quantities block are

• forward sampling to generate simulated data for model testing,
• generating predictions for new data,
• calculating posterior event probabilities, including multiple comparisons, sign tests, etc.,
• calculating posterior expectations,
• transforming parameters for reporting,
• applying full Bayesian decision theory,
• calculating log likelihoods, deviances, etc. for model comparison.

This Stan program simultaneously fits the logistic regression model based on the training data and generates predictions for `y_test` based on `x_test`.

We’ll estimate this model (the code is saved in a separate file `model_fit.stan`) and check that it was able to recover the parameters α and β.

The model converges and it the posterior distributions of the parameters are centered around their ‘true’ values. The accuracy of the model on new data is 0.75. This is a robust approach for making predictions for new data with Stan, but is impractical if predictions must be made frequently because it requires re-estimating the entire model every time new predictions need to be made.

# Predict with R

Another option is to extract the posterior distributions of the parameters and use them to recreate the model in R. The code below extracts the posterior distributions (`alpha_post` and `beta_post`) and uses them in a prediction function `gen_quant_r`. The function simulates the data generating process with samples from the parameters’ posterior distributions. This approach doesn’t generate full posterior distributions for each prediction, but it is fast and easy to implement.

The accuracy of this predictive approach is similar to the accuracy of the fit-and-predict approach.

# Predict with Stan

The third approach is to write another Stan program to make predictions without refitting the old model. The parameter estimates from the original program become the data for the prediction program. The code below shows how this program might look. Note that the `parameters` and `model` blocks are empty because we are not estimating parameter distributions from a model.

When we run this program, we have to set the algorithm to `fixed_param` so Stan knows that it’s not estimating parameters. We have to go through a bit of effort to extract the distributions of the generated quantities. But once we do, we’ll have full posterior distributions of our predictions with similar accuracy to the two approaches outlined above.

# Conclusion

Stan is a powerful language for Bayesian inference with a robust mechanism for out-of-sample prediction in its `generated quantities` block. However, it is not easy to ‘re-use’ Stan models on new data as it becomes available, particularly if the data is streaming in and predictions must be made regularly. This post has outlined a few options for making live predictions with Stan. Each has strengths and weaknesses; which is most appropriate depends on the use-case.

Written by

Written by

## Automatic Speech Recognition System using KALDI from scratch

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just \$5/month. Upgrade