Skip to main content

Survival analysis in Python, including Kaplan Meier, Nelson Aalen and regression

Project description

What is survival analysis and why should I learn it? Survival analysis was originally developed and applied heavily by the actuarial and medical community. Its purpose was to answer why do events occur now versus later under uncertainity (where events might refer to deaths, disease remission, etc.). This is great for researchers who are interested in measuring lifetimes: they can answer questions like what factors might influence deaths?

But outside of medicine and actuarial science, there are many other interesting and exciting applications of this lesser-known technique, for example: - SaaS providers are interested in measuring customer lifetimes, or time to first behaviours. - sociologists are interested in measure political parties lifetimes, or relationships, or marriages - Businesses are interested in what variables affect lifetime value

lifelines is a pure Python implementation of the best parts of survival analysis. We’d love to hear if you are using lifelines, please ping me at [@cmrn_dp](https://twitter.com/Cmrn_DP) and let me know your thoughts on the library.

Installation:

Dependencies:

The usual Python data stack: Numpy, Scipy, Pandas (a modern version please), Matplotlib

Installing

You can install lifelines using

pip install lifelines

Or getting the bleeding edge version with:

pip install git+https://github.com/CamDavidsonPilon/lifelines.git

or upgrade with

pip install --upgrade git+https://github.com/CamDavidsonPilon/lifelines.git

from the command line.

Intro to lifelines and survival analysis

Situation: 500 random individuals are born at time 0, currently it is time 12, so we have possibly not observed all death events yet.

# Create lifetimes, but censor all lifetimes after time 12
censor_after = 12
actual_lifetimes = np.random.exponential(10, size=500)
observed_lifetimes = np.minimum( actual_lifetimes, censor_after*np.ones(500) )
C = (actual_lifetimes < censor_after) #boolean array

Non-parametrically fit the survival curve:

from lifelines import KaplanMeierFitter

kmf = KaplanMeierFitter()
kmf.fit(observed_lifetimes, event_observed=C)

# fitter methods have an internal plotting method.
# plot the curve with the confidence intervals
kmf.plot()
kmf

It looks like 50% of all individuals are dead before time 7.

print kmf.survival_function_.head()

time            KM-estimate
0.000000        1.000
0.038912        0.998
0.120667        0.996
0.125719        0.994
0.133778        0.992

Non-parametrically fit the cumulative hazard curve:

from lifelines import NelsonAalenFitter

naf = NelsonAalenFitter()
naf.fit(observed_lifetimes, event_observed=C)

#plot the curve with the confidence intervals
naf.plot()
naf
print naf.cumulative_hazard_.head()

time       NA-estimate
0.000000     0.000000
0.038912     0.002000
0.120667     0.004004
0.125719     0.006012
0.133778     0.008024

Compare two populations using the logrank test:

from lifelines.statistics import logrank_test
other_lifetimes = np.random.exponential(3, size=500)

summary, p_value, results = logrank_test(observed_lifetimes, other_lifetimes, alpha=0.95)
print summary


Results
   df: 1
   alpha: 0.95
   t 0: -1
   test: logrank
   null distribution: chi squared

   __ p-value ___|__ test statistic __|__ test results __
         0.00000 |              268.465 |     True

(Less Quick) Intro to lifelines and survival analysis

If you are new to survival analysis, wondering why it is useful, or are interested in lifelines examples and syntax, please check out the Documentation and Tutorials page

Alternatively, you can use the IPython notebooks tutorials, located in the main directory of the repo:

  1. Introduction to survival analysis

  2. Using lifelines on real data

More examples

There are some IPython notebook files in the repo, and you can view them online here.

lifelines

License

The Feedback MIT License (FMIT)

Copyright (c) 2013, Cameron Davidson-Pilon

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

  2. Person obtaining a copy must return feedback to the authors.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lifelines logo designed by Pulse designed by TNS from the Noun Project

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lifelines-0.4.0.0.tar.gz (394.5 kB view details)

Uploaded Source

Built Distributions

lifelines-0.4.0.0-cp34-none-win_amd64.whl (402.0 kB view details)

Uploaded CPython 3.4 Windows x86-64

lifelines-0.4.0.0-cp34-none-win32.whl (402.5 kB view details)

Uploaded CPython 3.4 Windows x86

lifelines-0.4.0.0-cp33-none-win_amd64.whl (402.0 kB view details)

Uploaded CPython 3.3 Windows x86-64

lifelines-0.4.0.0-cp33-none-win32.whl (402.5 kB view details)

Uploaded CPython 3.3 Windows x86

lifelines-0.4.0.0-cp27-none-win_amd64.whl (402.0 kB view details)

Uploaded CPython 2.7 Windows x86-64

lifelines-0.4.0.0-cp27-none-win32.whl (402.5 kB view details)

Uploaded CPython 2.7 Windows x86

File details

Details for the file lifelines-0.4.0.0.tar.gz.

File metadata

  • Download URL: lifelines-0.4.0.0.tar.gz
  • Upload date:
  • Size: 394.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lifelines-0.4.0.0.tar.gz
Algorithm Hash digest
SHA256 29c486ef1e766501f1b93e98f5df3e22a0c0803b417360fc435a4c84ab86307b
MD5 8d62e765510217c2fbfd6143614714c4
BLAKE2b-256 5e7c7379d8165141a500bb06a6e993047452bc595f7b7fbad5eaa99c39bc925e

See more details on using hashes here.

File details

Details for the file lifelines-0.4.0.0-cp34-none-win_amd64.whl.

File metadata

File hashes

Hashes for lifelines-0.4.0.0-cp34-none-win_amd64.whl
Algorithm Hash digest
SHA256 59903a8126a1d33989a5f5ddfd57608394ced15a11467f3b279368f1cc2a115f
MD5 f911d221a7a708c26009498f3761dd2a
BLAKE2b-256 c99ea0f048f70178b06ec0d91c319dd7ae78131f75b77195ee784a6d28cebfe2

See more details on using hashes here.

File details

Details for the file lifelines-0.4.0.0-cp34-none-win32.whl.

File metadata

File hashes

Hashes for lifelines-0.4.0.0-cp34-none-win32.whl
Algorithm Hash digest
SHA256 8b578c07e4d471bdb651323a1c06dcffb569196b889701834accf9dc1265bae5
MD5 4b61bc3813392dd208492a4dc6723495
BLAKE2b-256 1332c0a1290f7fe54b0c7ebc36a68a5f7ef39ee5d08778cd349ebd32b6b1735e

See more details on using hashes here.

File details

Details for the file lifelines-0.4.0.0-cp33-none-win_amd64.whl.

File metadata

File hashes

Hashes for lifelines-0.4.0.0-cp33-none-win_amd64.whl
Algorithm Hash digest
SHA256 c9300a2e9977b96c3643184b1f022654a07fb4971b268864525033a1a9934286
MD5 4746ce77d2de3d141d537a07fb70b1cb
BLAKE2b-256 7a4ead2c1cc04d06fcc7afa394e5226af0376f02fedb6a75af821ed7f76f1bb5

See more details on using hashes here.

File details

Details for the file lifelines-0.4.0.0-cp33-none-win32.whl.

File metadata

File hashes

Hashes for lifelines-0.4.0.0-cp33-none-win32.whl
Algorithm Hash digest
SHA256 d1f4ea103f9cab98d99becb3c06dd8d330d418a79c79a86d2163d35b64854e35
MD5 1b48c64a2bc5d2222083c348dcd425c9
BLAKE2b-256 0b29462a702e906ccc75e8c819c4ed69d4083dedf7bc1ea0611b386a7b91ac93

See more details on using hashes here.

File details

Details for the file lifelines-0.4.0.0-cp27-none-win_amd64.whl.

File metadata

File hashes

Hashes for lifelines-0.4.0.0-cp27-none-win_amd64.whl
Algorithm Hash digest
SHA256 487451399c5ddaa22c01ea7288851ee0fe5421afe27a9af48ee0888773043e15
MD5 261005dc3a28aac5dce6ac622fdcac09
BLAKE2b-256 5eaf75a88ef70ca15d17f0763de06d288e50c206952da7bdb7bd4f29ff396123

See more details on using hashes here.

File details

Details for the file lifelines-0.4.0.0-cp27-none-win32.whl.

File metadata

File hashes

Hashes for lifelines-0.4.0.0-cp27-none-win32.whl
Algorithm Hash digest
SHA256 7de592aa00a7b339d4166e7214d6c54015a4c3aa36793e1eda3252a36e6adebe
MD5 cc6e74ed5f803da9a248d2fb2ff10aaa
BLAKE2b-256 7ac0580af6ae18aa46f84f5df5d8a98334a4892e602f35e0e6a2bb46fd38c51f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page