Getting started

Installation

You can install survivalstan with pip:

pip install survivalstan

Typical analysis workflow

At a high level, the Survival modeling workflow proceeds in several steps:

  1. Obtain or simulate data: survivalstan.sim
  2. Select model to fit: survivalstan.models
  3. Transform data, if required by your model: survivalstan.prep_data_long_surv
  4. Fit model to data: survivalstan.fit_stan_survival_model
  5. Check model fit
    • survivalstan.utils.print_stan_summary
    • survivalstan.utils.plot_pp_survival
  6. Review inferences from model
    • survivalstan.utils.plot_coefs, etc
  7. Update model: rince and repeat!

Some high-level comments:

  • Most if not all survivalstan functions accept a list of models.
    • It’s rare to analyze your data with one and only one model.
    • Often you run the same model with more interations, or you try alternate specifications of the same model.
    • Use the optional parameter model_cohort to “label” each model fit object.
  • Not all functions will work with all models
    • Example, survivalstan.utils.plot_grp_coefs will only work for models with grouped-coefficients (ie a varying-coefficient model).
    • Similarly, survivalstan.utils.plot_baseline_hazard will only work for “PEM” models, which have semi- or non-parametrics baseline hazards.

Obtain or simulate data

  • The survivalstan.sim module includes several data-simulation routines.
  • typically one record per subject
  • “survival status” typically consists of several columns. At a minimum: time and status (1:event, 0:censor)

Select the survival model you want to fit

  • Survivalstan includes Stan code for a variety of models, including:
    • Parametric `baseline hazards <>`__ (weibull, gamma, exponential)
    • Semi- and non-parametric baseline hazards (pem_randomwalk, pem_gamma, pem_unstructured)
    • Estimating time-dependent effects
    • Varying-coefficient models
    • Stratified models
    • Competing events models
  • Or, you may have your own Stan code - a variation of one of the above, or your own
    • Many of the models provided by Survivalstan include helper functions for regularizing priors, etc.

Transform data into a format suitable for your model

  • Many of the models provided by Survivalstan (any that start with “pem”) require that data be provided in a “long” format. See `Survival modeling with long vs wide format <>`__
  • You can use prep_data_long_surv function to transform your data into the long format prior to analysis.

Fit your model to your data: fit_stan_survival_model

  • (within fit_stan_survival_model):
    • calls StanSurvivalData() to return a data dictionary for your stan model
    • compiles & optionally caches the compiled model
    • calls pystan.stan, stancache.cached_model_fit to fit the model
    • (tries to) call stanity.psisloo to summarize fit
    • (tries to) call survivalstan.utils.extract_baseline_hazard, extract_grp_coefs, and extract_coefs
    • returns a data dictionary containing meta-data about your inputs & outputs

Check model fit & posterior predictive checking

  • Try survivalstan.utils.plot_baseline_hazard
  • Try survivalstan.utils.plot_pp_survival

Update the model (go back to # 2)