Hepatitis: a normal hierarchical model with measurement error (basic)
Spiegelhalter et al. (1996) follow 106 children after hepatitis B vaccination, tracking how the anti-Hb titre each child mounts decays over the months that follow. Titres and measurement times are both recorded on a log scale. The response Y is a 106-by-3 matrix: 76 children were measured three times and the remaining 30 twice, so the third column holds 30 missing entries and there are 288 observed titres in all. The matrix t gives the matching log measurement times, with the value 10 standing in wherever the corresponding titre is missing. A single baseline covariate y0, the log titre recorded at vaccination, is available for every child. The question is how quickly the titre falls away after vaccination and whether a child's baseline level predicts the level thereafter.
The model is a random-effects linear growth curve. Each child gets an individual intercept $\alpha_i$ and an individual slope $\beta_i$ against log time centred at 6.5, and those child-level coefficients are drawn from common population normals, so a child measured only twice still borrows strength from the rest of the sample. The baseline covariate enters through a single population-level coefficient $\gamma$ on the centred log baseline titre. Following the BUGS convention the second argument of dnorm is a precision, not a variance, so $\tau$ is the inverse of the residual variance and sigma is recovered from it as $1/\sqrt{\tau}$.
\[\begin{aligned} Y_{ij} &\sim \text{Normal}\left(\alpha_i + \beta_i (t_{ij} - 6.5) + \gamma\,(y_{0i} - \bar{y}_0),\ \tau\right) \\ \alpha_i &\sim \text{Normal}(\alpha_0, \tau_\alpha) \\ \beta_i &\sim \text{Normal}(\beta_0, \tau_\beta) \end{aligned}\]
The population parameters $\alpha_0$, $\tau_\alpha$, $\beta_0$, $\tau_\beta$, $\gamma$, and $\tau$ are given independent very flat conjugate priors. No correlation between $\alpha_i$ and $\beta_i$ is modelled here; the Birats example in Volume 2 shows the version that does. The baseline reading y0 is itself a titre measurement and so carries the same measurement error as the readings it is used to predict, which biases $\gamma$ towards zero when it is treated as a known covariate. Hepatitis ME is the same study fitted with that error modelled explicitly. This example is part of Volume 3 of the classic BUGS examples; see the MultiBUGS examples page and the OpenBUGS Hepatitis page, which carries both versions.
The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_3.hepatitis, which ships with the package.
Model
using JuliaBUGS
example = JuliaBUGS.BUGSExamples.VOLUME_3.hepatitis
example.model_defquote
for i = 1:N
for j = 1:T
Y[i, j] ~ dnorm(mu[i, j], tau)
mu[i, j] = alpha[i] + beta[i] * (t[i, j] - 6.5) + gamma * (y0[i] - mean(y0[:]))
end
alpha[i] ~ dnorm(alpha0, tau_alpha)
beta[i] ~ dnorm(beta0, tau_beta)
end
tau ~ dgamma(0.001, 0.001)
sigma = 1 / sqrt(tau)
alpha0 ~ dnorm(0.0, 1.0e-6)
tau_alpha ~ dgamma(0.001, 0.001)
beta0 ~ dnorm(0.0, 1.0e-6)
tau_beta ~ dgamma(0.001, 0.001)
gamma ~ dnorm(0.0, 1.0e-6)
endThe program as it appears in the original BUGS distribution:
print(example.original_syntax_program)model
{
for( i in 1 : N ) {
for( j in 1 : T ) {
Y[i , j] ~ dnorm(mu[i , j],tau)
mu[i , j] <- alpha[i] + beta[i] * (t[i,j] - 6.5) + gamma * (y0[i] - mean(y0[]))
}
alpha[i] ~ dnorm(alpha0,tau.alpha)
beta[i] ~ dnorm(beta0,tau.beta)
}
tau ~ dgamma(0.001,0.001)
sigma <- 1 / sqrt(tau)
alpha0 ~ dnorm(0.0,1.0E-6)
tau.alpha ~ dgamma(0.001,0.001)
beta0 ~ dnorm(0.0,1.0E-6)
tau.beta ~ dgamma(0.001,0.001)
gamma ~ dnorm(0.0,1.0E-6)
}Data
example.data(N = 106, T = 3, Y = Union{Missing, Float64}[4.997 8.028 missing; 6.83 4.905 6.295; … ; 7.07326971745971 7.12608727329912 5.01063529409626; 9.08636319215647 7.9359451033537 missing], t = [6.541 6.963 10.0; 5.8406416573734 6.52941883826223 6.98193467715639; … ; 5.8171111599632 6.51174532964473 6.93634273583405; 5.85793315448346 6.92264389147589 10.0], y0 = [8.613, 7.105, 6.896, 5.63835466933375, 6.35088571671474, 5.03043792139244, 5.8664680569333, 5.73979291217923, 7.07834157955767, 8.56655462095396 … 7.58984151218266, 6.39024066706535, 6.04025471127741, 2.77258872223978, 7.62948991639399, 7.85476918349913, 7.09007683577609, 5.50125821054473, 6.98100574072173, 5.50533153593236])Compiling the model
model = JuliaBUGS.compile(example.model_def, example.data, example.inits)BUGSModel (parameters are in transformed (unconstrained) space, with dimension 218):
Model parameters:
tau_beta
alpha[106], alpha[105], alpha[104], alpha[103], alpha[102], alpha[101], alpha[100], alpha[99], alpha[98], alpha[97], alpha[96], alpha[95], alpha[94], alpha[93], alpha[92], alpha[91], alpha[90], alpha[89], alpha[88], alpha[87], alpha[86], alpha[85], alpha[84], alpha[83], alpha[82], alpha[81], alpha[80], alpha[79], alpha[78], alpha[77], alpha[76], alpha[75], alpha[74], alpha[73], alpha[72], alpha[71], alpha[70], alpha[69], alpha[68], alpha[67], alpha[66], alpha[65], alpha[64], alpha[63], alpha[62], alpha[61], alpha[60], alpha[59], alpha[58], alpha[57], alpha[56], alpha[55], alpha[54], alpha[53], alpha[52], alpha[51], alpha[50], alpha[49], alpha[48], alpha[47], alpha[46], alpha[45], alpha[44], alpha[43], alpha[42], alpha[41], alpha[40], alpha[39], alpha[38], alpha[37], alpha[36], alpha[35], alpha[34], alpha[33], alpha[32], alpha[31], alpha[30], alpha[29], alpha[28], alpha[27], alpha[26], alpha[25], alpha[24], alpha[23], alpha[22], alpha[21], alpha[20], alpha[19], alpha[18], alpha[17], alpha[16], alpha[15], alpha[14], alpha[13], alpha[12], alpha[11], alpha[10], alpha[9], alpha[8], alpha[7], alpha[6], alpha[5], alpha[4], alpha[3], alpha[2], alpha[1]
beta[106], beta[105], beta[104], beta[103], beta[102], beta[101], beta[100], beta[99], beta[98], beta[97], beta[96], beta[95], beta[94], beta[93], beta[92], beta[91], beta[90], beta[89], beta[88], beta[87], beta[86], beta[85], beta[84], beta[83], beta[82], beta[81], beta[80], beta[79], beta[78], beta[77], beta[76], beta[75], beta[74], beta[73], beta[72], beta[71], beta[70], beta[69], beta[68], beta[67], beta[66], beta[65], beta[64], beta[63], beta[62], beta[61], beta[60], beta[59], beta[58], beta[57], beta[56], beta[55], beta[54], beta[53], beta[52], beta[51], beta[50], beta[49], beta[48], beta[47], beta[46], beta[45], beta[44], beta[43], beta[42], beta[41], beta[40], beta[39], beta[38], beta[37], beta[36], beta[35], beta[34], beta[33], beta[32], beta[31], beta[30], beta[29], beta[28], beta[27], beta[26], beta[25], beta[24], beta[23], beta[22], beta[21], beta[20], beta[19], beta[18], beta[17], beta[16], beta[15], beta[14], beta[13], beta[12], beta[11], beta[10], beta[9], beta[8], beta[7], beta[6], beta[5], beta[4], beta[3], beta[2], beta[1]
gamma
beta0
Y[106, 3], Y[104, 3], Y[103, 3], Y[96, 3], Y[89, 3], Y[88, 3], Y[85, 3], Y[83, 3], Y[80, 3], Y[78, 3], Y[77, 3], Y[71, 3], Y[70, 3], Y[64, 3], Y[63, 3], Y[54, 3], Y[50, 3], Y[46, 3], Y[42, 3], Y[41, 3], Y[34, 3], Y[32, 3], Y[21, 3], Y[20, 3], Y[18, 3], Y[10, 3], Y[8, 3], Y[7, 3], Y[3, 3], Y[1, 3]
tau
alpha0
tau_alpha
Variable sizes and types:
alpha: size = (106,), type = Vector{Float64}
gamma: type = Float64
tau_beta: type = Float64
beta0: type = Float64
y0: size = (106,), type = Vector{Float64}
sigma: type = Float64
alpha0: type = Float64
N: type = Int64
mu: size = (106, 3), type = Matrix{Float64}
Y: size = (106, 3), type = Matrix{Float64}
tau: type = Float64
T: type = Int64
beta: size = (106,), type = Vector{Float64}
t: size = (106, 3), type = Matrix{Float64}
tau_alpha: 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(alpha0 = (mean = 6.138, std = 0.1501), beta0 = (mean = -1.057, std = 0.1303), gamma = (mean = 0.6716, std = 0.08469), sigma = (mean = 1.002, std = 0.0549))