# In Stan is there a way to use parameter posterior from old analysis as prior in new analysis?

Normally within the model block I might specify a prior on a parameter with

y ~ normal(mu, sigma);


But what if I already have a posterior on y from a previous analysis, and what to use that posterior as the prior in the new analysis. Can I import a set of samples from the old posterior and sample from that? How is that done? Can I import an entire Stan object from an old analysis and use the parameter distributions coded therein?

• P.S. It would be nice to upvote / approve the answers as you see fit, to recognize the efforts of the respondents. – John K. Kruschke Dec 14 '16 at 21:19

As mentioned by previous answers, Stan, JAGS, and WinBUGS require that priors be specified as mathematical functions.

If you've already got an MCMC-represented posterior from a previous analysis, and you want to use that MCMC posterior as a prior for subsequent data, you must approximate the MCMC posterior in a mathematical form. Unless you have a simple model with conjugate priors, the mathematical approximation of the MCMC distribution will be only an approximation.

As was implicit in Bjorn's answer, it's important to include the correlations of the parameters from the MCMC distribution in the mathematical approximation for the prior.

Finally, if the previous data and the novel data have exactly the same structure and you're using exactly the same model, then you can get an exact answer by combining the two data sets and running the model just once on the combined data.

Most software such as Stan, WingBUGS, SAS etc. requires you to provide an analytic form for the prior instead of MCMC samples. Possible ways around it are to refit the model with all data or to approximate the posterior with e.g. some mixture distribution (e.g. of bivariate normals to $\mu$ and $\log \sigma$ - e.g. in R using e.g. the mclust package or in SAS using PROC FMM) and to use the mixture log-pdf as the new prior.

• This is exactly what I ended up doing. Thank you. – Count Zero Dec 27 '16 at 17:53

Could you clarify, what did you mean by "I already have a posterior on y?"

Do you mean you have generated samples to describe the distribution of the various parameters that were fit to your data (y), in accordance with the model you defined? If this is the case, you still need to apply those parameter fits to your input data to create predictions for each observation - which you can do by "reverse engineering" your model statement, using parameter values supplied by Stan - or generating predictions in the generated_quantities block (example here, or I can provide more detail on this if necessary, just post your original model statement). Once you have predictions, you can certainly apply any other kind of additional modeling to it, within or outside of Stan.

You might be also asking if you can "do something" with not only the point values of the parameters that came out of your first model, but also the shape of their distributions? If that's the case, I'd suggest looking into hierarchical models, which are awesome tools but come with their own set of challenges.

If you haven't seen it yet, I'd recommend John Kruschke's book Doing Bayesian Data Analysis as a great resource for this kind of thing. Chapter 9 is all about hierarchical models.