Computational Survival Analysis: A Tutorial in Python#
Survival analysis is a powerful and versatile set of statistical tools that not enough people know about. It is usually presented mathematically, but in this tutorial we take a computational approach, using pandas objects to represent survival functions and hazard functions, and to implement basic algorithms like Kaplan-Meier estimation.
The examples are about light bulbs and dog adoptions, but the same methods apply to human lifetimes, mechanical failures, customer churn, and the time until almost any kind of event.
Level: intermediate. You should know basic Python; familiarity with pandas helps, but isn’t required. You don’t need any background in survival analysis.
Length: about 90 minutes.
The notebooks#
The pages on this site show the notebooks with solutions. To do the exercises, run the versions with blanks on Colab:
Survival and hazard functions: four ways to represent a distribution of lifetimes (PMF, CDF, survival function, and hazard function), using data from an experiment that ran 50 light bulbs until they all failed. Exercises on Colab
Kaplan-Meier estimation: estimating the hazard and survival functions when some cases are incomplete, using hypothetical data from a dog shelter; then the same computation with the
lifelineslibrary. Exercises on Colab
Recording#
I presented this tutorial at PyData Global 2021; here is the video. The code in the video is an earlier version; the repository README has a link to the version that goes with it.
Your feedback is welcome. The best way to provide it is to click here and create an issue in this GitHub repository.
Next steps#
To go further, read the survival analysis chapter of Think Stats, which is free online, or see my other workshops.
Code license: MIT License
Text license: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International