I have some data on prices that I would like to predict as a function of covariates. One of those covariates is the predicted rating of that item as estimated from totally separate item rating data. I would like to treat this covariate as an out-of-sample prediction. So I would load the separate price and ratings data in the data step, estimate the parameters of the rating distribution in the model step, and then estimate the parameters of the price model given a proposal from the posterior predictive distribution of ratings.

Is this possible in Stan? Thank you.

P.S. Let me know if you need more details to formulate an answer. I hope this is enough but maybe not.

  • $\begingroup$ Without knowing too much about the details of the model, this sounds like a fairly straightforward regression. If so, then I recommend looking into the rstanarm package which has as a number of "built-in" models that use syntax you may be familiar with if you've ever used lme4. Posterior predictions are quite easy to obtain using this package and there is a growing body of documentation that is easy enough to run through. $\endgroup$ Commented Jul 27, 2016 at 15:23
  • $\begingroup$ This is not a straightforward regression because one of the covariates does not have a fixed value. As I mentioned in the question, its value is estimated from separate data. $\endgroup$ Commented Jul 27, 2016 at 15:36
  • 1
    $\begingroup$ Drawing a realization of a covariate from a distribution is technically not difficult. Estimating a model that treats an estimate of a covariate as if it were observed prevents the uncertainty in the estimate from propagating to the uncertainty about everything else. It would be better to do a bivariate likelihood. $\endgroup$ Commented Jul 27, 2016 at 18:05

1 Answer 1


The posterior predictive distribution can be obtained from any software that produces draws from the posterior distribution. It sounds as if your likelihood is normal or log-normal, in which case you can multiply the predictor(s) by a draw of the coefficient(s) to form a linear predictor. And then for each new observation you are trying to predict, draw from a normal distribution with mean equal to the corresponding linear predictor and standard deviation equal to the corresponding posterior draw of the error standard deviation. (If your likelihood is log-normal or the outcome was logged, then antilog this draw from the normal distribution). Then just repeat that process for all the draws of the coefficient(s) and error standard deviation that you obtain from the posterior distribution.

The above is not specific to Stan but you can perform the above steps in the generated quantities block of a Stan program utilizing the normal_rng function.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.