ehrapy.plot.kaplan_meier

ehrapy.plot.kaplan_meier(kmfs, ci_alpha=None, ci_force_lines=None, ci_show=None, ci_legend=None, at_risk_counts=None, color=None, grid=False, xlim=None, ylim=None, xlabel=None, ylabel=None, figsize=None, show=None, title=None)[source]

Plots a pretty figure of the Fitted KaplanMeierFitter model

See https://lifelines.readthedocs.io/en/latest/fitters/univariate/KaplanMeierFitter.html

Parameters:
  • kmfs (Sequence[KaplanMeierFitter]) – Iterables of fitted KaplanMeierFitter objects.

  • ci_alpha (list[float] | None, default: None) – The transparency level of the confidence interval. If more than one kmfs, this should be a list.

  • ci_force_lines (list[bool] | None, default: None) – Force the confidence intervals to be line plots (versus default shaded areas). If more than one kmfs, this should be a list.

  • ci_show (list[bool] | None, default: None) – Show confidence intervals. If more than one kmfs, this should be a list.

  • ci_legend (list[bool] | None, default: None) – If ci_force_lines is True, this is a boolean flag to add the lines’ labels to the legend. If more than one kmfs, this should be a list.

  • at_risk_counts (list[bool] | None, default: None) – Show group sizes at time points. If more than one kmfs, this should be a list.

  • color (list[str] | None, default: None) – List of colors for each kmf. If more than one kmfs, this should be a list.

  • grid (bool | None, default: False) – If True, plot grid lines.

  • xlim (tuple[float, float] | None, default: None) – Set the x-axis view limits.

  • ylim (tuple[float, float] | None, default: None) – Set the y-axis view limits.

  • xlabel (str | None, default: None) – The x-axis label text.

  • ylabel (str | None, default: None) – The y-axis label text.

  • figsize (tuple[float, float] | None, default: None) – Width, height in inches.

  • show (bool | None, default: None) – Show the plot, do not return axis.

  • title (str | None, default: None) – Set the title of the plot.

Return type:

Axes | None

Examples

>>> import ehrapy as ep
>>> import numpy as np
>>> adata = ep.dt.mimic_2(encoded=False)

# Because in MIMIC-II database, censor_fl is censored or death (binary: 0 = death, 1 = censored). # While in KaplanMeierFitter, event_observed is True if the the death was observed, False if the event was lost (right-censored). # So we need to flip censor_fl when pass censor_fl to KaplanMeierFitter

>>> adata[:, ["censor_flg"]].X = np.where(adata[:, ["censor_flg"]].X == 0, 1, 0)
>>> kmf = ep.tl.kaplan_meier(adata, "mort_day_censored", "censor_flg")
>>> ep.pl.kaplan_meier(
...     [kmf], color=["r"], xlim=[0, 700], ylim=[0, 1], xlabel="Days", ylabel="Proportion Survived", show=True
... )
../../_images/kmf_plot_1.png
>>> groups = adata[:, ["service_unit"]].X
>>> adata_ficu = adata[groups == "FICU"]
>>> adata_micu = adata[groups == "MICU"]
>>> adata_sicu = adata[groups == "SICU"]
>>> kmf_1 = ep.tl.kaplan_meier(adata_ficu, "mort_day_censored", "censor_flg", label="FICU")
>>> kmf_2 = ep.tl.kaplan_meier(adata_micu, "mort_day_censored", "censor_flg", label="MICU")
>>> kmf_3 = ep.tl.kaplan_meier(adata_sicu, "mort_day_censored", "censor_flg", label="SICU")
>>> ep.pl.kaplan_meier([kmf_1, kmf_2, kmf_3], ci_show=[False,False,False], color=['k','r', 'g'],
>>>           xlim=[0, 750], ylim=[0, 1], xlabel="Days", ylabel="Proportion Survived")
../../_images/kmf_plot_2.png