CoxRMSTSimpson (Python)
A Python companion to the R package CoxRMSTSimpson.
coxrmstsimpson estimates Restricted Mean Survival Time (RMST) from fitted Cox proportional hazards models using composite Simpson's rule. It predicts survival curves from a fitted lifelines.CoxPHFitter, estimates RMST up to a user-specified restriction time, and produces survival plots with shaded RMST areas.
Features
- Estimate RMST from fitted
lifelines.CoxPHFittermodels. - Compare the two levels of a binary treatment variable.
- Specify custom labels for treatment groups.
- Predict survival curves and estimate RMST up to a chosen time.
- Automatically display RMST results and a survival plot.
- Integrate survival curves using composite Simpson's rule.
Installation
Install the package from PyPI:
python -m pip install coxrmstsimpson
Python 3.10 or later is required. Dependencies, including NumPy, pandas, SciPy, Matplotlib, and lifelines, are installed automatically by pip.
Quick Start: Control versus Treatment
This example simulates survival data, fits a Cox proportional hazards model, estimates RMST up to time 10, and produces a plot comparing Control and Treatment.
import numpy as np
import pandas as pd
from lifelines import CoxPHFitter
from coxrmstsimpson import cox_rmst
# Simulate reproducible survival data
np.random.seed(123)
n = 200
group = np.random.binomial(1, 0.5, n)
event_time = np.random.exponential(
scale=1 / (0.10 * np.exp(-0.5 * group))
)
censor_time = np.random.exponential(scale=15, size=n)
df = pd.DataFrame({
"time": np.minimum(event_time, censor_time),
"event": (event_time <= censor_time).astype(int),
"group": group
})
# Fit the Cox proportional hazards model
cph = CoxPHFitter()
cph.fit(df, duration_col="time", event_col="event")
# Estimate RMST and create the plot
result = cox_rmst(
cph,
treatment="group",
tau=10,
labels=["Control", "Treatment"],
title="Restricted Mean Survival Time by Treatment"
)
# Display the RMST results
print(result["results"])
In this example:
group = 0represents Control.group = 1represents Treatment.event = 1indicates that the event occurred.event = 0indicates a right-censored observation.tau = 10sets the restriction time for the RMST calculation.
The package predicts survival curves for the two treatment groups, estimates their RMST values, displays the results, and creates the plot. A separate plotting command or plt.show() call is not required for this workflow.
For your own data, fit a Cox model using the appropriate survival-time, event-status, and covariate variables. The treatment variable supplied to cox_rmst() must be a binary covariate included in the fitted model, coded 0 and 1.
Using Explicit Covariate Profiles
You can also specify the covariate profiles directly using newdata.
For example, if the fitted Cox model includes a binary covariate named group, you can compare the following profiles:
profiles = pd.DataFrame({
"group": [0, 1]
})
result = cox_rmst(
cph,
newdata=profiles,
tau=10
)
print(result["results"])
Each row of profiles specifies one covariate profile for which a predicted survival curve and RMST estimate are calculated. The profile columns must match the covariates and encodings used to fit the Cox model.
Available Functions
cox_rmst()
Estimates restricted mean survival time from a fitted Cox proportional hazards model.
The function supports two workflows:
Automatic binary treatment comparison
result = cox_rmst(
cph,
treatment="group",
tau=10,
labels=["Control", "Treatment"]
)
Explicit covariate profiles
result = cox_rmst(
cph,
newdata=profiles,
tau=10,
n_grid=101
)
The n_grid parameter controls the number of time-grid points used for numerical integration and must be an odd integer of at least 3.
plot_cox_rmst()
Plots predicted survival curves and shades the areas beneath them up to the restriction time.
from coxrmstsimpson import plot_cox_rmst
plot_cox_rmst(result)
This function is available when you want to plot an existing result separately. In the automatic treatment-comparison workflow, cox_rmst() creates the plot for you.
simpson_rmst()
Estimates the area under a supplied survival curve using composite Simpson's rule.
from coxrmstsimpson import simpson_rmst
rmst = simpson_rmst(time, survival)
Here, time contains the time-grid values and survival contains the corresponding survival probabilities.
Mathematical Definition
Restricted mean survival time up to time (\tau) is the area under the survival function from time 0 to (\tau):
[ \operatorname{RMST}(\tau)=\int_0^\tau S(t),dt. ]
Here, (S(t)) is the predicted survival probability at time (t).
RMST provides an interpretable summary of the expected time alive over the specified follow-up interval, based on the survival curve.
Numerical Note
lifelines interpolates predicted survival probabilities at requested times that are not already on its prediction timeline. This can differ from the step-function evaluation commonly used by R's survival::survfit.
Consequently, results from the Python and R implementations may differ numerically. Cross-language validation is recommended before claiming exact numerical equivalence.
Requirements
- Python 3.10 or later
- NumPy
- pandas
- SciPy
- Matplotlib
- lifelines
These dependencies are installed automatically when the package is installed using pip.
Project Links
- PyPI package: https://pypi.org/project/coxrmstsimpson/
- GitHub Python repository: https://github.com/fordeia/CoxRMSTSimpson-Python
- Companion R package: https://github.com/fordeia/CoxRMSTSimpson
Citation
If you use coxrmstsimpson in research, cite the software and provide the package URL. For the companion R implementation, consult its GitHub repository for the available citation information.
Metadata
Release files for coxrmstsimpson 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| coxrmstsimpson-0.2.3.tar.gz | 6.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coxrmstsimpson-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.7 kB
Release files / coxrmstsimpson-0.2.3.tar.gz
| Download URL | coxrmstsimpson-0.2.3.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
385ce5346103d7ebc9abac214ae97f5613d4cfd4897c8e1115be69921314d417
|
|
BLAKE2b-256 checksum How to use checksums |
01f309eec7acb9abc6675daa5583c3f0f9f2457b5215052fb83e45cc687d8b05
|
| 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.3-py3-none-any.whl
| Download URL | coxrmstsimpson-0.2.3-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1d796188af4b23904bc3fc8ba8fc8ace31e6aa4cf4102edb4022ad982ff75bc3
|
|
BLAKE2b-256 checksum How to use checksums |
82dfe6c6cdb77083caf78ee5deef925c83a4021a8306d3d2f5ac81ca0236abdd
|
| 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.3
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.