Skip to main content

CoxRMSTSimpson (Python)

A Python companion to the R package CoxRMSTSimpson. It predicts survival curves from a fitted lifelines.CoxPHFitter, estimates restricted mean survival time (RMST) using composite Simpson's rule, and plots survival curves with shaded areas.

Development status: This package is under development. Validate numerical results against the R implementation before assuming numerical equivalence.

Install

python -m pip install coxrmstsimpson

Example

The following example simulates survival data, fits a Cox proportional hazards model, estimates RMST up to time (\tau=10), and plots two predicted survival curves.

import numpy as np
import pandas as pd
from lifelines import CoxPHFitter
from coxrmstsimpson import cox_rmst, plot_cox_rmst

# Simulate reproducible survival data.
np.random.seed(42)
n = 200

group = np.random.binomial(1, 0.5, n)
event_time = np.random.exponential(
    scale=1 / (0.10 * np.exp(0.4 * group))
)
censor_time = np.random.exponential(scale=15, size=n)

df = pd.DataFrame({
    "time": np.minimum(event_time, censor_time),
    "status": (event_time <= censor_time).astype(int),
    "group": group,
})

# Fit the Cox proportional hazards model.
cph = CoxPHFitter()
cph.fit(df, duration_col="time", event_col="status")

# Define the two covariate profiles to compare.
profiles = pd.DataFrame({"group": [0, 1]})

# Estimate RMST up to tau = 10.
result = cox_rmst(cph, profiles, tau=10, n_grid=101)
print(result["results"])

# Plot survival curves with RMST estimates in the legend.
ax = plot_cox_rmst(
    result,
    labels=["Control", "Treatment"],
    show_rmst=True,
)

In this example, status is 1 when the event occurs and 0 for right-censored observations. For your own data, use the covariates and encodings used to fit your Cox model. Each row of profiles specifies a covariate profile to evaluate, and the labels are supplied in the same order.

Functions

  • simpson_rmst(time, survival): Integrates a supplied survival curve using composite Simpson's rule.
  • cox_rmst(model, newdata, tau, n_grid=101): Predicts survival curves from a fitted Cox model and estimates RMST up to tau.
  • plot_cox_rmst(result, ax=None, labels=None, show_rmst=False): Plots predicted survival curves and shades the areas beneath them. Use labels for custom profile labels and show_rmst=True to include RMST estimates in the legend.

Definition

Restricted mean survival time up to time (\tau) is the area under the survival function from 0 to (\tau):

[ \operatorname{RMST}(\tau)=\int_0^\tau S(t),dt. ]

Here, (S(t)) is the predicted survival probability at time (t).

Numerical note

lifelines interpolates predicted survival probabilities at requested times not already on its prediction timeline. This can differ from the step-function evaluation commonly used by R's survival::survfit. Cross-language validation is therefore needed before claiming exact numerical equivalence.

Requirements

Python 3.10 or later. Dependencies, including NumPy, pandas, SciPy, Matplotlib, and lifelines, are installed automatically by pip.

Metadata

Release files for coxrmstsimpson 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for coxrmstsimpson 0.2.2
File Size Uploaded Provenance
coxrmstsimpson-0.2.2.tar.gz 5.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for coxrmstsimpson 0.2.2
File Interpreter ABI Platform
coxrmstsimpson-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / coxrmstsimpson-0.2.2.tar.gz

Download URL coxrmstsimpson-0.2.2.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1e3e16f9023e5a95e476a557ab9627728b606b9e2a236477ab886e1e82e480f9
BLAKE2b-256 checksum
How to use checksums
c2aecd782a815335d3cbf90c1456d62c80f66d2c3e96691202eda0113e5fcf65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release files / coxrmstsimpson-0.2.2-py3-none-any.whl

Download URL coxrmstsimpson-0.2.2-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0b0c0fe396d7f6fb5569200c460d3bcffd89fb30988f3a7dfb3495a856fdf38
BLAKE2b-256 checksum
How to use checksums
98ac40158ed5e8607ad952a5040db846dd6aa19e937493e1a6857afb3203c79a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Security for coxrmstsimpson 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page