HazReg.jl: Parametric Hazard-based regression models for survival data

Author
Published

January 29, 2024

1 Models

The HazReg.jl Julia package implements the following parametric hazard-based regression models for (overall) survival data.

These models are fitted using the Julia package Optim (methods included: “NM” (NelderMead), “N” (Newton), “LBFGS” (LBFGS), “CG” (ConjugateGradient), “GD” (GradientDescent)). Thus, the user needs to specify the initial points and to check the convergence of the optimisation step, as usual.

A description of these hazard models is presented below as well as the available baseline hazards.

1.1 General Hazard model

The GH model is formulated in terms of the hazard structure

\[ h(t; \alpha, \beta, \theta, {\bf x}) = h_0\left(t \exp\{\tilde{\bf x}^{\top}\alpha\}; \theta\right) \exp\{{\bf x}^{\top}\beta\}. \]

where \({\bf x}\in{\mathbb R}^p\) are the covariates that affect the hazard level; \(\tilde{\bf x} \in {\mathbb R}^q\) are the covariates the affect the time level (typically \(\tilde{\bf x} \subset {\bf x}\)); \(\alpha \in {\mathbb R}^q\) and \(\beta \in {\mathbb R}^p\) are the regression coefficients; and \(\theta \in \Theta\) is the vector of parameters of the baseline hazard \(h_0(\cdot)\).

This hazard structure leads to an identifiable model as long as the baseline hazard is not a hazard associated to a member of the Weibull family of distributions (Chen and Jewell 2001).

1.2 Accelerated Failure Time (AFT) model

The AFT model is formulated in terms of the hazard structure

\[ h(t; \beta, \theta, {\bf x}) = h_0\left(t \exp\{{\bf x}^{\top}\beta\}; \theta\right) \exp\{{\bf x}^{\top}\beta\}. \] where \({\bf x}\in{\mathbb R}^p\) are the available covariates; \(\beta \in {\mathbb R}^p\) are the regression coefficients; and \(\theta \in \Theta\) is the vector of parameters of the baseline hazard \(h_0(\cdot)\).

1.3 Proportional Hazards (PH) model

The PH model is formulated in terms of the hazard structure

\[ h(t; \beta, \theta, {\bf x}) = h_0\left(t ; \theta\right) \exp\{{\bf x}^{\top}\beta\}. \]

where \({\bf x}\in{\mathbb R}^p\) are the available covariates; \(\beta \in {\mathbb R}^p\) are the regression coefficients; and \(\theta \in \Theta\) is the vector of parameters of the baseline hazard \(h_0(\cdot)\).

1.4 Accelerated Hazards (AH) model

The AH model is formulated in terms of the hazard structure

\[ h(t; \alpha, \theta, \tilde{\bf x}) = h_0\left(t \exp\{\tilde{\bf x}^{\top}\alpha\}; \theta\right) . \]

where \(\tilde{\bf x}\in{\mathbb R}^q\) are the available covariates; \(\alpha \in {\mathbb R}^q\) are the regression coefficients; and \(\theta \in \Theta\) is the vector of parameters of the baseline hazard \(h_0(\cdot)\).

2 Available baseline hazards

The current version of the HazReg.jl Julia package implements the following parametric baseline hazards for the models discussed in the previous section.

All positive parameters are transformed into the real line using a log link (reparameterisation).

3 Illustrative example: Julia code

In this example, we analyse the LeukSurv data set from the R package spBayesSurv. This data set contains information about the survival of acute myeloid leukemia in 1,043 patients.

For the GH model, we consider the hazard level covariates (\({\bf x}\)) age (standardised), sex, wbc (white blood cell count at diagnosis, standardised), and tpi (the Townsend score, standardised); and the time level covariates (\({\bf x}\)) age (standardised), wbc (white blood cell count at diagnosis, standardised), and tpi (the Townsend score, standardised). For the PH, AFT, and AH models, we consider the covariates age (standardised), sex, wbc (white blood cell count at diagnosis, standardised), and tpi (the Townsend score, standardised).

For illustration, we fit the 4 models with both (3-parameter) PGW and (2-parameter) LL baseline hazard. In addition, we fit the GH model with GenGamma, EW, LogNormal, LogLogistic, and Gamma baseline hazards. We compare these models in terms of AIC (BIC can be used as well). We summarise the best selected model with the available tools in this package.

See also:

3.1 Data preparation

Code
using Distributions
using Random
using StatsBase
using Optim
using LinearAlgebra
using SpecialFunctions
using ForwardDiff

  
using HazReg
using Plots
using PrettyTables
using DataFrames
using NamedArrays
using CSV
using Survival

