import greenwood as gw
lung = gw.load_dataset("lung", backend="pandas").dropna(subset=["ph.ecog", "ph.karno"])
# Fit a forest and plot per-subject predicted survival
rsf = gw.RandomSurvivalForest(n_estimators=100, random_state=0).fit(
"Surv(time, status) ~ age + sex + ph.ecog + ph.karno + wt.loss", data=lung
)
gw.plot_predicted_survival(rsf, newdata=lung.head(4))plot_predicted_survival()
Plot per-subject predicted survival or cumulative-hazard curves.
Usage
plot_predicted_survival(
model,
newdata,
*,
type="survival",
times=None,
labels=None,
xlab="Time",
ylab=None,
width=500,
height=300,
backend="altair",
)Draws one right-continuous step curve per row of newdata, using any fitted model that exposes predict(newdata, type=..., times=..., format=...) returning a frame of per-subject curves — for example SurvivalTree, RandomSurvivalForest, ExtraSurvivalTrees, CoxPH, or CoxNet. This complements plot_survival, which draws population Kaplan-Meier curves, by visualizing how predicted risk varies across individuals.
Parameters
model: Any-
A fitted estimator whose
predictacceptstype=and returns atimecolumn plus one column per subject (e.g. a survival forest). newdata: Any-
Covariates for the subjects to plot (a dataframe or 2-D array), passed to
model.predict. type: str = "survival"-
Curve to draw:
"survival"(default) or"cumulative_hazard". times: Any = None-
Times at which to evaluate the curves. Defaults to the model’s training event times.
labels: Any = None-
Optional per-subject legend labels (one per row of
newdata). xlab: str = "Time"-
X-axis label (default
"Time"). ylab: str | None = None-
Y-axis label. Defaults to
"Survival probability"or"Cumulative hazard"bytype. width: int = 500-
Plot dimensions in pixels (defaults 500x300).
height: int = 500-
Plot dimensions in pixels (defaults 500x300).
backend: str = "altair"-
Plotting backend. Currently only
"altair"is supported.
Returns
altair.Chart- An interactive Altair chart with one colored step curve per subject.
Examples
Fit a random survival forest and plot survival curves for a few subjects: