Demonstration of survival curves with different hazard functions

Special Thanks

References

Definition of notations used here

Mathmathetical definitions

The survivor function and the cumulative density function have straightforward definitions as probabilities, and these are complementary to each other.

The probability density function and the hazard function can be defined as limits.

Relationships

Either one of the function can be defined with a functional form (parametric method) or estimated from the data (non-parametric method), and all other functions can be expressed as the function of the first function.

Define graphing function for graphical demonstration:

This function draws curves of functions using a given hazard function.

## Load ggplot2
library(ggplot2)

## Define graphing function
SurvGraph <- function(h, xlim = c(0,1), ylim = c(0,1)) {
    ## Define functions
    ## h(t) hazard function: Defined outside, and given as an argument

    ## H(t) cumulative hazard function: h(t) integrated from time = 0 to time = t
    ## Vectorize to enable use with a vector
    H <- Vectorize(function(t) {
        res <- integrate(h, lower = 0, upper = t)
        res$value
    })

    ## S(t) survivor function: Derived from H(t) = -logS(t)
    S <- function(t) {
        exp(-1 * H(t))
    }

    ## f(t) probability density function (pdf): Derived from h(t) = f(t) / S(t)
    f <- function(t) {
        S(t) * h(t)
    }

    ## F(t) cumulative distribution function (cdf): Complement of S(t), F(t) = 1 - S(t)
    F <- function(t) {
        1 - S(t)
    }

    ## Graphing with ggplot2
    ggplot(data = data.frame(x = xlim), aes(x)) +
        stat_function(fun = h, aes(color = "h")) +
        stat_function(fun = H, aes(color = "H")) +
        stat_function(fun = S, aes(color = "S")) +
        stat_function(fun = f, aes(color = "f")) +
        stat_function(fun = F, aes(color = "F")) +
        scale_x_continuous(name = "time",  limit = xlim) +
        scale_y_continuous(name = "value", limit = ylim) +
        scale_color_manual(name = "functions",
                           values = c("h" = "black", "H" = "red", "S" = "green", "f" = "blue", "F" = "purple"),
                           breaks = c("h","H","S","f","F"),
                           labels = c("h(t)","H(t)","S(t)","f(t)","F(t)"))
}

Constant hazard:

eg. Death in healthy young population. \( h(t) = 0.5 + 0 * t \) (added 0 * t to return a vector) The survival curve is exponential.

h.constant <- function(t) 0.5 + 0 * t

SurvGraph(h = h.constant, xlim = c(0,5), ylim = c(0,2)) +
    labs(title = "constant hazard: h(t) = 0.5")

plot of chunk unnamed-chunk-3

Increasing hazard:

eg. Cancer patients not responding to treatment \( h(t) = 0.3 * t \)

h.increasing <- function(t) 0.3 * t

SurvGraph(h = h.increasing, xlim = c(0,5), ylim = c(0,2)) +
        labs(title = "increasing hazard: h(t) = 0.3t")

plot of chunk unnamed-chunk-4

Decreasing hazard:

eg. Survival following surgery \( h(t) = 0.5 - 0.1 * t \).

h.decreasing <- function(t) 0.5 - 0.1 * t

SurvGraph(h = h.decreasing, xlim = c(0,5), ylim = c(0,2)) +
        labs(title = "decreasing hazard: h(t) = 0.5 - 0.1t")

plot of chunk unnamed-chunk-5

Rising and falling hazard:

eg. Survival following tuberculosis infection (Potential of death increases early and decreases later)

h.rise.fall <- function(t, scale = 2, shape = 1.5) {
    (shape / scale) * (t / scale)^(shape - 1) * exp(-1 * (t / scale)^shape)
}

SurvGraph(h = h.rise.fall, xlim = c(0,5), ylim = c(0,2)) +
        labs(title = "rising and falling hazard")

plot of chunk unnamed-chunk-6

Falling and rising hazard: h(t) = 0.15 * (t - 1)2 + 0.1

eg. Lifespan of animals (more death at extremes of ages)

h.fall.rise <- function(t) 0.2 * (t - 1.5)^2 + 0.1

SurvGraph(h = h.fall.rise, xlim = c(0,5), ylim = c(0,2)) +
        opts(title = "falling and rising hazard: h(t) = 0.15 * (t - 1)^2 + 0.1")

plot of chunk unnamed-chunk-7


For other information: http://rpubs.com/kaz_yos/ If you find errors: kazky AT mac.com