Biopsies: discrete variable latent class model
Spiegelhalter and Stovin (1983) studied transplanted hearts that were biopsied more than once in a sitting, and this example fits their data. A total of 414 biopsies were taken across $n_s = 157$ sessions, 57 sessions contributing two biopsies and 100 contributing three, and every biopsy was graded on a four-point ordinal scale for evidence of rejection, running from no rejection up to moderate or severe rejection. The data as supplied record, for each session, how many of its biopsies fell into each of the four grades. The problem is that a biopsy samples only a small part of the heart, so two biopsies from the same session can disagree. What is wanted is the distribution of the underlying rejection state and a quantification of how unreliable a single reading is.
The model treats the true rejection state of each session as an unobserved discrete label, which is what makes this a latent class model. Session $i$ has a true grade, the node the program calls true[i], drawn from a population distribution $p$ over the four classes, and its biopsies are then a multinomial sample of size $n_i$ from the row of a misclassification matrix indexed by that true grade.
\[\begin{aligned} \text{true}_i &\sim \text{Categorical}(p) \\ \text{biopsies}_{i,1:4} &\sim \text{Multinomial}\left(\text{error}[\text{true}_i, \cdot],\ n_i\right) \end{aligned}\]
The structure of the error matrix encodes the substantive assumption that a biopsy can understate the true state but never overstate it: all entries above the diagonal are fixed to zero, so a session whose true grade is 1 always reads grade 1, a session whose true grade is 2 reads either 1 or 2, and so on. Only the lower-triangular entries are free parameters, and each free row is given a flat Dirichlet prior, as is the class distribution $p$ itself. In the shipped data the fixed entries of error are supplied as numbers and the free entries as missing.
The example appears in Volume 2 of the classic BUGS examples; see also the OpenBUGS version of this example.
The model definition, data, initial values, and published reference results shown here all come from JuliaBUGS.BUGSExamples.VOLUME_2.biopsies, which ships with the package.
Model
using JuliaBUGS
example = JuliaBUGS.BUGSExamples.VOLUME_2.biopsies
example.model_defquote
for i = 1:ns
nbiops[i] = sum(biopsies[i, :])
var"true"[i] ~ dcat(p[:])
biopsies[i, 1:4] ~ dmulti(error[var"true"[i], :], nbiops[i])
end
error[2, 1:2] ~ ddirich(prior[1:2])
error[3, 1:3] ~ ddirich(prior[1:3])
error[4, 1:4] ~ ddirich(prior[1:4])
p[1:4] ~ ddirich(prior[:])
endThe program as it appears in the original BUGS distribution:
print(example.original_syntax_program)model
{
for (i in 1 : ns){
nbiops[i] <- sum(biopsies[i, ])
true[i] ~ dcat(p[])
biopsies[i, 1 : 4] ~ dmulti(error[true[i], ], nbiops[i])
}
error[2,1 : 2] ~ ddirich(prior[1 : 2])
error[3,1 : 3] ~ ddirich(prior[1 : 3])
error[4,1 : 4] ~ ddirich(prior[1 : 4])
p[1 : 4] ~ ddirich(prior[]); # prior for p
}Data
example.data(ns = 157, error = Union{Missing, Int64}[1 0 0 0; missing missing 0 0; missing missing missing 0; missing missing missing missing], prior = [1, 1, 1, 1], biopsies = [2 0 0 0; 2 0 0 0; … ; 0 0 0 3; 0 0 0 3])Compiling the model
model = JuliaBUGS.compile(example.model_def, example.data)BUGSModel (parameters are in transformed (unconstrained) space, with dimension 166):
Model parameters:
p[1:4]
true[157], true[156], true[155], true[154], true[153], true[152], true[151], true[150], true[149], true[148], true[147], true[146], true[145], true[144], true[143], true[142], true[141], true[140], true[139], true[138], true[137], true[136], true[135], true[134], true[133], true[132], true[131], true[130], true[129], true[128], true[127], true[126], true[125], true[124], true[123], true[122], true[121], true[120], true[119], true[118], true[117], true[116], true[115], true[114], true[113], true[112], true[111], true[110], true[109], true[108], true[107], true[106], true[105], true[104], true[103], true[102], true[101], true[100], true[99], true[98], true[97], true[96], true[95], true[94], true[93], true[92], true[91], true[90], true[89], true[88], true[87], true[86], true[85], true[84], true[83], true[82], true[81], true[80], true[79], true[78], true[77], true[76], true[75], true[74], true[73], true[72], true[71], true[70], true[69], true[68], true[67], true[66], true[65], true[64], true[63], true[62], true[61], true[60], true[59], true[58], true[57], true[56], true[55], true[54], true[53], true[52], true[51], true[50], true[49], true[48], true[47], true[46], true[45], true[44], true[43], true[42], true[41], true[40], true[39], true[38], true[37], true[36], true[35], true[34], true[33], true[32], true[31], true[30], true[29], true[28], true[27], true[26], true[25], true[24], true[23], true[22], true[21], true[20], true[19], true[18], true[17], true[16], true[15], true[14], true[13], true[12], true[11], true[10], true[9], true[8], true[7], true[6], true[5], true[4], true[3], true[2], true[1]
error[4, 1:4], error[3, 1:3], error[2, 1:2]
Variable sizes and types:
nbiops: size = (157,), type = Vector{Int64}
p: size = (4,), type = Vector{Float64}
biopsies: size = (157, 4), type = Matrix{Int64}
ns: type = Int64
true: size = (157,), type = Vector{Float64}
error: size = (4, 4), type = Matrix{Float64}
prior: size = (4,), type = Vector{Int64}
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"error[2,1]" = (mean = 0.5875, std = 0.06656), var"error[2,2]" = (mean = 0.4125, std = 0.06656), var"error[3,1]" = (mean = 0.3416, std = 0.0449), var"error[3,2]" = (mean = 0.03708, std = 0.01763), var"error[3,3]" = (mean = 0.6213, std = 0.04684), var"error[4,1]" = (mean = 0.0992, std = 0.04295), var"error[4,2]" = (mean = 0.02187, std = 0.02251), var"error[4,3]" = (mean = 0.2055, std = 0.06041), var"error[4,4]" = (mean = 0.6734, std = 0.07332), var"p[1]" = (mean = 0.1527, std = 0.04979), var"p[2]" = (mean = 0.3116, std = 0.05514), var"p[3]" = (mean = 0.3886, std = 0.04336), var"p[4]" = (mean = 0.1471, std = 0.02971))