11
$\begingroup$

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.

$\endgroup$
1
  • 3
    $\begingroup$ There is a generated quantities section at the end that is supposed to be able to handle simulation, but the documentation (as of ver. 1.3, but ver. 2 should be out soon) does not explain in much detail how to accomplish this. You might consider asking the mailing list. $\endgroup$
    – John
    Commented Oct 11, 2013 at 17:19

2 Answers 2

5
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$

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.