pythonlifelines

How to indicate Kaplan-Meier Fitter (python) to plot 90% of datapoints to avoid sudden drops


I am writing some python code to do Kaplan-Meier (KM) curves using the KM Fitter and usually plot 4 curves in the same graph to compare different groups. The basic way to get a KM curve is:

from lifelines import KaplanMeierFitter

#Create the KMF object
KM_curve = KaplanMeierFitter()

#Give data to object. Status is 0 if alive, 1 if deceased (in my case)
KM_curve.fit (durations=My_Data["Time"], event_observed=My_Data["Status"])

#I do a figure in which I use this line 4 times (one per group)
KM_curve.plot(ci_show=False)

With those 4 lines of code and a pandas dataframe (here called My_Data) the KM Fitter automatically does all the calculations and plotting, but I was wondering if anyone knows how to stop the curve prematurely. I have done around 50 different graphs, they look nice and give me the info I need, but sometimes the last part of some curves dramatically drops to 0% (vertically) or very close to it. That is weird since none of my groups has 0 survivors at the end of my x-axis [See in this example, the red line https://i.sstatic.net/bn6Vy.png ]

I did read that the KM curves are good to see trends in the middle section, but the last part of the curves may be misleading and has to be examined carefully. That is especially true if there are not enough patients left in that group and thus, the %survival estimate drops dramatically. Someone who does bioinformatics told me she usually stops plotting the curve whenever 10% of patients are left, to prevent this issue. Is it possible to do that in python KMF?


Solution

  • There are few ways to achieve this:

    1.

    ax = KM_curve.plot(ci_show=False)
    ax.set_xlim(0, <your upper limit here>)
    
    KM_curve.plot(ci_show=False, loc=slice(0, <your upper limit here>))
    

    See more documentation on loc and iloc here: https://lifelines.readthedocs.io/en/latest/fitters/univariate/KaplanMeierFitter.html#lifelines.fitters.kaplan_meier_fitter.KaplanMeierFitter.plot