Return the numbers-at-risk table as a Great Tables object.
risk_table(
km,
times=None,
)
Creates a publication-ready table showing the number of subjects at risk at specified time points, stratified by group (if fitted with strata). The table is formatted with Great Tables for interactive viewing and export to various formats.
Parameters
km: KaplanMeier
-
A fitted KaplanMeier object.
times: Any = None
-
Query times for the numbers-at-risk table. Defaults to six evenly spaced, rounded times from 0 to the maximum observed follow-up time.
Returns
great_tables.GT
-
A Great Tables object, which can be displayed in notebooks or exported.
Examples
Fit a stratified Kaplan-Meier estimator and produce a publication-ready risk table using Great Tables:
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"])
# Produce a publication-ready numbers-at-risk table
gw.risk_table(km, times=[0, 250, 500, 750, 1000])
| Numbers at Risk |
| Count of subjects at risk at each time point |
| strata |
0.0 |
250.0 |
500.0 |
750.0 |
1000.0 |
| 1 |
138 |
62 |
20 |
7 |
2 |
| 2 |
90 |
53 |
21 |
3 |
0 |