Simulating posterior predictive samples for hierachical models

Is anyone able to help me identify the issues with my posterior predictive checks? I’m attempting to simulate data based on my model. I’m getting an error message stating that there are too many indexes. Expression dimensions = 0, indexes = 1.

Here is the model:

model{
  //Likelihood
  for(i in 1:N) {
    int a_respondent;
    a_respondent = respondent[i];
    behaviour[i] ~ normal(alpha + respon_int[a_respondent] + beta_prevalence * prevalence[i], sigma_bar); 
  }
  //Priors
  respon_int ~ normal(0,sigma_resp);
  beta_prevalence ~ normal(0,2);
  sigma_bar ~ normal(0,2);
  alpha ~ normal(0,2);
  //Hyperpriors
  sigma_resp ~ cauchy(0,2);
}

Here is the generated quantities block where I’m attempting to simulate data:

generated quantities {
  real y_rep; 
  for (i in 1:N) {
    y_rep[i] = normal_rng(alpha + respon_int[respondent[i]] + beta_prevalence * prevalence[i], sigma_bar);
  }
}

Any help or guidance would be very much appreciated.

Also, I’m very new to stan (and Bayesian and R and hierarchical modelling) and indexing seems to always stump me. Does anyone have any good sources of literature/tutorials for coming to syntax for indexing?

Shouldn’t this be

real y_rep[N];

?

3 Likes

Yes, that seems to work. Thanks, @maxbiostat. Marked as solution!