plot_schoenfeld()

Scaled Schoenfeld residual plots for assessing proportional hazards.

Usage

Source

plot_schoenfeld(
    cox,
    *,
    transform="identity",
    show_zph=True,
    title=None,
    backend="altair",
    width=280,
    height=200,
)

Plots scaled Schoenfeld residuals against event time, one panel per covariate, with a loess smooth line and a dashed reference line at the overall coefficient estimate. Under proportional hazards, the smooth should be approximately flat. A trend indicates that the covariate’s effect changes over time.

This is the visual equivalent of R’s plot(cox.zph(fit)).

Parameters

cox: Any

A fitted CoxPH model.

transform: str = "identity"

Time-axis transformation: "identity" (default), "log", "km" (Kaplan-Meier failure probability), or "rank".

show_zph: bool = True

If True (default), annotate each panel with the Grambsch-Therneau test p-value from cox_zph().

title: str | None = None

Optional supertitle for the combined chart.

backend: Literal["altair", "plotnine"] = "altair"

Plotting backend: "altair" (default) or "plotnine".

width: int = 280

Width of each panel in pixels (Altair) or approximate inches × 72 (plotnine).

height: int = 200
Height of each panel in pixels (Altair) or approximate inches × 72 (plotnine).

Returns

alt.Chart or plotnine.ggplot
A composite chart (Altair HConcatChart) or a faceted plotnine plot.

Examples

import greenwood as gw

lung = gw.load_dataset("lung", backend="polars")
cox = gw.CoxPH().fit("Surv(time, status) ~ age + sex", data=lung)

gw.plot_schoenfeld(cox)

Use a log time axis and suppress the p-value annotation:

gw.plot_schoenfeld(cox, transform="log", show_zph=False)

Use the plotnine backend for a static ggplot object:

gw.plot_schoenfeld(cox, backend="plotnine")