# plot_predicted_survival()


Plot per-subject predicted survival or cumulative-hazard curves.


Usage

``` python
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](SurvivalTree.md#greenwood.SurvivalTree), [RandomSurvivalForest](RandomSurvivalForest.md#greenwood.RandomSurvivalForest), [ExtraSurvivalTrees](ExtraSurvivalTrees.md#greenwood.ExtraSurvivalTrees), [CoxPH](CoxPH.md#greenwood.CoxPH), or [CoxNet](CoxNet.md#greenwood.CoxNet). This complements [plot_survival](plot_survival.md#greenwood.plot_survival), which draws population Kaplan-Meier curves, by visualizing how predicted risk varies across individuals.


## Parameters


`model: Any`  
A fitted estimator whose `predict` accepts `type=` and returns a `time` column 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"` by `type`.

`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:


``` python
import greenwood as gw

# Load data and build a right-censored response
lung = gw.load_dataset("lung", backend="pandas").dropna(subset=["ph.ecog", "ph.karno"])
y = gw.Surv.right(lung["time"], event=(lung["status"] == 2))
cols = ["age", "sex", "ph.ecog", "ph.karno", "wt.loss"]

# Fit a forest and plot per-subject predicted survival
rsf = gw.RandomSurvivalForest(n_estimators=100, random_state=0).fit(y, lung[cols])
gw.plot_predicted_survival(rsf, lung[cols][:4])
```


<style>
  #altair-viz-04ccdf2cbfdd45d885658d3aa8693c40.vega-embed {
    width: 100%;
    display: flex;
  }

  #altair-viz-04ccdf2cbfdd45d885658d3aa8693c40.vega-embed details,
  #altair-viz-04ccdf2cbfdd45d885658d3aa8693c40.vega-embed details summary {
    position: relative;
  }
</style>
