viz.get_risk_table_frame()

Return a tidy frame of the number at risk per stratum at each of times.

Usage

Source

viz.get_risk_table_frame(
    km,
    times=None,
    *,
    format=None,
)

Parameters

km: KaplanMeier

A fitted KaplanMeier estimator.

times: Any = None

Query times for the numbers-at-risk table. Defaults to an automatic grid.

format: str | None = None
Output format: None (default), "pandas", "polars", or "pyarrow". When None, a backend is auto-detected (Polars, then Pandas, then PyArrow).

Returns

DataFrame
A tidy frame with columns strata, time, and n_risk (one row per stratum per time point).

Examples

Fit a stratified Kaplan-Meier estimator on the bundled lung dataset, then tabulate the number of subjects still at risk in each group at a chosen set of times. This returns the numbers as a tidy frame (one row per stratum and time).

import greenwood as gw

# Load data and fit a stratified Kaplan-Meier estimator
lung = gw.load_dataset("lung", backend="polars")
y = gw.Surv.right(lung["time"], event=(lung["status"] == 2))
km = gw.KaplanMeier().fit(y, by=lung["sex"])

# Get the numbers at risk as a tidy Polars DataFrame
gw.get_risk_table_frame(km, times=[0, 250, 500, 750, 1000], format="polars")
shape: (10, 3)
stratatimen_risk
strf64f64
"1"0.0138.0
"1"250.062.0
"1"500.020.0
"1"750.07.0
"1"1000.02.0
"2"0.090.0
"2"250.053.0
"2"500.021.0
"2"750.03.0
"2"1000.00.0