SmoothHRResult

Smooth non-linear hazard ratio curve for a continuous covariate.

Usage

Source

SmoothHRResult(
    term,
    reference,
    grid,
    log_hr,
    hr,
    log_hr_lower,
    log_hr_upper,
    hr_lower,
    hr_upper,
    df,
    knots,
    adjustment,
)

Contains the estimated log-hazard ratio (and hazard ratio) as a function of a continuous covariate, computed by refitting the Cox model with a B-spline basis expansion. The curve shows how the covariate’s effect on the hazard varies across its range, relative to a reference value.

Attributes

term: str

Name of the covariate whose smooth effect is estimated.

reference: float

Reference value of the covariate. The log-HR is zero at this point.

grid: Array

Array of covariate values at which the curve is evaluated.

log_hr: Array

Log hazard ratio at each grid point (relative to reference).

hr: Array

Hazard ratio at each grid point (exp(log_hr)).

log_hr_lower: Array

Lower confidence bound on the log-HR.

log_hr_upper: Array

Upper confidence bound on the log-HR.

hr_lower: Array

Lower confidence bound on the HR (exp(log_hr_lower)).

hr_upper: Array

Upper confidence bound on the HR (exp(log_hr_upper)).

df: int

Degrees of freedom of the spline (number of basis functions).

knots: Array

Interior knot positions used for the B-spline basis.

adjustment: dict[str, float]
Dictionary of covariate names and the values they were held at.

Examples

Estimate a smooth hazard ratio curve for age from a Cox model fit on the lung dataset. The curve shows how the hazard changes across the range of age, relative to the median:

import greenwood as gw

lung = gw.load_dataset("lung", backend="polars")
cox = gw.CoxPH().fit("Surv(time, status) ~ age + sex", data=lung)

shr = cox.smooth_hr(term="age")
shr
SmoothHRResult(term='age', reference=62.44736842105263, df=4, grid=200 points)

Convert the curve to a DataFrame on the hazard-ratio scale:

shr.to_frame(scale="hr", format="polars")
shape: (200, 4)
agehrhr_lowerhr_upper
f64f64f64f64
39.00.2863150.0453221.808755
39.216080.3013670.0513351.769193
39.4321610.3168180.0581711.725496
39.6482410.3326540.0653741.692704
39.8643220.3488590.0732461.661556
…………
81.1356782.6397340.8398048.297414
81.3517592.7011190.8097419.010339
81.5678392.7646170.7812989.782569
81.783922.8303070.75126410.662885
82.02.8982730.72125311.646384

Methods

Name Description
to_frame() Return the smooth curve as a DataFrame.

to_frame()

Return the smooth curve as a DataFrame.

Usage

Source

to_frame(
    *,
    scale="log_hr",
    format=None,
)

Parameters

scale: str = "log_hr"

"log_hr" (the default) returns log hazard ratios and confidence bounds. "hr" returns hazard ratios (exponentiated).

format: str | None = None
Output format: None (default), "pandas", "polars", or "pyarrow".

Returns

pandas.DataFrame, polars.DataFrame, or pyarrow.Table

Examples

Export the smooth hazard ratio curve on the log-HR scale (the default):

import greenwood as gw

lung = gw.load_dataset("lung", backend="polars")
cox = gw.CoxPH().fit("Surv(time, status) ~ age + sex", data=lung)

shr = cox.smooth_hr(term="age")
shr.to_frame(format="polars")
shape: (200, 4)
agelog_hrlog_hr_lowerlog_hr_upper
f64f64f64f64
39.0-1.250662-3.0939620.592639
39.21608-1.199427-2.9693770.570524
39.432161-1.149428-2.8443710.545514
39.648241-1.100653-2.7276340.526327
39.864322-1.053087-2.6139290.507754
…………
81.1356780.970678-0.1745872.115944
81.3517590.993666-0.211042.198373
81.5678391.016902-0.2467982.280602
81.783921.040385-0.2859982.366769
82.01.064115-0.3267652.454996

Switch to the exponentiated hazard-ratio scale:

shr.to_frame(scale="hr", format="polars")
shape: (200, 4)
agehrhr_lowerhr_upper
f64f64f64f64
39.00.2863150.0453221.808755
39.216080.3013670.0513351.769193
39.4321610.3168180.0581711.725496
39.6482410.3326540.0653741.692704
39.8643220.3488590.0732461.661556
…………
81.1356782.6397340.8398048.297414
81.3517592.7011190.8097419.010339
81.5678392.7646170.7812989.782569
81.783922.8303070.75126410.662885
82.02.8982730.72125311.646384