Hips1: Closed form estimates for each strata

This is the first of four linked examples that build up a cost-effectiveness model for total hip replacement, following Spiegelhalter and Best (2003). A patient who receives a primary prosthesis is followed through a Markov model with S = 5 states over N = 60 annual cycles, separately for each of K = 12 age and sex strata. The five states are: alive with the original prosthesis, death at the time of a revision operation, a successful revision in the current cycle, alive after a revision, and death from other causes. Every input is supplied as fixed data: a set-up cost C0 of 4052 for the primary operation and a further 5290 for each revision, a 1% operative mortality rate lambda.op, a 4% annual re-revision rate rho, a 12-by-60 table lambda of age and sex specific background mortality rates, per-cycle utilities bq of 0.938 while stable against decrements of -0.622 and -0.3387 in the cycle of a fatal or a successful revision, and a 6% annual discount rate for both costs (delta.c) and health benefits (delta.b). The revision hazard is the vector h, one value per strata, taking the four values 0.0022, 0.0016, 0.0017 and 0.0012.

The model contains no stochastic nodes at all. It propagates the marginal state distribution forward analytically, one cycle at a time,

\[\pi_{k,t,s} = \sum_{r=1}^{S} \pi_{k,t-1,r} \, \Lambda_{k,t,r,s}\]

where $\Lambda_{k,t}$ is the transition matrix for strata $k$ at cycle $t$ and the annual revision probability $\gamma_{k,t} = h_k (t-1)$ grows linearly with the years since the operation. Discounted costs, life years and QALYs are then exact expectations, obtained by weighting the state distribution at each cycle by the per-state cost and benefit vectors c, bl and bq. One evaluation of the model gives the answer with no simulation error, which is why the example carries no reference posterior summaries.

The final block averages the strata-specific results over p.strata, the distribution of hip replacements across the 12 strata, to give mean.C, mean.BL and mean.BQ along with the matching standard deviations. Those standard deviations describe how much costs and benefits vary between strata, that is, case mix; they are not uncertainty about a parameter, because at this stage the model has none. The three examples that follow relax this in turn: Hips2 replaces the closed-form expectation with a Monte Carlo simulation of individual patients, Hips3 puts a distribution on the revision hazard, and Hips4 synthesises external trial evidence to compare two prostheses. All four are 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.hips1, which ships with the package.

Model

using JuliaBUGS

example = JuliaBUGS.BUGSExamples.VOLUME_3.hips1
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
    var"mean.C" = inprod(var"p.strata"[:], C[:])
    for k = 1:12
        var"dev.C"[k] = pow(C[k] - var"mean.C", 2)
    end
    var"var.C" = inprod(var"p.strata"[:], var"dev.C"[:])
    var"sd.C" = sqrt(var"var.C")
    var"mean.BL" = inprod(var"p.strata"[:], BL[:])
    for k = 1:12
        var"dev.BL"[k] = pow(BL[k] - var"mean.BL", 2)
    end
    var"var.BL" = inprod(var"p.strata"[:], var"dev.BL"[:])
    var"sd.BL" = sqrt(var"var.BL")
    var"mean.BQ" = inprod(var"p.strata"[:], BQ[:])
    for k = 1:12
        var"dev.BQ"[k] = pow(BQ[k] - var"mean.BQ", 2)
    end
    var"var.BQ" = inprod(var"p.strata"[:], var"dev.BQ"[:])
    var"sd.BQ" = sqrt(var"var.BQ")
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])
            }
        }
    }

    # Mean and sd of costs and benefits over strata
    #######################################

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

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

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

Data

example.data
(N = 60, K = 12, S = 5, rho = 0.04, var"lambda.op" = 0.01, h = [0.0022, 0.0022, 0.0022, 0.0016, 0.0016, 0.0016, 0.0017, 0.0017, 0.0017, 0.0012, 0.0012, 0.0012], 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 0):

  Model parameters:

  Variable sizes and types:
    sd.BL: type = Float64
    bl: size = (5,), type = Vector{Int64}
    gamma: size = (12, 60), type = Matrix{Float64}
    rho: type = Float64
    h: size = (12,), type = Vector{Float64}
    c: size = (5,), type = Vector{Int64}
    sd.BQ: type = 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}
    dev.C: size = (12,), type = Vector{Float64}
    dev.BL: size = (12,), type = Vector{Float64}
    K: type = Int64
    S: type = Int64
    BL: 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
    bqt: size = (12, 60), type = Matrix{Float64}
    BQ: size = (12,), type = Vector{Float64}
    dev.BQ: size = (12,), type = Vector{Float64}
    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
    sd.C: type = 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.