/* * A zero-inflated Poisson-model * * References * * Lambert, D. 1992. Zero-inflated Poisson regression, with an * application to defects in manufacturing. * Technometrics 34: 1-14. * * * Specific characteristics of the Zero-inflated Poisson model coded below: * - both theta and lambda have a trend over time * - repeated measures per nest, thus a random nest effect * is added to the linear predictors for theta * the random effect for lambda has been deleted, because has not seem to be relevant */ data { int N; // sample size int Nnests; // number of nests int y[N]; // counts (number of youngs) vector[N] year; // numeric covariate int nest[N]; // index of nest vector[2] priorpars; } parameters { vector[2] a; // coef of linear pred for theta vector[2] b; // coef of linear pred for lambda real invsigma2; real groupefftheta[Nnests]; // nest effects for theta } transformed parameters{ real sigmatheta; // between nest sd in logit(theta) sigmatheta <- 1/sqrt(invsigma2); } model { //transformed parameters (within model to avoid monitoring) vector[N] theta; // probability of zero youngs vector[N] lambda; // avg. number of youngs // in successful nests { for(ii in 1:N){ // linear predictors with random effect // add fixed and random effects here theta[ii] <- inv_logit(a[1] + a[2]*year[ii] + sigmatheta*groupefftheta[nest[ii]]); lambda[ii] <- exp(b[1] + b[2]*year[ii]); } } // priors a[1]~normal(0,5); a[2]~normal(0,5); b[1]~normal(0,5); b[2]~normal(0,5); invsigma2~gamma(priorpars[1], priorpars[2]); // random effects for(g in 1:Nnests) { groupefftheta[g]~normal(0,1); } // likelihood for (i in 1:N) { if(y[i] == 0) increment_log_prob(log_sum_exp(bernoulli_log(1, theta[i]), bernoulli_log(0, theta[i]) + poisson_log(y[i], lambda[i]))); else increment_log_prob(bernoulli_log(0, theta[i]) + poisson_log(y[i], lambda[i])); } }