ehrapy.plot.kmf#

ehrapy.plot.kmf(kmfs=None, 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 (Optional[Sequence[KaplanMeierFitter]]) – Iterables of fitted KaplanMeierFitter objects.

  • ci_alpha (Optional[list[float]]) – The transparency level of the confidence interval. If more than one kmfs, this should be a list. Defaults to 0.3.

  • ci_force_lines (Optional[list[bool]]) – 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 (Optional[list[bool]]) – Show confidence intervals. If more than one kmfs, this should be a list. Defaults to True .

  • ci_legend (Optional[list[bool]]) – 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 (Optional[list[bool]]) – Show group sizes at time points. If more than one kmfs, this should be a list. Defaults to False.

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

  • grid (Optional[bool]) – If True, plot grid lines.

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

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

  • xlabel (Optional[str]) – The x-axis label text.

  • ylabel (Optional[str]) – The y-axis label text.

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

  • show (Optional[bool]) – Show the plot, do not return axis.

  • title (Optional[str]) – 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