# Using beta binomial with size of the population provided as a "real"

Hi,

I’m trying to convert a model I fitted in JAGS into STAN.
In both JAGS and STAN, the binomial or betabinomial functions need “integer” values for the population size.
However, in my case, the population size considered is provided by another part of the model as a “real” value (i.e. a population dynamic model … not shown here).

In JAGS I used the following code:

``````## JAGS
prop ~ dunif(0,1)
phi <- 100
for (sID in 1:NsID){
propWerr[sID] ~ dbeta(prop * phi, (1 - prop) * phi)
RpredN[sID] <- round(predN[year[sID], 1])
obsN[sID] ~ dbin(propWerr[sID], RpredN[sID])
}
``````

However, in STAN I cannot use the round function to convert my population value provided as a “real” into an “integer” value before using it into the binomial function.
To make the conversion, I used an inefficient trick (see below), searching for the closest integer and providing the latter to the binomial function. In fact, as the formulation used in JAGS correspond to a betabinomial, I used the specific “betabinomial” function in STAN.

``````## STAN
prop ~ uniform(0,1);
int phi = 100;
for (sID in 1:NsID){
// ### search for the closest integer ###
real res = -1;
int RpredN = -1;
while(res<0){
RpredN = RpredN+1;
res = RpredN - round(predN[year[sID]]);
}
// ######################################

obsN[sID] ~ beta_binomial(RpredN, prop * phi, (1 - prop) * phi);
}
``````

While my “rounding up” approach seems to do the trick, there’s a good chance it’s going to cause problems during the fitting and I am pretty sure there is a much better/cleaner way to achieve what I want.

Any help would be appreciated.

1 Like

No, Stan doesn’t allow that cast on purpose.

If you have discrete parameters, you gotta integrate them out or something like that. If there’s a generalization for beta-binomials to have non-integer number of trials, that’ll be a different distribution and you’ll need to code that up manually.

I forget where, but I’ve seen people manually code up a generalization of the binomial with real instead of integer trials but I haven’t seen the beta-binomial. It might exist though and if you can find it then, like @bbbales2 said, it could be written in Stan code, it just doesn’t exist as a built-in function.

But I’m curious how the population size is “real” here and not an integer. How can there be fractional members of a population?

1 Like

@jonah The population size is modeled in Stan too (calculated in the transformed parameter section) and the result is a “real”. As all stay in Stan, I cannot convert it into “integer” before providing it to the binomial (or the betabinomial).

If you can direct me to the generalization of the binomial with real it would be greatly appreciated.

Unfortunately I forget where I’ve seen the continuous generalization of the binomial! If I remember I will post it here. A quick google search returned this paper about continuous versions of the poisson and binomial

but I didn’t have time to read through it, so I can’t vouch for it. Maybe it has something useful though.

There’s more stuff on google too that I didn’t have time to go through. E.g., searching for “continuous extension of the binomial distribution”. Unfortunately I can’t guarantee that it will be obvious how to do it for the beta-binomial though!

Oh I see what you mean. If possible, can you share the transformed parameters code that creates the real numbered population size?

Thanks to have searched !
I am a biologist not a statistician so not sure I can write of function in Stan to do what was proposed in the paper you mentioned. :-)

@jonah Unfortunately, I can’t. But you can think about the application of a model like the Ricker’s model.

It might be worth asking on a math/stats forum about a continuous generalization of the beta-binomial. If you find anything then you can definitely bring it back here and we can help you code it up in Stan.

You could also code up a toy version of your model that has the same issue you’re confronting but not your actual code (if you need to keep it private). Having something that people can play with and try out usually helps attract more responses.

See discussion here for more on these generalisations.

2 Likes