Schools: ranking schoolexamination results using multivariate hierarcical models
This example concerns the examination results of 1978 pupils taught at 38 different schools. For each pupil the data record an exam score Y, a London Reading Test score LRT taken at intake, a verbal reasoning band VR, and the pupil's Gender. Two further variables describe the school rather than the pupil: School.gender codes the intake as all girls, all boys or mixed, and School.denom the school's denomination. The categorical variables arrive already expanded into indicator columns with one level held back as the baseline, which is why VR and School.gender occupy two columns each and School.denom three. The practical question is how to rank the 38 schools on exam performance once differences in the ability of the pupils they admit have been accounted for, which is exactly the sort of comparison a raw league table gets wrong.
The model is a multivariate hierarchical regression. Each school gets its own intercept and its own slopes on LRT and on the first verbal-reasoning indicator, and those three school-level coefficients are drawn jointly from a trivariate normal distribution whose mean vector gamma and precision matrix T are themselves estimated: gamma is given a multivariate normal prior and T a Wishart prior with 3 degrees of freedom and scale matrix R. Modelling the three coefficients jointly rather than separately lets the data say how a school's baseline level and its sensitivity to intake ability are correlated. The remaining eight covariate effects are common to all schools and are given vague independent normal priors.
The second unusual feature is that the residual precision is not constant. Rather than a single tau, the model lets the log precision of a pupil's score depend linearly on that pupil's intake score, so that pupils entering with low reading scores can be more variable in outcome than those entering with high ones:
\[\begin{aligned} Y_p &\sim \text{Normal}(\mu_p,\ \tau_p) \\ \log \tau_p &= \theta + \phi \, \mathrm{LRT}_p \\ \alpha_j &\sim \text{MVNormal}(\gamma,\ T), \qquad j = 1, \ldots, 38 \end{aligned}\]
following the BUGS convention in which the second argument of a normal distribution is the precision, the inverse of the variance. The derived quantities min.var and max.var evaluate the implied residual variance at the lowest and highest LRT scores in the data, -34.6193 and 37.3807, which is the readable way to report how much the variance actually changes across the range. See Volume 2 of the classic BUGS examples and the OpenBUGS Schools page for the original write-up.
The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_2.schools, which ships with the package.
Model
using JuliaBUGS
example = JuliaBUGS.BUGSExamples.VOLUME_2.schools
example.model_defquote
for p = 1:N
Y[p] ~ dnorm(mu[p], tau[p])
mu[p] = alpha[school[p], 1] + alpha[school[p], 2] * LRT[p] + alpha[school[p], 3] * VR[p, 1] + beta[1] * LRT2[p] + beta[2] * VR[p, 2] + beta[3] * Gender[p] + beta[4] * var"School.gender"[p, 1] + beta[5] * var"School.gender"[p, 2] + beta[6] * var"School.denom"[p, 1] + beta[7] * var"School.denom"[p, 2] + beta[8] * var"School.denom"[p, 3]
tau[p] = exp(theta + phi * LRT[p])
sigma2[p] = 1 / tau[p]
LRT2[p] = LRT[p] * LRT[p]
end
var"min.var" = exp(-((theta + phi * -34.6193)))
var"max.var" = exp(-((theta + phi * 37.3807)))
for k = 1:8
beta[k] ~ dnorm(0.0, 0.0001)
end
theta ~ dnorm(0.0, 0.0001)
phi ~ dnorm(0.0, 0.0001)
for j = 1:M
alpha[j, 1:3] ~ dmnorm(gamma[1:3], T[1:3, 1:3])
alpha1[j] = alpha[j, 1]
end
gamma[1:3] ~ dmnorm(mn[1:3], prec[1:3, 1:3])
T[1:3, 1:3] ~ dwish(R[1:3, 1:3], 3)
endThe program as it appears in the original BUGS distribution:
print(example.original_syntax_program)model
{
for(p in 1 : N) {
Y[p] ~ dnorm(mu[p], tau[p])
mu[p] <- alpha[school[p], 1] + alpha[school[p], 2] * LRT[p]
+ alpha[school[p], 3] * VR[p, 1] + beta[1] * LRT2[p]
+ beta[2] * VR[p, 2] + beta[3] * Gender[p]
+ beta[4] * School.gender[p, 1] + beta[5] * School.gender[p, 2]
+ beta[6] * School.denom[p, 1] + beta[7] * School.denom[p, 2]
+ beta[8] * School.denom[p, 3]
log(tau[p]) <- theta + phi * LRT[p]
sigma2[p] <- 1 / tau[p]
LRT2[p] <- LRT[p] * LRT[p]
}
min.var <- exp(-(theta + phi * (-34.6193))) # lowest LRT score = -34.6193
max.var <- exp(-(theta + phi * (37.3807))) # highest LRT score = 37.3807
# Priors for fixed effects:
for (k in 1 : 8) {
beta[k] ~ dnorm(0.0, 0.0001)
}
theta ~ dnorm(0.0, 0.0001)
phi ~ dnorm(0.0, 0.0001)
# Priors for random coefficients:
for (j in 1 : M) {
alpha[j, 1 : 3] ~ dmnorm(gamma[1:3 ], T[1:3 ,1:3 ]);
alpha1[j] <- alpha[j,1]
}
# Hyper-priors:
gamma[1 : 3] ~ dmnorm(mn[1:3 ], prec[1:3 ,1:3 ]);
T[1 : 3, 1 : 3 ] ~ dwish(R[1:3 ,1:3 ], 3)
}Data
example.data(N = 1978, M = 38, mn = [0, 0, 0], prec = [0.0001 0.0 0.0; 0.0 0.0001 0.0; 0.0 0.0 0.0001], R = [0.1 0.005 0.005; 0.005 0.1 0.005; 0.005 0.005 0.1], school = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1 … 34, 34, 35, 35, 36, 37, 37, 37, 37, 38], LRT = [-10.6193, 8.3807, -12.6193, 14.3807, 9.3807, 2.3807, 9.3807, -7.6193, -4.6193, 0.3807 … -3.6193, -4.6193, -13.6193, 15.3807, -9.6193, 20.3807, 23.3807, 11.3807, -6.6193, 15.3807], var"School.gender" = [0 0; 0 0; … ; 0 0; 0 0], var"School.denom" = [0 0 0; 0 0 0; … ; 0 1 0; 0 0 1], Gender = [1, 1, 0, 1, 0, 1, 1, 0, 0, 1 … 1, 1, 1, 1, 1, 0, 0, 0, 0, 1], VR = [0 0; 1 0; … ; 0 0; 1 0], Y = [-0.6058763, 0.9911532, -1.5640939, 0.112787, 0.7515988, 0.5918959, 0.7515988, -1.0849851, -1.8036484, 1.3904106 … -0.684282, -0.046961, -1.799594, 0.829355, -0.684282, 1.227681, 2.422658, 0.431029, -1.560598, 0.032704])Compiling the model
model = JuliaBUGS.compile(example.model_def, example.data, example.inits)BUGSModel (parameters are in transformed (unconstrained) space, with dimension 133):
Model parameters:
T[1:3, 1:3]
alpha[38, 1:3], alpha[37, 1:3], alpha[36, 1:3], alpha[35, 1:3], alpha[34, 1:3], alpha[33, 1:3], alpha[32, 1:3], alpha[31, 1:3], alpha[30, 1:3], alpha[29, 1:3], alpha[28, 1:3], alpha[27, 1:3], alpha[26, 1:3], alpha[25, 1:3], alpha[24, 1:3], alpha[23, 1:3], alpha[22, 1:3], alpha[21, 1:3], alpha[20, 1:3], alpha[19, 1:3], alpha[18, 1:3], alpha[17, 1:3], alpha[16, 1:3], alpha[15, 1:3], alpha[14, 1:3], alpha[13, 1:3], alpha[12, 1:3], alpha[11, 1:3], alpha[10, 1:3], alpha[9, 1:3], alpha[8, 1:3], alpha[7, 1:3], alpha[6, 1:3], alpha[5, 1:3], alpha[4, 1:3], alpha[3, 1:3], alpha[2, 1:3], alpha[1, 1:3]
beta[8], beta[7], beta[6], beta[5], beta[4], beta[3], beta[2], beta[1]
gamma[1:3]
phi
theta
Variable sizes and types:
alpha: size = (38, 3), type = Matrix{Float64}
Gender: size = (1978,), type = Vector{Int64}
School.denom: size = (1978, 3), type = Matrix{Int64}
LRT: size = (1978,), type = Vector{Float64}
gamma: size = (3,), type = Vector{Float64}
prec: size = (3, 3), type = Matrix{Float64}
mn: size = (3,), type = Vector{Int64}
LRT2: size = (1978,), type = Vector{Float64}
VR: size = (1978, 2), type = Matrix{Int64}
school: size = (1978,), type = Vector{Int64}
phi: type = Float64
sigma2: size = (1978,), type = Vector{Float64}
N: type = Int64
M: type = Int64
R: size = (3, 3), type = Matrix{Float64}
mu: size = (1978,), type = Vector{Float64}
School.gender: size = (1978, 2), type = Matrix{Int64}
min.var: type = Float64
Y: size = (1978,), type = Vector{Float64}
tau: size = (1978,), type = Vector{Float64}
alpha1: size = (38,), type = Vector{Float64}
T: size = (3, 3), type = Matrix{Float64}
beta: size = (8,), type = Vector{Float64}
theta: type = Int64
max.var: type = Float64
The example's own initial values are passed in here. Several of these models fail from random starting values, so example.inits is not optional in practice; a second set is available as example.inits_alternative.
See Getting Started for the recipe that takes a compiled model to posterior samples.
Reference results
The posterior summaries published with the original example. A converged chain should reproduce these up to Monte Carlo error.
example.reference_results(var"beta[1]" = (mean = 0.0002589, std = 9.8e-5), var"beta[2]" = (mean = 0.4143, std = 0.0597), var"beta[3]" = (mean = 0.1698, std = 0.04795), var"beta[4]" = (mean = 0.1119, std = 0.132), var"beta[5]" = (mean = 0.05174, std = 0.1048), var"beta[6]" = (mean = -0.2844, std = 0.1853), var"beta[7]" = (mean = 0.1433, std = 0.1049), var"beta[8]" = (mean = -0.1671, std = 0.1792), var"gamma[1]" = (mean = -0.662, std = 0.09236), var"gamma[2]" = (mean = 0.03141, std = 0.01023), var"gamma[3]" = (mean = 0.9507, std = 0.08317), phi = (mean = -0.002685, std = 0.00283), theta = (mean = 0.5801, std = 0.03232))