#= Data =#
df = CSV.File("LeukSurv.csv");

#= Design matrix for hazard level effects =#
des = hcat( standardise(df.age), df.sex, standardise(df.wbc), standardise(df.tpi) );

#= Design matrix for time level effects =#
des_t = hcat( standardise(df.age), standardise(df.wbc), standardise(df.tpi) );

#= Vital status =#
status = collect(Bool,(df.cens));

#= Survival times =#
times = df.time/365.25 ;

3.2 Model fit and MLEs

Code
# PGWGH
OPTPGWGH = GHMLE(init = fill(0.0, 3 + size(des_t)[2] + size(des)[2]), times = times,
            status = status, hstr = "GH", dist = "PGW", 
            des = des, des_t = des_t, method = "NM", maxit = 1000)

# PGWAFT
OPTPGWAFT = GHMLE(init = fill(0.0, 3 + size(des)[2]), times = times, 
                  status = status, hstr = "AFT", dist = "PGW", 
                  des = des, des_t = nothing, method = "NM", maxit = 1000)

# PGWPH
OPTPGWPH = GHMLE(init = fill(0.0, 3 +size(des)[2]), times = times, 
                 status = status, hstr = "PH", dist = "PGW", 
                 des = des, des_t = nothing, method = "NM", maxit = 1000)

# PGWAH
OPTPGWAH = GHMLE(init = fill(0.0, 3 + size(des_t)[2] ), times = times, 
                 status = status, hstr = "AH", dist = "PGW", 
                 des_t = des_t, des = nothing, method = "NM", maxit = 1000)


# LLGH
OPTLLGH = GHMLE(init = fill(0.0, 2 + size(des_t)[2] + size(des)[2]), times = times,
                status = status, hstr = "GH", dist = "LogLogistic", 
                des = des, des_t = des_t, method = "NM", maxit = 1000)

# LLAFT
OPTLLAFT = GHMLE(init = fill(0.0, 2 + size(des)[2]), times = times, 
                  status = status, hstr = "AFT", dist = "LogLogistic", 
                  des = des, des_t = nothing, method = "NM", maxit = 1000)

# LLPH
OPTLLPH = GHMLE(init = fill(0.0, 2 + size(des)[2]), times = times, 
                status = status, hstr = "PH", dist = "LogLogistic", 
                des = des, des_t = nothing, method = "NM", maxit = 1000)

# LLAH
OPTLLAH = GHMLE(init = fill(0.0, 2 + size(des_t)[2]), times = times, 
                status = status, hstr = "AH", dist = "LogLogistic", 
                des = nothing, des_t = des_t, method = "NM", maxit = 1000)


# EWGH
OPTEWGH = GHMLE(init = fill(0.0, 3 + size(des_t)[2] + size(des)[2]), times = times, 
                status = status, hstr = "GH", dist = "EW", 
                des = des, des_t = des_t, method = "NM", maxit = 1000)

# GGGH
OPTGGGH = GHMLE(init = fill(0.0, 3 + size(des_t)[2] + size(des)[2]), times = times,                  status = status, hstr = "GH", dist = "GenGamma",
                des = des, des_t = des_t, method = "NM", maxit = 1000)

# LNGH
OPTLNGH = GHMLE(init = fill(0.0, 2 + size(des_t)[2] + size(des)[2]), times = times, 
                status = status, hstr = "GH", dist = "LogNormal", 
                des = des, des_t = des_t, method = "NM", maxit = 1000)

# GGH
OPTGGH = GHMLE(init = fill(0.0, 2 + size(des_t)[2] + size(des)[2]), times = times, 
               status = status, hstr = "GH", dist = "Gamma", 
               des = des, des_t = des_t, method = "N", maxit = 1000)

# MLEs in the original parameterisations
MLEPGWGH = [exp(OPTPGWGH[1].minimizer[j]) for j in 1:3] 
append!(MLEPGWGH, OPTPGWGH[1].minimizer[4:end]) 

MLEEWGH = [exp(OPTEWGH[1].minimizer[j]) for j in 1:3] 
append!(MLEEWGH, OPTEWGH[1].minimizer[4:end]) 

MLEEWGH = [exp(OPTEWGH[1].minimizer[j]) for j in 1:3] 
append!(MLEEWGH, OPTEWGH[1].minimizer[4:end]) 

MLEGGGH = [exp(OPTGGGH[1].minimizer[j]) for j in 1:3] 
append!(MLEGGGH, OPTGGGH[1].minimizer[4:end]) 

