Return a tidy frame of the number at risk per stratum at each of times.
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)| strata | time | n_risk |
|---|
| str | f64 | f64 |
| "1" | 0.0 | 138.0 |
| "1" | 250.0 | 62.0 |
| "1" | 500.0 | 20.0 |
| "1" | 750.0 | 7.0 |
| "1" | 1000.0 | 2.0 |
| "2" | 0.0 | 90.0 |
| "2" | 250.0 | 53.0 |
| "2" | 500.0 | 21.0 |
| "2" | 750.0 | 3.0 |
| "2" | 1000.0 | 0.0 |