Hips3: MC estimates for each strata, allowing for parameter uncertainty in revision hazard, h

Third in the hip-replacement series, this example abandons the patient-level simulation and goes back to the closed-form Markov calculation of Hips1, whose inputs it inherits unchanged with a single exception. That exception is the age and sex specific revision hazard h: the quantity the whole evaluation is most sensitive to, the one that is least well known, and now the only thing in the program that is not a fixed number.

The hazard is given a distribution on the log scale, so that it stays positive:

\[\log h_k \sim \text{Normal}(\log h_{0,k},\ \tau), \qquad h_k = \exp(\log h_k)\]

Following the BUGS convention the second argument of the normal is a precision, not a variance. Here tau is supplied as data at 25, which is a standard deviation of 0.2 on the log-hazard scale, and the prior means logh0 are the logarithms of the four fixed hazards used in the two previous examples: -6.119, -6.438, -6.377 and -6.725 correspond to hazards of 0.0022, 0.0016, 0.0017 and 0.0012. Everything downstream of h is deterministic within an iteration, so each draw of the hazards yields a complete set of exact expected costs, life years and QALYs for all 12 strata.

That separation is the point of the example. The posterior standard deviations of C[k], BL[k] and BQ[k] here reflect uncertainty about the revision hazard alone, uncontaminated by the individual-level variability that Hips2 reports, and they turn out to be negligible for life expectancy, which barely depends on whether a hip is ever revised, but appreciable for cost, which is driven directly by the number of revisions. Alongside these, the model recomputes the case-mix mean and variance across strata at every iteration, weighting by p.strata, so that heterogeneity between strata and uncertainty about the parameters can be reported side by side. Hips4 completes the series by estimating the hazard ratio between two prostheses from trial data rather than assuming it. The example is in Volume 3 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_3.hips3, which ships with the package.

Model

using JuliaBUGS

example = JuliaBUGS.BUGSExamples.VOLUME_3.hips3
example.model_def
quote
    for k = 1:K
        for t = 1:N
            ct[k, t] = inprod(pi[k, t, :], c[:]) / pow(1 + var"delta.c", t - 1)
        end
        C[k] = C0 + sum(ct[k, :])
        for t = 1:N
            blt[k, t] = inprod(pi[k, t, :], bl[:]) / pow(1 + var"delta.b", t - 1)
        end
        BL[k] = sum(blt[k, :])
        for t = 1:N
            bqt[k, t] = inprod(pi[k, t, :], bq[:]) / pow(1 + var"delta.b", t - 1)
        end
        BQ[k] = sum(bqt[k, :])
        for t = 2:N
            Lambda[k, t, 1, 1] = (1 - gamma[k, t]) - lambda[k, t]
            Lambda[k, t, 1, 2] = gamma[k, t] * var"lambda.op"
            Lambda[k, t, 1, 3] = gamma[k, t] * (1 - var"lambda.op")
            Lambda[k, t, 1, 4] = 0
            Lambda[k, t, 1, 5] = lambda[k, t]
            Lambda[k, t, 2, 1] = 0
            Lambda[k, t, 2, 2] = 0
            Lambda[k, t, 2, 3] = 0
            Lambda[k, t, 2, 4] = 0
            Lambda[k, t, 2, 5] = 1
            Lambda[k, t, 3, 1] = 0
            Lambda[k, t, 3, 2] = 0
            Lambda[k, t, 3, 3] = 0
            Lambda[k, t, 3, 4] = 1 - lambda[k, t]
            Lambda[k, t, 3, 5] = lambda[k, t]
            Lambda[k, t, 4, 1] = 0
            Lambda[k, t, 4, 2] = rho * var"lambda.op"
            Lambda[k, t, 4, 3] = rho * (1 - var"lambda.op")
            Lambda[k, t, 4, 4] = (1 - rho) - lambda[k, t]
            Lambda[k, t, 4, 5] = lambda[k, t]
            Lambda[k, t, 5, 1] = 0
            Lambda[k, t, 5, 2] = 0
            Lambda[k, t, 5, 3] = 0
            Lambda[k, t, 5, 4] = 0
            Lambda[k, t, 5, 5] = 1
            gamma[k, t] = h[k] * (t - 1)
        end
        pi[k, 1, 1] = 1 - var"lambda.op"
        pi[k, 1, 2] = 0
        pi[k, 1, 3] = 0
        pi[k, 1, 4] = 0
        pi[k, 1, 5] = var"lambda.op"
        for t = 2:N
            for s = 1:S
                pi[k, t, s] = inprod(pi[k, t - 1, :], Lambda[k, t, :, s])
            end
        end
    end
    for k = 1:K
        logh[k] ~ dnorm(logh0[k], tau)
        h[k] = exp(logh[k])
    end
    var"mean.C" = inprod(var"p.strata"[:], C[:])
    var"mean.BL" = inprod(var"p.strata"[:], BL[:])
    var"mean.BQ" = inprod(var"p.strata"[:], BQ[:])
    for k = 1:12
        var"C.dev"[k] = pow(C[k] - var"mean.C", 2)
        var"BL.dev"[k] = pow(BL[k] - var"mean.BL", 2)
        var"BQ.dev"[k] = pow(BQ[k] - var"mean.BQ", 2)
    end
    var"var.C" = inprod(var"p.strata"[:], var"C.dev"[:])
    var"var.BL" = inprod(var"p.strata"[:], var"BL.dev"[:])
    var"var.BQ" = inprod(var"p.strata"[:], var"BQ.dev"[:])