MLEGGH = [exp(OPTGGH[1].minimizer[j]) for j in 1:2] 
append!(MLEGGH, OPTGGH[1].minimizer[3:end]) 

MLELNGH = [OPTLNGH[1].minimizer[1], exp(OPTLNGH[1].minimizer[2]), OPTLNGH[1].minimizer[3:end]...]

MLELLGH = [OPTLLGH[1].minimizer[1], exp(OPTLLGH[1].minimizer[2]), OPTLLGH[1].minimizer[3:end]...]

MLES = hcat(MLEPGWGH, MLEEWGH, MLEGGGH, [MLEGGH[1], MLEGGH[2], nothing, MLEGGH[3:end]...],
            [MLELNGH[1], MLELNGH[2], nothing, MLELNGH[3:end]...],[MLELLGH[1], MLELLGH[2], nothing, MLELLGH[3:end]...])

MLES = DataFrame(MLES, :auto)
 
rename!( MLES, ["PGWGH", "EWGH", "GGGH", "GGH", "LNGH", "LLGH"] )

# MLEs for GH models
println(MLES)
10×6 DataFrame
 Row │ PGWGH      EWGH       GGGH        GGH         LNGH       LLGH      
     │ Union…     Union…     Union…      Union…      Union…     Union…    
─────┼────────────────────────────────────────────────────────────────────
   1 │ 0.0950152  0.0106152  0.00658277  0.48821     -0.729543  -0.718369
   2 │ 1.00611    0.232759   1.06396     4.11739     1.98069    1.12585
   3 │ 3.47407    7.64616    0.282329                                     
   4 │ 0.91133    0.868762   1.22694     -0.800448   0.852858   0.828229
   5 │ 0.897534   0.925936   1.19993     -0.510837   0.788362   0.740527
   6 │ 0.413299   0.44666    0.862072    -0.592114   0.369539   0.41986
   7 │ 0.978584   0.956015   1.09682     0.338275    0.95762    0.948681
   8 │ 0.077424   0.0745275  0.0760824   0.147894    0.0743278  0.0720056
   9 │ 0.681224   0.69978    0.80311     0.00433901  0.630381   0.609399
  10 │ 0.302625   0.319423   0.494054    -0.115218   0.286803   0.31509

3.3 Model Comparison

Code
# AIC for models with PGW baseline hazard
AICPGWGH = 2*OPTPGWGH[1].minimum + 2*length(OPTPGWGH[1].minimizer)
AICPGWAFT = 2*OPTPGWAFT[1].minimum + 2*length(OPTPGWAFT[1].minimizer)
AICPGWPH = 2*OPTPGWPH[1].minimum + 2*length(OPTPGWPH[1].minimizer)
AICPGWAH = 2*OPTPGWAH[1].minimum + 2*length(OPTPGWAH[1].minimizer)

# AICs for models with LL baseline hazard
AICLLGH = 2*OPTLLGH[1].minimum + 2*length(OPTLLGH[1].minimizer)
AICLLAFT = 2*OPTLLAFT[1].minimum + 2*length(OPTLLAFT[1].minimizer)
AICLLPH = 2*OPTLLPH[1].minimum + 2*length(OPTLLPH[1].minimizer)
AICLLAH = 2*OPTLLAH[1].minimum + 2*length(OPTLLAH[1].minimizer)

# AICs for GH models with GG, EW, LN, and G hazards
AICGGGH = 2*OPTGGGH[1].minimum + 2*length(OPTGGGH[1].minimizer)
AICEWGH = 2*OPTEWGH[1].minimum + 2*length(OPTEWGH[1].minimizer)
AICLNGH = 2*OPTLNGH[1].minimum + 2*length(OPTLNGH[1].minimizer)
AICGGH = 2*OPTGGH[1].minimum + 2*length(OPTGGH[1].minimizer)



# All AICs
AICs = [AICPGWGH, AICPGWAFT, AICPGWPH, AICPGWAH,
          AICLLGH, AICLLAFT, AICLLPH, AICLLAH,
          AICGGGH, AICEWGH, AICLNGH, AICGGH]

println(AICs)

# Best model: LLGH
argmin(AICs)
[1537.1904839778242, 1539.43006952374, 1586.4352749290651, 1627.5425264516907, 1527.3620024670893, 1529.328819881987, 1580.5159233440302, 1672.4640359064224, 1554.6674846756873, 1534.05811185893, 1533.6282300060163, 1705.9109039251707]
5

3.4 Baseline hazards for GH models

Code
# Fitted baseline hazard functions for GH models
function PGWGHhaz(t::Float64) 
    out = hPGW(t, MLEPGWGH[1], MLEPGWGH[2], MLEPGWGH[3])  
    return out
