ehrapy.tools.kmf#

ehrapy.tools.kmf(durations, event_observed=None, timeline=None, entry=None, label=None, alpha=None, ci_labels=None, weights=None, censoring=None)[source]#

Fit the Kaplan-Meier estimate for the survival function.

See https://lifelines.readthedocs.io/en/latest/fitters/univariate/KaplanMeierFitter.html#module-lifelines.fitters.kaplan_meier_fitter Class for fitting the Kaplan-Meier estimate for the survival function.

Parameters:
  • durations (Iterable) – length n – duration (relative to subject’s birth) the subject was alive for.

  • event_observed (Optional[Iterable]) – True if the the death was observed, False if the event was lost (right-censored). Defaults to all True if event_observed==None.

  • timeline (Optional[Iterable]) – return the best estimate at the values in timelines (positively increasing)

  • entry (Optional[Iterable]) – Relative time when a subject entered the study. This is useful for left-truncated (not left-censored) observations. If None, all members of the population entered study when they were “born”.

  • label (Optional[str]) – A string to name the column of the estimate.

  • alpha (Optional[float]) – The alpha value in the confidence intervals. Overrides the initializing alpha for this call to fit only.

  • ci_labels (Optional[tuple[str, str]]) – Add custom column names to the generated confidence intervals as a length-2 list: [<lower-bound name>, <upper-bound name>] (default: <label>_lower_<1-alpha/2>).

  • weights (Optional[Iterable]) – If providing a weighted dataset. For example, instead of providing every subject as a single element of durations and event_observed, one could weigh subject differently.

  • censoring (Optional[Literal['right', 'left']]) – ‘right’ for fitting the model to a right-censored dataset. ‘left’ for fitting the model to a left-censored dataset (default: fit the model to a right-censored dataset).

Return type:

KaplanMeierFitter

Returns:

Fitted KaplanMeierFitter

Examples

>>> import ehrapy as ep
>>> 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)