How to write the stan code with a parameter which follows a negative binomial distribution

Can you please help me by correcting my model or finding me some tutorials, thank you!
YunnanSingleVirus.stan (5.1 KB)

// Per week
for (w in 1 : Nw) {
m_ILIw[w] = 0;
for (i in ((w - 1) * 7 + 1) : (w * 7)) {
m_ILIw[w] = m_ILIw[w] + m_ILI[i];

}

}
}
model {

//priors
E0 ~ uniform(1, Y_C[1] * 10);
betac ~ normal(1.25, 1);
real R1 = betac / gamma;
Rt_ILI[1] ~ normal(R1, 0.5); // mean values of 2 ~ 3.5 are ok
for (t in 2 : Ni) {
Rt_ILI[t] ~ normal(Rt_ILI[t - 1], 0.5);
}

phiC ~ normal(100, 10);
phiILI ~ normal(100, 10);
// phi ~ uniform(0, 100);
rhoa ~ beta(1, 5);
// asc_ILI ~ exponential(10);

//sampling distribution
Y_C ~ neg_binomial_2(m_c * rhoc, phiC);

for (w in 1 : Nw) {
target += neg_binomial_2_lpmf(Y_ILI | m_ILIw * rhoa, phiILI);

nilip[w] ~ binomial(nili[w], Y_ILI[w] / Z_ILI[w]);

}
}
generated quantities {
real Rc = betac / gamma;
real log10_rhoa = log10(rhoa);
vector[Nt] IAR = C / N; // cumulative infection attack rate

vector[Nt] beta_t;
for (t in 1 : Nt) {
if (t <= days1[Nt1]) {
beta_t[t] = betac;
} else {
beta_t[t] = beta_t_ILI[t - days1[Nt1]];
}
}

vector[Nt] Re;
for (t in 1 : Nt) {
Re[t] = beta_t[t] / gamma * S[t] / N;
}

array[Nc] int ppd_cases = neg_binomial_2_rng(m_c * rhoc, phiC);

array[Ni] real ppd_ILI;
for (t in 1 : Ni) {
ppd_ILI[t] = neg_binomial_2_rng(m_ILI[t] / 10 * rhoa, phiILI) * 10;
}
}

You need either to approximate it by a continuous variable (e.g., Y_C = m_c * rhoc) or to reformulate your model as the mixture model (you may check the stan manual on how to implement integer counts as a part of your model)

Thank you for your reply, what I am trying to do is how to define a support variable Y_ILI = neg_binomial_2(m_ILIw * rhoa, phiILI). Where in stan the parameter can not be int, but in neg_binomial_2 the Y_ILI must be int. How to solve this? Sorry I am new to the R and stan, so the question maybe stupid.

I think this may be helpful Integer parameters

In my opinion, you may try to appriximate the negative binomial by some continuous distribution. I am not sure, but to have ILI as integers can be not extremely important (sometimes I use this approximation in my work, see Figure 2 in https://journals.sagepub.com/doi/full/10.1177/0962280217747054)

Thank you, I have read about it, it really helps!

1 Like