functions { /* * Alternative to poisson_log_rng() that avoids potential numerical problems during warmup */ int poisson_log_safe_rng(real eta) { real pois_rate = exp(eta); if (pois_rate >= exp(20.79)) return -9; return poisson_rng(pois_rate); } } data { int N; int complaints[N]; vector[N] traps; } parameters { real alpha; real beta; } model { // weakly informative priors: // we expect negative slope on traps and a positive intercept, // but we will allow ourselves to be wrong beta ~ normal(-0.25, 1); alpha ~ normal(log(4), 1); // poisson_log(eta) is more efficient and stable alternative to poisson(exp(eta)) complaints ~ poisson_log(alpha + beta * traps); } generated quantities { // sample predicted values from the model for posterior predictive checks int y_rep[N]; for (n in 1:N) { real eta_n = alpha + beta * traps[n]; y_rep[n] = poisson_log_safe_rng(eta_n); } }