# https://github.com/stan-dev/example-models/tree/d09a5f38d5fc12f9e9d08a1203429d7e49334e72/bugs_examples/vol1/rats # # http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/Vol1.pdf # Page 3: Rats data { int N; int TT; // originally T, but now reserved word real x[TT]; real y[N,TT]; real xbar; } parameters { real alpha[N]; real beta[N]; real mu_alpha; real mu_beta; // beta.c in original bugs model real sigmasq_y; real sigmasq_alpha; real sigmasq_beta; } transformed parameters { real sigma_y; // sigma in original bugs model real sigma_alpha; real sigma_beta; sigma_y <- sqrt(sigmasq_y); sigma_alpha <- sqrt(sigmasq_alpha); sigma_beta <- sqrt(sigmasq_beta); } model { mu_alpha ~ normal(0, 100); mu_beta ~ normal(0, 100); sigmasq_y ~ inv_gamma(0.001, 0.001); sigmasq_alpha ~ inv_gamma(0.001, 0.001); sigmasq_beta ~ inv_gamma(0.001, 0.001); alpha ~ normal(mu_alpha, sigma_alpha); // vectorized beta ~ normal(mu_beta, sigma_beta); // vectorized for (n in 1:N) for (t in 1:TT) y[n,t] ~ normal(alpha[n] + beta[n] * (x[t] - xbar), sigma_y); } generated quantities { real alpha0; alpha0 <- mu_alpha - xbar * mu_beta; }