ehrapy.plot.kmf

ehrapy.plot.kmf(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) – The transparency level of the confidence interval. If more than one kmfs, this should be a list. Defaults to 0.3.

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

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

  • ci_legend (list[bool] | 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. Defaults to False .

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

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

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

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

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

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

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

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

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

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

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.kmf(adata[:, ["mort_day_censored"]].X, adata[:, ["censor_flg"]].X)
>>> ep.pl.kmf(
...     [kmf], color=["r"], xlim=[0, 700], ylim=[0, 1], xlabel="Days", ylabel="Proportion Survived", show=True
... )
../../_images/kmf_plot_1.png
>>> T = adata[:, ["mort_day_censored"]].X
>>> E = adata[:, ["censor_flg"]].X
>>> groups = adata[:, ["service_unit"]].X
>>> ix1 = groups == "FICU"
>>> ix2 = groups == "MICU"
>>> ix3 = groups == "SICU"
>>> kmf_1 = ep.tl.kmf(T[ix1], E[ix1], label="FICU")
>>> kmf_2 = ep.tl.kmf(T[ix2], E[ix2], label="MICU")
>>> kmf_3 = ep.tl.kmf(T[ix3], E[ix3], label="SICU")
>>> ep.pl.kmf([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