end

The program as it appears in the original BUGS distribution:

print(example.original_syntax_program)
model {

    for(k in 1 : K) {    # loop over strata

    # Cost and benefit equations in closed form:
    ####################################

        # Costs
        for(t in 1 : N) {
            ct[k, t] <- inprod(pi[k, t, ], c[]) / pow((1 + delta.c), t - 1)
        }
        C[k] <- C0 + sum(ct[k, ])

        # Benefits - life expectancy
        for(t in 1:N) {
            blt[k, t] <- inprod(pi[k, t, ], bl[]) / pow((1 + delta.b), t - 1)
        }
        BL[k] <- sum(blt[k, ])

        # Benefits - QALYs
        for(t in 1 : N) {
            bqt[k, t] <- inprod(pi[k,t, ], bq[]) / pow((1 + delta.b), t - 1)
        }
        BQ[k] <- sum(bqt[k, ])

        # Markov model probabilities:
        #######################

        # Transition matrix
        for(t in 2 : N) {
            Lambda[k, t, 1, 1] <- 1 -  gamma[k, t] - lambda[k, t]
            Lambda[k, t, 1, 2] <- gamma[k, t] * lambda.op
            Lambda[k, t, 1, 3] <- gamma[k, t] * (1 - lambda.op)
            Lambda[k, t, 1, 4] <- 0
            Lambda[k, t, 1, 5] <- lambda[k, t]

            Lambda[k, t, 2, 1] <- 0
            Lambda[k, t, 2, 2] <- 0
            Lambda[k, t, 2, 3] <- 0
            Lambda[k, t, 2, 4] <- 0
            Lambda[k, t, 2, 5] <- 1

            Lambda[k, t, 3, 1] <- 0
            Lambda[k, t, 3, 2] <- 0
            Lambda[k, t, 3, 3] <- 0
            Lambda[k, t, 3, 4] <- 1 -  lambda[k, t]
            Lambda[k, t, 3, 5] <- lambda[k, t]

            Lambda[k, t, 4, 1] <- 0
            Lambda[k, t, 4, 2] <- rho * lambda.op
            Lambda[k, t, 4, 3] <- rho * (1 - lambda.op)
            Lambda[k, t, 4, 4] <- 1 - rho - lambda[k, t]
            Lambda[k, t, 4, 5] <- lambda[k,t]

            Lambda[k, t, 5, 1] <- 0
            Lambda[k, t, 5, 2] <- 0
            Lambda[k, t, 5, 3] <- 0
            Lambda[k, t, 5, 4] <- 0
            Lambda[k, t, 5, 5] <- 1

            gamma[k, t] <- h[k] * (t - 1)
        }

        # Marginal probability of being in each state at time 1
        pi[k,1,1] <- 1 - lambda.op  pi[k,1, 2]<-0     pi[k,1,3] <- 0
        pi[k,1, 4] <- 0   pi[k,1, 5] <- lambda.op

        # Marginal probability of being in each state at time t>1
        for(t in 2:N) {
            for(s in 1:S) {
                pi[k, t, s] <- inprod(pi[k, t - 1, ], Lambda[k, t, , s])
            }
        }
    }

    # age-sex specific revision hazard
    for(k in 1 : K) {
        logh[k] ~ dnorm(logh0[k], tau)
        h[k] <- exp(logh[k])
    }

    # Calculate mean and variance across strata at each iteration
    # (Gives overall mean and variance using approach 1)

    mean.C <- inprod(p.strata[], C[])
    mean.BL <- inprod(p.strata[], BL[])
    mean.BQ <- inprod(p.strata[], BQ[])

    for(k in 1:12) {
        C.dev[k] <- pow(C[k]-mean.C , 2)
        BL.dev[k] <- pow(BL[k]-mean.BL , 2)
        BQ.dev[k] <- pow(BQ[k]-mean.BQ , 2)
    }
    var.C <- inprod(p.strata[], C.dev[])
    var.BL <- inprod(p.strata[], BL.dev[])
    var.BQ <- inprod(p.strata[], BQ.dev[])

}

Data

example.data
(N = 60, K = 12, S = 5, rho = 0.04, var"lambda.op" = 0.01, logh0 = [-6.119, -6.119, -6.119, -6.438, -6.438, -6.438, -6.377, -6.377, -6.377, -6.725, -6.725, -6.725], tau = 25, C0 = 4052, c = [0, 5290, 5290, 0, 0], bl = [1, 0, 1, 1, 0], bq = [0.938, -0.622, -0.3387, 0.938, 0.0], var"delta.c" = 0.06, var"delta.b" = 0.06, var"p.strata" = [0.02, 0.03, 0.07, 0.13, 0.1, 0.0, 0.02, 0.04, 0.1, 0.22, 0.26, 0.01], lambda = [0.0017 0.0017 … 0.1958 0.1958; 0.0044 0.0044 … 0.1958 0.1958; … ; 0.0578 0.0578 … 0.1503 0.1503; 0.1503 0.1503 … 0.1503 0.1503])

