lifelines Survival Analysis Documentation

repository·master·Indexed 25 days ago

https://github.com/camdavidsonpilon/lifelines

A pure Python library for survival analysis providing tools to model and analyze time-to-event data. It includes implementations for Kaplan-Meier estimation, Cox Proportional Hazards models, and various parametric models (Weibull, Log-Normal, Log-Logistic, Exponential). The library supports handling right, left, and interval censoring, time-varying covariates, and provides utilities for model diagnostics, AIC-based model selection, and survival plotting.

Tokens
26.1K
Snippets
75
Records
123
Agent score
82%

What's inside lifelines

  1. Overview of lifelines for survival analysis

    master

    lifelines is a pure Python implementation of survival analysis techniques. It is designed to answer questions about why events occur at specific times under uncertainty (e.g., time-to-event data).

    Common use cases include:

    • Medical/Actuarial: Measuring lifetimes or disease remission.
    • SaaS: Measuring subscriber lifetimes or time to first action.
    • Inventory: Analyzing stock-out events relative to demand.
    • Sociology: Measuring the duration of political parties, relationships, or marriages.
    • A/B Testing: Determining the time taken for different groups to perform an action.
  2. Core Mathematical Concepts: Survival, Hazard, and Cumulative Hazard Functions

    master

    Survival analysis revolves around three interconnected mathematical entities. lifelines provides tools to estimate and transform between these:

    1. Survival Function $S(t)$: The probability that the event has not occurred yet by time $t$ (i.e., $Pr(T > t)$). It is a non-increasing function ranging from 0 to 1.
    2. Hazard Function $h(t)$: The instantaneous rate of the event occurring at time $t$, given that the event has not occurred until then.
    3. Cumulative Hazard Function $H(t)$: The integral of the hazard function over time: $H(t) = \int_0^t h(z) \mathrm{d}z$.

    Relationships:

    • $S(t) = \exp(-H(t))$
    • $H(t) = -\ln(S(t))$
    • $h(t) = \frac{-S'(t)}{S(t)}$
  3. Understand Right-Censoring in Survival Analysis

    master

    In survival analysis, right-censoring occurs when the event of interest (e.g., death, churn, failure) has not yet been observed for some individuals in the study at the time of analysis.

    Key points:

    • Right-censored individuals: We only know their current lifetime duration, which is strictly less than their actual lifetime.
    • Common Pitfall: Ignoring right-censored individuals or simply taking the mean of all observed lifespans (including censored ones) leads to a severe underestimation of the true average lifespan.
    • Purpose of Survival Analysis: It is specifically designed to handle these estimations correctly by accounting for the fact that some individuals have not yet experienced the event.
  4. Understand prediction limitations in time-varying models

    master

    Predicting survival in a time-varying setting is non-trivial because future covariate values are unknown. While CoxTimeVaryingFitter provides prediction methods, they are logically limited.

    Users can still compute:

    • Hazard values at known observation times.
    • The baseline cumulative hazard rate.
    • The baseline survival function.

    However, predicting future survival requires assuming future covariate values, which may not be accurate.

  5. Interpret CoxPHFitter coefficients and baseline hazard

    master

    After fitting, you can access model components via these attributes:

    • cph.params_: The coefficients of the covariates.
    • cph.baseline_hazard_: The baseline hazard.

    Hazard Ratio: The exponentiated coefficient $\exp(\text{coef})$ is the hazard ratio. For a binary covariate (e.g., mar for married), $\exp(\text{coef})$ represents the ratio of hazards between the two levels (e.g., married vs. unmarried).

  6. Prepare a dataset for survival regression

    master

    To perform survival regression, your data must be in a Pandas DataFrame format where each row represents a single observation. The DataFrame should include:

    1. Duration column: Denotes the time elapsed for each observation.
    2. Event status column (optional): Denotes if the event occurred (typically 1 if the event occurred, 0 if censored).
    3. Covariates: Additional variables you wish to regress against.
    4. Optional columns: You may also include columns for stratification, weights, or clusters.
  7. Predict survival for censored subjects using conditional probability

    master

    When predicting for subjects who have already survived past a certain time $s$ (censored subjects), you must calculate the conditional survival function. This answers: What is the subject's new survival function given they have already lived until time $s$?

    In lifelines, all prediction methods support the conditional_after keyword argument to handle this.

    Important: When using conditional_after, the resulting metrics are conditional. For example, if predict_median returns 10.5, the predicted total lifetime is $10.5 + s$.

    To predict the remaining life of censored subjects:

    1. Identify the censored subjects.
    2. Pass their last observed time as the conditional_after argument.
    # all regression models can be used here, WeibullAFTFitter is used for illustration
    from lifelines import WeibullAFTFitter
    from lifelines.datasets import load_rossi
    
    rossi = load_rossi()
    wf = WeibullAFTFitter().fit(rossi, "week", "arrest")
    
    # filter down to just censored subjects to predict remaining survival
    censored_subjects = rossi.loc[~rossi['arrest'].astype(bool)]
    censored_subjects_last_obs = censored_subjects['week']
    
    # predict new survival function
    # the survival function is scaled by the survival at the conditional_after time
    wf.predict_survival_function(censored_subjects, conditional_after=censored_subjects_last_obs)
    
    # predict median remaining life
    # result is the additional time expected after censored_subjects_last_obs
    wf.predict_median(censored_subjects, conditional_after=censored_subjects_last_obs)
  8. Predict survival functions and cumulative hazards

    master

    Once a regression model (like CoxPHFitter, WeibullAFTFitter, or AalenAdditiveFitter) is fitted, you can predict survival outcomes for specific individuals or covariate sets using the following methods:

    • predict_survival_function(X): Returns the estimated survival function for the provided covariates X.
    • predict_cumulative_hazards(X): Returns the estimated cumulative hazard function for the provided covariates X.

    Note that X must be a set of covariates (e.g., a Pandas Series representing a single observation or a DataFrame of multiple observations).

    X = regression_dataset.loc[0]
    
    ax = wft.predict_survival_function(X).rename(columns={0:'WeibullAFT'}).plot()
    cph.predict_survival_function(X).rename(columns={0:'CoxPHFitter'}).plot(ax=ax)
    aaf.predict_survival_function(X).rename(columns={0:'AalenAdditive'}).plot(ax=ax)
  9. Understand survival regression concepts

    master

    Survival regression is used when you have additional data (covariates like age, country, etc.) in addition to the duration of an observation. It involves regressing these covariates against durations. Traditional linear regression cannot be used because of censoring.

    Common models in survival regression include:

    • Cox's model
    • Accelerated failure models
    • Aalen's additive model

    All these models attempt to represent the hazard rate $h(t | x)$ as a function of time $t$ and covariates $x$.

  10. Access lifelines documentation and tutorials

    master
    If you are new to survival analysis or need to understand the lifelines API and syntax, refer to the official documentation and tutorials. The documentation includes introductory material on survival analysis and practical examples of how to use the library.
  11. Adjust for correlated subjects in a Cox model

    master

    If your dataset contains correlated subjects (e.g., subjects appearing multiple times or matched pairs from propensity-score matching), the independent-and-identically-distributed assumption is violated and standard errors will be incorrect.

    To adjust for this, use the cluster_col keyword in the .fit() method of CoxPHFitter. Pass the name of a column that contains identifiers for correlated groups. For example, in a matched pair, both subjects in the pair should share the same value in the cluster_col column. Specifying cluster_col automatically invokes the robust sandwich estimator for standard errors (equivalent to setting robust=True).

    from lifelines import CoxPHFitter
    
    # Assuming 'id' column contains group identifiers for correlated subjects
    cph = CoxPHFitter()
    cph.fit(rossi, 'week', 'arrest', cluster_col='id')