How to compute log_lik in Stan file for computing LOOIC

I know Vehtari shows the method that

generated quantities {
  vector[N] log_lik;
  for (n in 1:N)
    log_lik[n] <- bernoulli_logit_log(y[n], beta0 + x[n] * beta);
}

However, now I need something like this:

generated quantities {
  vector[N_1+N_2] log_lik;
  for (n in 1:N_1) {
    log_lik[n] = bernoulli_logit_log(y_1[n], beta0 + x_1[n] * beta);
  }
  for (n in (N_1+1):(N_1+N_2)) {
    log_lik[n] = bernoulli_logit_log(y_2[n], beta0 + x_2[n] * beta);
  }
}

But I got the error like this:

Chain 1: Exception: array[uni,…] index: accessing element out of range. index 101 out of range; expecting index to be between 1 and 100.

N_1 = N_2 = 100 in my model.

So my question is that how can I combine the two log_lik’s into one log_lik :) So I can use it to compute LOOIC.

Thanks!

I think you are close.

Think about how you index y_2

You could explicitly remove N1 from n (in the second loop) so it will take items from 1 to N2.

Thank you man. You are god, you should run for Bayesian president :)

change it to this and it works :)

generated quantities {
  vector[N_1+N_2] log_lik;
  for (n in 1:N_1) {
    log_lik[n] = bernoulli_logit_log(y_1[n], beta0 + x_1[n] * beta);
  }
  for (n in 1:N_2) {
    log_lik[n+N_1] = bernoulli_logit_log(y_2[n], beta0 + x_2[n] * beta);
  }
}