Skip to content

Add FlexibleParametricPHFitter: Royston-Parmar flexible parametric survival model#1690

Open
hass-nation wants to merge 1 commit into
CamDavidsonPilon:masterfrom
hass-nation:feat/royston-parmar
Open

Add FlexibleParametricPHFitter: Royston-Parmar flexible parametric survival model#1690
hass-nation wants to merge 1 commit into
CamDavidsonPilon:masterfrom
hass-nation:feat/royston-parmar

Conversation

@hass-nation

Copy link
Copy Markdown

Summary

Adds FlexibleParametricPHFitter, a flexible parametric proportional hazards model that uses restricted natural cubic splines (RCS) on log(t) to model the log cumulative hazard log(H(t)) = s(log(t); γ) + x'β.

Unlike the existing Weibull/log-normal/log-logistic fitters which impose a fixed parametric shape, the Royston-Parmar model adapts the baseline hazard to the data via a spline with user-controlled knot count.

Algorithm

  • Model: log(H(t|x)) = γ₀ + γ₁·log(t) + Σⱼ γⱼ₊₁·vⱼ(log(t)) + x'β, where vⱼ are restricted cubic spline basis functions.
  • Estimation: MLE via L-BFGS-B (scipy) with analytical log-likelihood gradient.
  • Knot placement: percentiles of log(observed event times); user-overridable.
  • Penalizer: optional L2 penalty on spline coefficients γ for regularisation.

New file

lifelines/fitters/royston_parmar_fitter.pyFlexibleParametricPHFitter class

Interface

from lifelines import FlexibleParametricPHFitter

fitter = FlexibleParametricPHFitter(n_baseline_knots=4)
fitter.fit(df, duration_col="T", event_col="E", covariates=["x1", "x2"])
fitter.predict_survival_function(X, times=np.linspace(0, 10, 100))
fitter.predict_median(X)
fitter.AIC_

Tests

17 tests — shape checks, monotone survival, concordance index above chance, custom knot locations, covariate effects, AIC computation. All pass.

Reference

Royston, P. & Parmar, M. K. B. (2002). Flexible parametric proportional-hazards and proportional-odds models for censored survival data, with application to prognostic modelling and estimation of treatment effects. Statistics in Medicine, 21(15), 2175–2197.

Implements the Royston-Parmar flexible parametric proportional-hazards
model (Statistics in Medicine, 21(15):2175-2197, 2002).

Algorithm details:
- Models log(H(t|x)) = s(log(t); gamma) + x'*beta where s() is a
  restricted natural cubic spline (RCS) in log(t).
- Fit via MLE using analytical gradients and L-BFGS-B (scipy).
- Default n_baseline_knots=4 placed at quantiles of log(event times).
- RCS basis handles K=2 (Exponential), K=3 (Weibull-like) and K>=4
  (full cubic spline) cases correctly.

New files:
- lifelines/fitters/royston_parmar_fitter.py  -- FlexibleParametricPHFitter
- tests/test_royston_parmar_fitter.py          -- 17 tests, all passing

Modified:
- lifelines/__init__.py  -- export FlexibleParametricPHFitter

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant