functions { int neg_binomial_2_log_safe_rng(real eta, real phi) { real gamma_rate = gamma_rng(phi, phi / exp(eta)); if (gamma_rate >= exp(20.79)) return -9; return poisson_rng(gamma_rate); } } data { int N; int complaints[N]; vector[N] traps; vector[N] super; //vector[N] log_sqfoot; vector[N] sqfoot; } transformed data{ vector[N] log_sqfoot = log(sqfoot); } parameters { real alpha; real beta1; real beta2; real inv_phi; } transformed parameters { real phi = inv(inv_phi); } model { vector[N] eta = alpha + beta1 * traps + beta2 * super + log_sqfoot; alpha ~ normal(log(4), 1); beta1 ~ normal(-0.25, 1); beta2 ~ normal(-0.5, 1); inv_phi ~ normal(0, 1); complaints ~ neg_binomial_2_log(eta, phi); } generated quantities { int y_rep[N]; for (n in 1:N) { real eta_n = alpha + beta1 * traps[n] + beta2 * super[n] + log_sqfoot[n]; y_rep[n] = neg_binomial_2_log_safe_rng(eta_n, phi); } }