# Combine output of two groups in generated quantities

Hi! I used multilevel model to calculate parameters for gamma distribution in a semiparametric survival curve. The output I wanted to fit was time-to-event (infectious period) ~ gamma(shape, rate). I have right censored and observed period (_obs, _cens are assigned to parameters’ name). I managed to run it using two for loop for censored and observed data. now I wanted to combine them and extracted time-to-event of each group (define as isolated[I]).

data{
int<lower=0> Total;
int<lower=0> M; // data length observed
int<lower=0> N; // data length censored
int<lower=0> Niso; // total number of isolator
real inf_obs[M];
real inf_cens[N];
int isol_obs[M];
int isol_cens[N];
}

parameters{
real<lower=0> alpha; // log recovery rate
real<lower=0> beta;
real<lower=0> zalpha_obs[M];
real<lower=0> zbeta_obs[M];
real<lower=0> zalpha_cens[N];
real<lower=0> zbeta_cens[N];
real<lower=0> logsigmaA; // variation
real<lower=0> logsigmaB;
}

model{
vector[M] alphaInf_obs;
vector[M] betaInf_obs;
vector[N] alphaInf_cens;
vector[N] betaInf_cens;
alpha ~ lognormal(0,0.05);
beta ~ normal(0.01,0.05);
zalpha_obs ~lognormal(0, 1);
zbeta_obs ~ lognormal(0, 1);
zalpha_cens ~lognormal(0, 1);
zbeta_cens ~ lognormal(0, 1);
logsigmaA ~normal(0,0.09);
logsigmaB ~normal(0.01,0.01);
for ( i in 1:M ) {  //M is observed
alphaInf_obs[i]  = alpha + zalpha_obs[isol_obs[i]]*exp(logsigmaA);
betaInf_obs[i] = exp(beta) + zbeta_obs[isol_obs[i]]*exp(logsigmaB);
target +=  gamma_lpdf(inf_obs[i]| alphaInf_obs[i],betaInf_obs[i]/alphaInf_obs[i]);
}
for ( i in 1:N ) {  //N is observed
alphaInf_cens[i]  = alpha + zalpha_cens[isol_cens[i]]*exp(logsigmaA);;
betaInf_cens[i] = exp(beta) + zbeta_cens[isol_cens[i]]*exp(logsigmaB);
target+= log(1-gamma_cdf(inf_cens[i],alphaInf_cens[i],betaInf_cens[i]/alphaInf_cens[i]));
}
}

generated quantities{
vector[M] log_lik_obs;
vector[N] log_lik_cens;
vector[M] alphaInf_obs;
vector[M] betaInf_obs;
vector[N] alphaInf_cens;
vector[N] betaInf_cens;
for ( i in 1:M ) {  //M is observed
alphaInf_obs[i]  = alpha + zalpha_obs[isol_obs[i]]*exp(logsigmaA) ;
betaInf_obs[i] = exp(beta) + zbeta_obs[isol_obs[i]]*exp(logsigmaB) ;
log_lik_obs[i] = gamma_lpdf(inf_obs[i]| alphaInf_obs[i],betaInf_obs[i]/alphaInf_obs[i]) ;
}
for ( i in 1:N ) {  //M is observed
alphaInf_cens[i]  = alpha + zalpha_cens[isol_cens[i]]*exp(logsigmaA) ;
betaInf_cens[i] = exp(beta) + zbeta_cens[isol_cens[i]]*exp(logsigmaB) ;
log_lik_cens[i] = log(1-gamma_cdf(inf_cens[i],alphaInf_cens[i],betaInf_cens[i]/alphaInf_cens[i]));
}
}

In generated quantities, I tried creating output of each group (isolator) by creating new variable and assigned group effect. but I got error since the beginning and i am also don’t know how to do it correctly.
Mainly, I want to extract result based on group that isolator as it assigned in function [isol_obs[I]] and also combine output of censored and observed together.

generated quantities{
real alphaall[Niso]
alphaOB = alpha + zalpha_obs*logsigmaA;
betaOB= beta+zbeta_obs*logsigmaB;
alphaC = alpha + zalpha_cens*logsigmaA;
betaOB= beta+zbeta_cens*logsigmaB;

My data is simply infectious period (1-400 hours), censored status(0,1), and isolator groups (1-40) where multiple infectious period are in a isolator.