hammerlab/survivalstan

Name: survivalstan

Owner: Hammer Lab

Description: Library of Stan Models for Survival Analysis

Created: 2016-06-29 16:49:49.0

Updated: 2018-01-15 16:58:06.0

Pushed: 2017-10-24 20:08:51.0

Homepage:

Size: 6860

Language: Jupyter Notebook

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status Coverage Status PyPI version

survivalstan: Survival Models in Stan

author: Jacki Novik

Overview

Library of Stan Models for Survival Analysis

Features:

Support

Documentation is available online.

For help, please reach out to us on gitter.

Installation / Usage

Install using pip, as:

$ pip install survivalstan

Or, you can clone the repo:

$ git clone https://github.com/hammerlab/survivalstan.git
$ pip install .
Contributing

Please contribute to survivalstan development by letting us know if you encounter any bugs or have specific feature requests.

In addition, we welcome contributions of:

Usage examples

There are several examples included in the example-notebooks, roughly one corresponding to each model.

If you are not sure where to start, Test pem_survival_model with simulated data.ipynb contains the most explanatory text. Many of the other notebooks are sparse on explanation, but do illustrate variations on the different models.

For basic usage:

rt survivalstan
rt stanity
rt seaborn as sb
rt matplotlib.pyplot as plt
rt statsmodels

oad flchain test data from R's `survival` package
set = statsmodels.datasets.get_rdataset(package = 'survival', dataname = 'flchain' )
 dataset.data.query('futime > 7')
set_index(level = 0, inplace = True)

.g. fit Weibull survival model
fit_wei = survivalstan.fit_stan_survival_model(
model_cohort = 'Weibull model',
model_code = survivalstan.models.weibull_survival_model,
df = d,
time_col = 'futime',
event_col = 'death',
formula = 'age + sex',
iter = 3000,
chains = 4,
make_inits = survivalstan.make_weibull_survival_model_inits
)

oefplot for Weibull coefficient estimates
oxplot(x = 'value', y = 'variable', data = testfit_wei['coefs'])

r, use plot_coefs
ivalstan.utils.plot_coefs([testfit_wei])

rint summary of MCMC draws from posterior for each parameter
t(testfit_wei['fit'])


.g. fit Piecewise-exponential survival model 
g = survivalstan.prep_data_long_surv(d, time_col = 'futime', event_col = 'death')
fit_pem = survivalstan.fit_stan_survival_model(
model_cohort = 'PEM model',
model_code = survivalstan.models.pem_survival_model,
df = dlong,
sample_col = 'index',
timepoint_end_col = 'end_time',
event_col = 'end_failure',
formula = 'age + sex',
iter = 3000,
chains = 4,
)

rint summary of MCMC draws from posterior for each parameter
t(testfit_pem['fit'])

oefplot for PEM model results
oxplot(x = 'value', y = 'variable', data = testfit_pem['coefs'])

lot baseline hazard (only PEM models)
ivalstan.utils.plot_coefs([testfit_pem], element='baseline')

osterior-predictive checking (only PEM models)
ivalstan.utils.plot_pp_survival([testfit_pem])

.g. compare models using PSIS-LOO
ity.loo_compare(testfit_wei['loo'], testfit_pem['loo'])

ompare coefplots 
oxplot(x = 'value', y = 'variable', hue = 'model_cohort',
data = testfit_pem['coefs'].append(testfit_wei['coefs']))
legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

or, use survivalstan.utils.plot_coefs)
ivalstan.utils.plot_coefs([testfit_wei, testfit_pem])

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.