Skip to main content

A Python package for IPTW-based survival analysis with bootstrapped metrics and covariate balance diagnostics.

Project description

iptw-survival

iptw-survival is a Python package for calculating inverse probability of treatment weights (IPTW) and performing survival analysis using real-world data. It includes functionality for estimating stabilized weights, checking covariate balance, visualizing propensity scores, and calculating bootstrapped survival metrics.

Features

  • Calculate stabilized or unstabilized IPTW using logistic regression
  • Visualize propensity score distributions for treatment groups
  • Assess covariate balance using standardized mean differences (SMD), including a Love plot
  • Generate a bootstrapped Kaplan-Meier survival summary DataFrame with point estimates and bootstrapped 95% confidence intervals (useful for plotting with your preferred visualization library)
  • Compute survival metrics -- all with bootstrapped 95% condience intervals -- including:
    • Probability of survival at fixed timepoints
    • Restricted mean survival time (RMST)
    • Median survival

Standard variance estimates for survival curves can be biased when using IPTW, especially when weights are non-integer or extreme. To address this, iptw-survival uses bootstrapping to provide robust confidence intervals for survival metrics.

Installation

pip install iptw-survival

Quick Start

from iptw_survival import IPTWSurvivalEstimator

# Instantiate and fit model
estimator = IPTWSurvivalEstimator()
iptw_df = estimator.fit_transform(df,
                                  treatment_col = 'treatment',
                                  cat_var = ['stage', 'ecog'],
                                  cont_var = ['age', 'creatinine'],
                                  binary_var = ['surgery', 'medicaid'],
                                  lr_kwargs = {
                                    'class_weight': 'balanced',
                                    'random_state': 42},
                                  clip_bounds = (0.01, 0.99),
                                  stabilized = True)

# Plot propensity score distribution
estimator.propensity_score_plot()

# Assess covariate balance
smd_df, fig = estimator.standardized_mean_differences(return_fig = True)

# Kaplan-Meier plot with bootstrapped CIs
km_df = estimator.km_confidence_interval(df = iptw_df
                                         event_col='event', 
                                         duration_col = 'duration',
                                         n_bootstrap = 500
                                         random_state = 42)

# Use km_df to plot survival curves 
import matplotlib.pyplot as plt

plt.plot(km_df['time'], km_df['treatment_estimate'], label = 'Treatment')
plt.fill_between(km_df['time'], km_df['treatment_lower_ci'], km_df['treatment_upper_ci'], alpha = 0.1)
plt.xlabel('Time (months)')
plt.ylabel('Survival probability')
plt.legend()
plt.title('IPTW-adjusted Kaplan-Meier Curve')
plt.show()

# Calculate survival metrics
results = estimator.survival_metrics(df = iptw_df
                                     event_col='event', 
                                     duration_col = 'duration',
                                     psurv_time_points = [24, 36],
                                     rmst_time_points = [12],
                                     median_time = True
                                     n_bootstrap = 500
                                     random_state = 42)

Example Tutorial

A full walkthrough using the Flatiron Health advanced urothelial cancer dataset is available in tutorial/tutorial.ipynb.

Requirements

Built and tested in python 3.13

Core dependencies:

  • pandas
  • numpy
  • scikit-learn
  • lifelines
  • matplotlib

Contact

Contributions and feedback are welcome. Contact: xavierorcutt@gmail.com

Project details


Download files

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

Source Distribution

iptw_survival-0.1.4.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iptw_survival-0.1.4-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file iptw_survival-0.1.4.tar.gz.

File metadata

  • Download URL: iptw_survival-0.1.4.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for iptw_survival-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a3d154eb43bcde776617783196ff66db9afdc0cbbd606eeb5c8c565672827aa6
MD5 17187044e96bc761ea13ca633d5c5eae
BLAKE2b-256 e1f76a4c447f3d552a844dc90b9c54cd67f7c4271793c7f223499b4daa1f5a41

See more details on using hashes here.

File details

Details for the file iptw_survival-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: iptw_survival-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for iptw_survival-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 32588db0730d71a4e3f7bff546020f141e8fc81cce6440d1c165b70ba6fde2b9
MD5 7c7cc99a3af860e73dcebcad1a2c4fc8
BLAKE2b-256 ff300feece91aeb04b0a6e951a36d78a343d72d9b9a7d7782279bf1d1d27a68e

See more details on using hashes here.

Supported by

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