end

function EWGHhaz(t::Float64) 
    out = hEW(t, MLEEWGH[1], MLEEWGH[2], MLEEWGH[3])  
    return out
end

function GGGHhaz(t::Float64) 
    out = hGenGamma(t, MLEGGGH[1], MLEGGGH[2], MLEGGGH[3])  
    return out
end

function GGHhaz(t::Float64) 
    out = hGamma(t, MLEGGH[1], MLEGGH[2])  
    return out
end

function LNGHhaz(t::Float64) 
    out = hLogNormal(t, MLELNGH[1], MLELNGH[2])  
    return out
end

function LLGHhaz(t::Float64) 
    out = hLogNormal(t, MLELLGH[1], MLELLGH[2])  
    return out
end

# Note that the baseline hazards associated to the top models look similar
plot(t -> PGWGHhaz(t),
      xlabel = "Time (years)", ylabel = "Baseline Hazard", title = "",
    xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue", ylims = (0,4))

plot!(t -> EWGHhaz(t),
   xlabel = "Time (years)", ylabel = "Baseline Hazard", title = "",
  xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
  xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
  xguidefontsize=18, yguidefontsize=18, linewidth=3,
  linecolor = "blue", ylims = (0,4), linestyle=:dash)

plot!(t -> GGGHhaz(t),
   xlabel = "Time (years)", ylabel = "Baseline Hazard", title = "",
  xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
  xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
  xguidefontsize=18, yguidefontsize=18, linewidth=3,
  linecolor = "blue", ylims = (0,4), linestyle=:dot)

plot!(t -> GGHhaz(t),
   xlabel = "Time (years)", ylabel = "Baseline Hazard", title = "",
  xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
  xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
  xguidefontsize=18, yguidefontsize=18, linewidth=3,
  linecolor = "blue", ylims = (0,4), linestyle=:dashdot)

plot!(t -> LNGHhaz(t),
      xlabel = "Time (years)", ylabel = "Baseline Hazard", title = "",
    xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue", ylims = (0,4), linestyle=:dashdotdot)

plot!(t -> LLGHhaz(t),
    xlabel = "Time (years)", ylabel = "Baseline Hazard", title = "",
  xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
  xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
  xguidefontsize=18, yguidefontsize=18, linewidth=3,
  linecolor = "red", ylims = (0,4), linestyle=:solid)
1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4

3.5 Best-model summaries

Code
# MLE in the original parameterisation
MLE = MLELLGH

println(MLE)

# 95% Confidence intervals under the reparameterisation
CI = ConfInt(FUN = OPTLLGH[2], MLE = OPTLLGH[1].minimizer, level = 0.95)

CI = DataFrame(CI, :auto)
 
rename!( CI, ["Lower", "Upper"] )

println(CI)
[-0.7183693784128291, 1.1258507232839738, 0.8282291818977023, 0.7405270729888672, 0.41985953386652336, 0.9486806545482077, 0.07200557173840726, 0.6093993558633369, 0.3150899846867708]
9×2 DataFrame
 Row │ Lower       Upper     
     │ Float64     Float64   
─────┼───────────────────────
   1 │ -0.883692   -0.553047
   2 │  0.0596398   0.177438
   3 │  0.528404    1.12805
   4 │  0.492198    0.988857
   5 │  0.119745    0.719974
   6 │  0.790656    1.10671
   7 │ -0.0579488   0.20196
   8 │  0.446133    0.772666
   9 │  0.149466    0.480714
Code
# Fitted baseline hazard function
plot(t -> LLGHhaz(t),
    xlabel = "Time (years)", ylabel = "Baseline Hazard", title = "Best Model",
  xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
  xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
  xguidefontsize=18, yguidefontsize=18, linewidth=3,
  linecolor = "red", ylims = (0,4), linestyle=:solid)

# Average population survival function and KM estimator

function pop_surv(t::Float64)
  p0 = size(des_t)[2]
  p1 = size(des)[2]
  theta1 = MLE[1]
  theta2 = MLE[2]
  alpha = MLE[3:(2+p0)]
  beta = MLE[(3+p0):(2+p0+p1)]
  x_alpha = des_t * alpha
  x_dif = des * beta - x_alpha
  out = mean( exp.( - chLogLogistic(t*exp.(x_alpha), theta1, theta2).* exp.(x_dif)  )  )
  return out
end


# Kaplan-Meier estimator 
km_fit = fit(KaplanMeier, times, df.cens)

ktimes = sort(unique(times))
ksurvival_probs = km_fit.survival

# Comparison
plot(ktimes, ksurvival_probs,
    xlabel = "Time (years)", ylabel = "Population Survival", title = "Best Model",
  xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
  xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
  xguidefontsize=18, yguidefontsize=18, linewidth=3,
  linecolor = "gray", ylims = (0,1), linestyle=:solid)

plot!(t -> pop_surv(t),
  xlabel = "Time (years)", ylabel = "Population Survival", title = "Best Model",
xlims = (0.0001,maximum(times)),   xticks = 0:1:maximum(times), label = "", 
xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
xguidefontsize=18, yguidefontsize=18, linewidth=3,
linecolor = "black", ylims = (0,1), linestyle=:solid)
1 2 3 4 5 6 7 8 9 10 11 12 13 0.0 0.2 0.4 0.6 0.8 1.0
Code
# Confidence intervals for the survival function based on a normal approximation
# at specific time points t0

# Hessian and asymptotic covariance matrix
HESS = ForwardDiff.hessian(OPTLLGH[2], OPTLLGH[1].minimizer);
Sigma = inv(HESS);

#= 
A "hackish" workaround to a bug in ForwardDiff, which may
produce non-symmetric hessian matrices.
Here, I am replacing the lower diagonal of Sigma by its 
upper diagonal
=#
ps = size(Sigma, 1)
for i in 1:ps-1
    for j in i+1:ps
        Sigma[i, j] = Sigma[j, i]
    end
end


# Reparameterised MLE 
r_MLE = OPTLLGH[1].minimizer;


# The function to obtain approximate CIs based on Monte Carlo simulations 
# from the asymptotic normal distribution of the MLEs
# t0 : time where the confidence interval will be calculated
# level : confidence level
# nmc : number of Monte Carlo iterations

function ConfIntSurv(t0::Float64, level::Float64, nmc::Int64)
    p0 = size(des_t)[2]
    p1 = size(des)[2]
    mc = fill(0.0, nmc)
    function S_par(par::Vector{Float64})
        
        outs = mean( exp.( - chLogLogistic(t0.*exp.(des_t * par[3:(2+p0)]), par[1], par[2]) .*
                                        exp.( des * par[(3+p0):(2+p0+p1)].- des_t * par[3:(2+p0)])  )  )
                                        return outs
    end
    
    for i in 1:nmc
      mv_normal = MvNormal(r_MLE, Sigma) 
      val = rand(mv_normal, 1)
      val1 = [val[1],exp(val[2]),val[3:end]...]
      mc[i] = S_par(val1)
    end
    
    L = quantile(mc,(1-level)*0.5)
    U = quantile(mc,(1+level)*0.5)
    
    M = S_par(MLE)
    
    return [L,M,U]
end
  


# times for CIs calculations
timesCI = [1.0,2.5,5,7.5,10,12.5];

CIS = zeros(length(timesCI), 4);

for k in 1:length(timesCI)
CIS[k,:] = vcat(timesCI[k], ConfIntSurv(timesCI[k],0.95,10000))
end

CIS = DataFrame(CIS, :auto);
 
rename!( CIS, ["year","lower","population survival","upper"] );

println(CIS)
6×4 DataFrame
 Row │ year     lower      population survival  upper     
     │ Float64  Float64    Float64              Float64   
─────┼────────────────────────────────────────────────────
   1 │     1.0  0.335479             0.356815   0.380269
   2 │     2.5  0.201698             0.219102   0.239836
   3 │     5.0  0.127659             0.142754   0.160797
   4 │     7.5  0.0951545            0.108917   0.125896
   5 │    10.0  0.0767494            0.0892138  0.105421
   6 │    12.5  0.064639             0.0761296  0.0913768

References

Chen, Y. Q., and N. P. Jewell. 2001. “On a General Class of Semiparametric Hazards Regression Models.” Biometrika 88 (3): 687–702.
Chen, Y. Q., and M. C. Wang. 2000. “Analysis of Accelerated Hazards Models.” Journal of the American Statistical Association 95 (450): 608–18.
Cox, D. R. 1972. “Regression Models and Life-Tables.” Journal of the Royal Statistical Society: Series B (Methodological) 34 (2): 187–202.
Kalbfleisch, J. D., and R. L. Prentice. 2011. The Statistical Analysis of Failure Time Data. John Wiley & Sons.
Rubio, F. J., L. Remontet, N. P. Jewell, and A. Belot. 2019. “On a General Structure for Hazard-Based Regression Models: An Application to Population-Based Cancer Research.” Statistical Methods in Medical Research 28: 2404–17.