50
$\begingroup$

I am wondering if there are any packages for python that is capable of performing survival analysis. I have been using the survival package in R but would like to port my work to python.

$\endgroup$
5
  • 12
    $\begingroup$ Use Rpy to call R from Python ;-) $\endgroup$
    – user88
    Commented Aug 16, 2010 at 12:35
  • 5
    $\begingroup$ It is exceedingly doubtful that the Python developers for survival analysis have put into the effort anywhere near what Terry Therneau and others have put into the R survival package in the past 30 years, including extensive testing. The survival package undergoes the most rigorous testing procedures I've seen in statistics. $\endgroup$ Commented Jun 25, 2013 at 16:09
  • 3
    $\begingroup$ Agreed. R's survival package is under close scrutiny by a large community. $\endgroup$ Commented Nov 24, 2013 at 20:15
  • $\begingroup$ RPy (2 now) is very painful to install in my experience. $\endgroup$
    – Zhubarb
    Commented Jan 9, 2014 at 8:36
  • $\begingroup$ meanwhile, you can easily call python from within R $\endgroup$
    – Ben
    Commented Mar 31, 2022 at 14:50

7 Answers 7

23
$\begingroup$

AFAIK, there aren't any survival analysis packages in python. As mbq comments above, the only route available would be to Rpy.

Even if there were a pure python package available, I would be very careful in using it, in particular I would look at:

  • How often does it get updated.
  • Does it have a large user base?
  • Does it have advanced techniques?

One of the benefits of R, is that these standard packages get a massive amount of testing and user feed back. When dealing with real data, unexpected edge cases can creep in.

$\endgroup$
1
72
$\begingroup$

Check out the lifelines¹ project for a simple and clean implementation of survival models in Python, including

  • Estimators of survival functions
  • Estimators of cumulative hazard curves
  • Cox's proportional hazard regression model
  • Cox's time varying regression model
  • parametric AFT models
  • Aalen's additive regression model
  • Multivariate testing

Benefits:

  • built on top of Pandas
  • pure Python & easy to install
  • built in plotting functions
  • simple interface

Documentation is available here: documentation and examples

Example usage:

from lifelines import KaplanMeierFitter

survival_times = np.array([0., 3., 4.5, 10., 1.])
events = np.array([False, True, True, False, True])

kmf = KaplanMeierFitter()
kmf.fit(survival_times, event_observed=events)

print(kmf.survival_function_)
print(kmf.median_)
kmf.plot()

Example plots from the built-in plotting library:

enter image description here

  1. Disclaimer: I'm the main author. Ping me (email in profile) for questions or feedback about lifelines.
$\endgroup$
1
  • 17
    $\begingroup$ (+1) Pretty clever name for a survival package. $\endgroup$ Commented Mar 1, 2015 at 20:19
9
$\begingroup$

python-asurv is an effort to port the asurv software for survival methods in astronomy. Might be worth keeping an eye on, but cgillespie is right about the things to watch out for: it has a long way to go and development doesn't seem active. (AFAICT only one method exists and even completed, the package may be lacking for, say, biostatisticians.)

You're probably better off using survival package in R from Python through something like RPy or PypeR. I haven't had any problems doing this myself.

$\endgroup$
8
$\begingroup$

PyIMSL contains a handful of routines for survival analyses. It is Free As In Beer for noncommercial use, fully supported otherwise. From the documentation in the Statistics User Guide...

Computes Kaplan-Meier estimates of survival probabilties: kaplanMeierEstimates()

Analyzes survival and reliability data using Cox’s proportional hazards model: propHazardsGenLin()

Analyzes survival data using the generalized linear model: survivalGlm()

Estimates using various parametric modes: survivalEstimates()

Estimates a reliability hazard function using a nonparametric approach: nonparamHazardRate()

Produces population and cohort life tables: lifeTables()

$\endgroup$
5
$\begingroup$

You can now use R from within IPython, so you might want to look into using IPython with the R extension.

$\endgroup$
4
  • 2
    $\begingroup$ Can you give an example of this? I'm curious to try it out! $\endgroup$
    – user603
    Commented Jul 24, 2012 at 8:47
  • $\begingroup$ I believe you refer to R magic interface (which uses rpy2). I too would love to see a quick example. (It is my understanding that survival models are not yet fully available in statsmodels.) $\endgroup$
    – chl
    Commented Jul 24, 2012 at 14:40
  • $\begingroup$ I'm not too sure about statsmodels, but you can get notebooks from github with examples of the C, Octave and R extensions. You need to run the Notebook to use them obviously, but I'm sure you can use the same code in any interface. $\endgroup$ Commented Jul 26, 2012 at 1:32
  • 3
    $\begingroup$ @user603 Here is a simple demo: nbviewer.ipython.org/4383682 ; this relies on fairly recent iPython, I believe. $\endgroup$
    – shabbychef
    Commented Dec 26, 2012 at 22:41
5
$\begingroup$

I also want to mention scikit-survival, which provides models for survival analysis that can be easily combined with tools from scikit-learn (e.g. KFold cross-validation).

As of this writing, scikit-survival includes implementations of

  • Nelson-Aalen estimator of cumulative hazard function.
  • Kaplan-Meier estimator of survival function.
  • Cox's proportional hazard’s model with and without elastic net penalty.
  • Accelerated failure time model.
  • Survival Support Vector Machine.
  • Gradient boosted Cox model.
  • concordance index for performance evaluation.
$\endgroup$
1
  • $\begingroup$ scikit-survival is excellent for time-to-event prediction related tasks! $\endgroup$ Commented Nov 21, 2018 at 5:02
1
$\begingroup$

Apart from using R through RPy or equivalent there are a number of survival analysis routines in the statsmodels (formerly sicpy.statsmodel) python library. They are in the "sandbox" package though, meaning they aren't supposed to be ready for production right now.

E.g. you have the Cox model of proportional hazard coded here.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.