Hi,

I am very new to Stan, and I would really appreciate some guidance and advice on modelling a problem. I have count data from three underlying sources. I assume each source can be modelled as a negative binomial distribution NB(r, p), where r is the number of failures until the experiment is stopped and p is the probability of a failure. So, my model is:

y \sim NB(r_1, p_1) + NB(r_2 p_2) + NB(r_3, p_3)

and I’d like to estimate the parameters of the NB distributions given the data y. But I thought this model is too complicated to solve even with Stan (please correct me if I’m mistaken). So, I make a simplifying assumption that p_1 = p_2 = p_3 = p, and so my model is now:

y \sim NB(r_1 + r_2 + r_3, p)

I also have some additional information I can leverage. For some of the y, the data only comes from either one or two sources, such that:

y_X \sim NB(r_1, p)

y_Y \sim NB(r_1 + r_2, p)

y_Z \sim NB(r_1 + r_2 + r_3, p)

and I’m given y_X, y_Y, and y_Z. I also know that r_1 < r_2 << r_3. Hopefully, this description is clear. I’ve constructed the model below:

```
data {
int<lower=0> NX;
int<lower=0> yX[NX];
int<lower=0> NY;
int<lower=0> yY[NY];
int<lower=0> NZ;
int<lower=0> yZ[NZ];
}
parameters {
positive_ordered[3] r;
real<lower=0.0, upper=1.0> p;
}
model {
real muX;
real muZ;
real muY;
real sigmaSqX;
real sigmaSqY;
real sigmaSqZ;
real phiX;
real phiY;
real phiZ;
muX = ((r[1] * (1 - p)) / p);
muY = (((r[1] + r[2]) * (1 - p)) / p);
muZ = ((((r[1] + r[2]) + r[3]) * (1 - p)) / p);
sigmaSqX = (((r[1] * (1 - p)) / p) ^ 2);
sigmaSqY = ((((r[1] + r[2]) * (1 - p)) / p) ^ 2);
sigmaSqZ = (((((r[1] + r[2]) + r[3]) * (1 - p)) / p) ^ 2);
phiX = (muX ^ (2 / (sigmaSqX - muX)));
phiZ = (muZ ^ (2 / (sigmaSqZ - muZ)));
phiY = (muY ^ (2 / (sigmaSqY - muY)));
p ~ beta(0.001,10);
r[1] ~ gamma(0.01,0.01);
r[2] ~ gamma(0.01,0.01);
r[3] ~ gamma(5,10);
yX ~ neg_binomial_2(muX, phiX);
yY ~ neg_binomial_2(muY, phiY);
yZ ~ neg_binomial_2(muZ, phiZ);
}
```

Everything seems to work fine, and I’m getting results that look right. I’m wondering though if my Stan implementation can be improved.

Thank you for any feedback, advice or suggestions,

-Larry.