Compiling the model

model = JuliaBUGS.compile(example.model_def, example.data)
BUGSModel (parameters are in transformed (unconstrained) space, with dimension 12):

  Model parameters:
    logh[12], logh[11], logh[10], logh[9], logh[8], logh[7], logh[6], logh[5], logh[4], logh[3], logh[2], logh[1]

  Variable sizes and types:
    bl: size = (5,), type = Vector{Int64}
    gamma: size = (12, 60), type = Matrix{Float64}
    rho: type = Float64
    c: size = (5,), type = Vector{Int64}
    h: size = (12,), type = Vector{Float64}
    ct: size = (12, 60), type = Matrix{Float64}
    var.C: type = Float64
    pi: size = (12, 60, 5), type = Array{Float64, 3}
    lambda: size = (12, 60), type = Matrix{Float64}
    K: type = Int64
    S: type = Int64
    BL: size = (12,), type = Vector{Float64}
    BQ.dev: size = (12,), type = Vector{Float64}
    logh: size = (12,), type = Vector{Float64}
    N: type = Int64
    p.strata: size = (12,), type = Vector{Float64}
    blt: size = (12, 60), type = Matrix{Float64}
    C0: type = Int64
    delta.c: type = Float64
    delta.b: type = Float64
    var.BL: type = Float64
    logh0: size = (12,), type = Vector{Float64}
    bqt: size = (12, 60), type = Matrix{Float64}
    BQ: size = (12,), type = Vector{Float64}
    tau: type = Int64
    mean.BL: type = Float64
    bq: size = (5,), type = Vector{Float64}
    mean.BQ: type = Float64
    Lambda: size = (12, 60, 5, 5), type = Array{Float64, 4}
    mean.C: type = Float64
    lambda.op: type = Float64
    BL.dev: size = (12,), type = Vector{Float64}
    C.dev: size = (12,), type = Vector{Float64}
    var.BQ: type = Float64
    C: size = (12,), type = Vector{Float64}

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"BL[1]" = (mean = 14.48, std = 0.005149), var"BL[2]" = (mean = 12.7, std = 0.003636), var"BL[3]" = (mean = 10.34, std = 0.002146), var"BL[4]" = (mean = 7.737, std = 0.0007918), var"BL[5]" = (mean = 5.405, std = 0.0003277), var"BL[6]" = (mean = 4.101, std = 0.0002131), var"BL[7]" = (mean = 15.13, std = 0.005099), var"BL[8]" = (mean = 13.69, std = 0.003885), var"BL[9]" = (mean = 11.65, std = 0.002426), var"BL[10]" = (mean = 9.1, std = 0.0009708), var"BL[11]" = (mean = 6.46, std = 0.0004248), var"BL[12]" = (mean = 4.988, std = 0.0002913), var"BQ[1]" = (mean = 13.17, std = 0.06006), var"BQ[2]" = (mean = 11.59, std = 0.05141), var"BQ[3]" = (mean = 9.468, std = 0.03856), var"BQ[4]" = (mean = 7.157, std = 0.01894), var"BQ[5]" = (mean = 5.018, std = 0.01004), var"BQ[6]" = (mean = 3.813, std = 0.006732), var"BQ[7]" = (mean = 13.82, std = 0.05649), var"BQ[8]" = (mean = 12.53, std = 0.0507), var"BQ[9]" = (mean = 10.69, std = 0.03913), var"BQ[10]" = (mean = 8.43, std = 0.02027), var"BQ[11]" = (mean = 6.004, std = 0.01089), var"BQ[12]" = (mean = 4.64, std = 0.007614), var"C[1]" = (mean = 5790.0, std = 230.0), var"C[2]" = (mean = 5427.0, std = 199.9), var"C[3]" = (mean = 4999.0, std = 152.2), var"C[4]" = (mean = 4471.0, std = 75.77), var"C[5]" = (mean = 4267.0, std = 40.55), var"C[6]" = (mean = 4195.0, std = 27.21), var"C[7]" = (mean = 5634.0, std = 215.4), var"C[8]" = (mean = 5362.0, std = 196.0), var"C[9]" = (mean = 5007.0, std = 153.5), var"C[10]" = (mean = 4494.0, std = 80.64), var"C[11]" = (mean = 4285.0, std = 43.69), var"C[12]" = (mean = 4215.0, std = 30.57), var"mean.BL" = (mean = 8.687, std = 0.0004563), var"mean.BQ" = (mean = 8.015, std = 0.008174), var"mean.C" = (mean = 4609.0, std = 32.31), var"var.BL" = (mean = 6.714, std = 0.003008), var"var.BQ" = (mean = 5.466, std = 0.03841), var"var.C" = (mean = 174500.0, std = 28450.0))