I have the program with vector indicating grouping (group_id) and continuous variable to model (y). My interest is to generate prediction based on estimated \mu and a new length, grouping data (N_New, group_id_new). In addition, I want calcutate some quantities, e.g. mean of predicted difference between groups, e.g. Group_2 - Group_1, and some other quantities. Is there a way to index and and obtain by_group “y_rep” in order to create other quantities of interest in the generated quantities block or is this something that I need to do outside the stan program? Here is a simple example code assuming 3 groups.
Hopefully stan code below is clear and your responses don’t make me look dumb.
data {
int<lower = 1> N; // Data sample size
int<lower = 2> N_g; // number of groups
vector[N] y; // continuous response
int<lower = 1, upper = N_g> group_id[N]; // group ID for each response
int<lower = 1> N_New; // Prediction sample size
int<lower = 1, upper = N_g> group_id_new[N_New]; // prediction group ID for predicted responses
}
parameters {
vector[N_g] mu; // estimated group means
vector<lower = 0>[N_g] sigma; // estimated group SD
}
model {
// priors
mu ~ normal(0, 100);
sigma ~ cauchy(0, 2.5);
// Model
for (n in 1:N) {
y[n] ~ normal(mu[group_id[n]], sigma[group_id[n]]);
}
}
generated quantities {
vector[N_New] y_rep; // posterior predictive
vector[N_g] mu_pred; // mean of predicted values (I may be defining this incorrectly, need help)
for (p in 1:N_New) {
y_rep[p] = normal_rng(mu[group_id_new[p]], sigma[group_id_new[p]]);
}
// This is where I need help
// I would like to have mu_pred separated so that I can have
// other quantities e.g. "mu_pred[2]-mu_pred[1], e.t.c
for (g in 1:N_g) {
mu_pred[g] = mean(y_rep[group_id_new[g]]);
}
}
****