Some hazard functions in Julia

Author
Published

January 14, 2024

1 Hazard and cumulative hazard functions

The hazard and the cumulative hazard functions play a crucial role in survival analysis. These functions define the likelihood function in the presence of censored observations. Thus, they are important in many context. For more information about these functions, see Short course on Parametric Survival Analysis.

This tutorial contains the implementation of the hazard and cumulative hazard functions for some popular distributions using the Julia package HazReg.jl, such as the LogNormal, LogLogistic, Weibull, and Gamma distributions.

The Julia implementation of other, less common, distributions that are of interest in survival analysis can be found in the Julia package HazReg.jl: HazReg.jl.

See also:

2 Required packages

Code
using Distributions
using Plots
using StatsBase
using HazReg

3 Examples

3.1 LogNormal

Code
#= Hazard function =#
plot(t -> hLogNormal(t, 0.5, 1),
      xlabel = "x", ylabel = "Hazard", title = "LogNormal distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0.0 0.1 0.2 0.3 0.4 0.5
Code
#= Cumulative Hazard function =#
plot(t -> chLogNormal(t, 0.5, 1),
      xlabel = "x", ylabel = "Cumulative Hazard", title = "LogNormal distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3

3.2 LogLogistic

Code
#= Hazard function =#
plot(t -> hLogLogistic(t, 1, 0.5),
      xlabel = "x", ylabel = "Hazard", title = "LogLogistic distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0.0 0.1 0.2 0.3
Code
#= Cumulative Hazard function =#
plot(t -> chLogLogistic(t, 1, 0.5),
      xlabel = "x", ylabel = "Cumulative Hazard", title = "LogLogistic distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0.0 0.5 1.0 1.5 2.0 2.5

3.3 Weibull

Code
#= Weibull function =#
plot(t -> hWeibull(t, 3, 0.5),
      xlabel = "x", ylabel = "Hazard", title = "Weibull distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0 500 1000 1500 2000
Code
#= Cumulative Hazard function =#
plot(t -> chWeibull(t, 3, 0.5),
      xlabel = "x", ylabel = "Cumulative Hazard", title = "Weibull distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0 2000 4000 6000 8000

3.4 Gamma

Code
#= Weibull function =#
plot(t -> hGamma(t, 3, 0.5),
      xlabel = "x", ylabel = "Hazard", title = "Gamma distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0.0 0.5 1.0 1.5
Code
#= Cumulative Hazard function =#
plot(t -> chGamma(t, 3, 0.5),
      xlabel = "x", ylabel = "Cumulative Hazard", title = "Gamma distribution",
    xlims = (0,10),   xticks = 0:1:10, label = "", 
    xtickfont = font(16, "Courier"),  ytickfont = font(16, "Courier"),
    xguidefontsize=18, yguidefontsize=18, linewidth=3,
    linecolor = "blue")
0 1 2 3 4 5 6 7 8 9 10 0 5 10 15