### flock[i] is flock size of group i and is only observed for a subset of flocks (Nspec)
### We want an estimate of total abundance, including those flocks we did not observe
### In the full model, Nspec is estimated based on data, but I set it here as a random number
### that's larger than the number of observed flocks (which is 282)
### Also in the full model, Nmax is chosen arbitrarily, but large enough so it does not truncate Nspec.
### Flock sizes comes from a 0-truncated negative binomial distribution
### anything above Nspec is not part of the population and thus has a fixed size of 0
### this is an attempt to get around using the random 'Nspec' as upper limit for the 'flock' loop
### Essentially, this is a negative binomial hurdle model (I believe)
### Following an example in the BUGS manual, we model the negative binomial explicitly as a Poisson-gamma mixture
### because using the negative binomial directly gave counterintuitive estimates of mu and r
### if you remove the truncation of the Poisson, or if Nspec is fixed rather than random, the model works
model{
#priors
r~dunif(0,100)
mu~dunif(0,100)
Nspec~dunif(283, Nmax)
for (i in 1:Nmax){
Pin[i]<-step( (Nspec+0.1) - i)
flock[i]~dpois(mustar[i,Pin[i]+1]) T(Pin[i],)
mustar[i,1]<-0
mustar[i,2]<-mu * rho[i]
rho[i]~dgamma(r,r)
}
Atot<-sum(flock)
}