The BUGS project provide a number of example models written in the BUGS modelling language. These models will run in WinBUGS and OpenBUGS, and likely also in JAGS. The Stan wiki provides Stan implementations of these models.
The following sections provide greta implementations of some of these example models, alongside the BUGS code from WinBUGS examples volume 2 (pdf) and Stan code and an R version of the data from the Stan example models wiki.
Air analyses reported respiratory illness versus exposure to nitrogen dioxide in 103 children. The parameters alpha
, beta
and sigma2
are known in advance, and the data are grouped into three categories.
See WinBUGS examples volume 2 (pdf) for details.
y <- c(21, 20, 15)
n <- c(48, 34, 21)
Z <- c(10, 30, 50)
alpha <- 4.48
beta <- 0.76
sigma2 <- 81.14
sigma <- sqrt(sigma2)
tau <- 1 / sigma2
J <- 3
theta = normal(0, 32, dim = 2)
mu <- alpha + beta * Z
X = normal(mu, sigma)
p <- ilogit(theta[1] + theta[2] * X)
distribution(y) = binomial(n, p)
for(j in 1 : J) {
y[j] ~ dbin(p[j], n[j])
logit(p[j]) <- theta[1] + theta[2] * X[j]
X[j] ~ dnorm(mu[j], tau)
mu[j] <- alpha + beta * Z[j]
}
theta[1] ~ dnorm(0.0, 0.001)
theta[2] ~ dnorm(0.0, 0.001)
data {
real alpha;
real beta;
real<lower=0> sigma2;
int<lower=0> J;
int y[J];
vector[J] Z;
int n[J];
}
transformed data {
real<lower=0> sigma;
sigma <- sqrt(sigma2);
}
parameters {
real theta1;
real theta2;
vector[J] X;
}
model {
real p[J];
theta1 ~ normal(0, 32); // 32^2 = 1024
theta2 ~ normal(0, 32);
X ~ normal(alpha + beta * Z, sigma);
y ~ binomial_logit(n, theta1 + theta2 * X);
}
Beetles considers dose-response data from an experiment applying carbon disulphide to 8 beetles. The original example compares three different link functions; the logit, probit and complementary log-log. Here, only the code for the logit link is shown. You can implement the other two link functions in greta by changing ilogit
to iprobit
or icloglog
.
See WinBUGS examples volume 2 (pdf) for details.
x <- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113, 1.8369, 1.8610, 1.8839)
n <- c(59, 60, 62, 56, 63, 59, 62, 60)
r <- c(6, 13, 18, 28, 52, 53, 61, 60)
N <- 8
alpha_star = normal(0, 32)
beta = normal(0, 32)
p <- ilogit(alpha_star + beta * (x - mean(x)))
distribution(r) = binomial(n, p)
alpha <- alpha_star - beta * mean(x)
rhat <- p * n
for( i in 1 : N ) {
r[i] ~ dbin(p[i],n[i])
logit(p[i]) <- alpha.star + beta * (x[i] - mean(x[]))
rhat[i] <- n[i] * p[i]
culmative.r[i] <- culmative(r[i], r[i])
}
alpha <- alpha.star - beta * mean(x[])
beta ~ dnorm(0.0,0.001)
alpha.star ~ dnorm(0.0,0.001)
data {
int<lower=0> N;
int<lower=0> n[N];
int<lower=0> r[N];
vector[N] x;
}
transformed data {
vector[N] centered_x;
real mean_x;
mean_x <- mean(x);
centered_x <- x - mean_x;
}
parameters {
real alpha_star;
real beta;
}
transformed parameters {
vector[N] m;
m <- alpha_star + beta * centered_x;
}
model {
alpha_star ~ normal(0.0, 1.0E4);
beta ~ normal(0.0, 1.0E4);
r ~ binomial_logit(n, m);
}
generated quantities {
real alpha;
real p[N];
real llike[N];
real rhat[N];
for (i in 1:N) {
p[i] <- inv_logit(m[i]);
llike[i] <- r[i]*log(p[i]) + (n[i]-r[i])*log(1-p[i]);
rhat[i] <- p[i]*n[i]; // fitted values
}
alpha <- alpha_star - beta*mean_x;
}