Does stan (in particular, rstan) have built-in facilities to generate predictive posterior distributions?
It's not hard to generate the distribution from the stan fit, but I'd rather not reinvent the wheel.
Does stan (in particular, rstan) have built-in facilities to generate predictive posterior distributions?
It's not hard to generate the distribution from the stan fit, but I'd rather not reinvent the wheel.
According to the Stan User manual v2.2.0 (pages 361–362):
In Stan, posterior simulations can be generated in two ways. The first approach is to treat the predicted variables as parameters and then define their distributions in the model block. The second approach, which also works for discrete variables, is to generate replicated data using random-number generators in the generated quantities block.
I usually use the latter.
The following is not a thorough answer, but hopefully it's better than no answer. In my own applications I apply posterior predictive checks to examine model predictions for a single dependent measure which has been generated from a linear model. This is simple in JAGS, but somewhat more opaque in Stan.
data{
int<lower=1> N; // no. rows
real x[N]; // predictor
real y[N]; // dependent variable
}
parameters{
real alpha; // int.
real beta; // slope
real<lower=0> sigma_e; // resid. var.
real y_tilde[N]; // post. pred.
}
model{
real mu[N];
for(i in 1:N){
mu[i] <- alpha + beta*x[i];
}
y ~ normal(mu,sigma_e); //lik
y_tilde ~ normal(mu,sigma_e);
alpha ~ normal(0,5);
beta ~ normal(0,5);
sigma_e ~ cauchy(0,5);
}
generated quantities{
real minimum;
real maximum;
minimum <- min(y_tilde);
maximum <- max(y_tilde);
}
There must be a better way to do this, so someone please post a better answer. But the above code generates N posterior predictive distributions, one for each observation. I do this so that the a predictive distribution of extrema can be found, but if you only are interested in the posterior predictive quantity y_tilde
you may be able to do without all of them. For large data sets the above solution is obviously too space-intensive.