Hey team, I’m still new to STAN. I’m struggling to understand how to use the generated quantities
block to get inferences for new data points. I’ll use rstan
as my interface.
Stan program
For simplicity, let’s use the example program in the stan getting started guide. And the schools data as a simple example.
// saved as schools.stan
data {
int<lower=0> J;
real y[J];
real<lower=0> sigma[J];
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
vector[J] theta;
theta = mu + tau * eta;
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
Example
library(rstan)
data = list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
# new schools:
new_schools = list(K=3, y_tilde=c(15, 25, 23), sigma_tilde=c(10, 9, 18))
fit1 = stan(
file= schools.stan
, data = data
, chains = 4
, cores = 4
, warmup = 1000
, iter = 2000
)
I would like to take draws of p(\tilde{\theta_k} | \mu,\tau, \tilde{y}) to summarize the posterior prediction of the new school. I understand that to do so, I first need draws of the posterior of (\mu, \tau) and then draws of p(\tilde{\theta_k} | \mu,\tau), but I’m unsure how to use the generated quantities block to do so.
I have reviewed the documentation and other questions on this forum (1, 2, 3) but am still confused on how to go about this.
Any help is much appreciated, thanks!