Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KM at_risk_counts intervals do not align with manually selected plot interval #1184

Closed
kadufendach opened this issue Dec 3, 2020 · 5 comments
Labels

Comments

@kadufendach
Copy link

I am trying to generate a K-M curve over 60 months with interval 12 (instead of 10, which is automatically selected). As demonstrated below, at_risk_counts do not align with 12 month intervals which I've manually set using set_xticks.

KM_issue

Example code is as follows:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from lifelines import KaplanMeierFitter

df = pd.DataFrame({
    'durations': [5, 32, 14, 55, 18, 29, 28, 39, 2, 1, 60, 7],
    'events': [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0]})

D = df['durations']
E = df['events']

kmf = KaplanMeierFitter() 
kmf.fit(D, E,label="Kaplan-Meier Estimate")

ax = kmf.plot(ci_show=False, at_risk_counts=True) ## want at_risk_counts to be 12 month intervals
plt.tight_layout()

ax.set_xticks(range(0,61,12)) ## set x-axis ticks to represent 12 month intervals
ax.set_xlabel('Months')
ax.grid(b=True, axis='y')
plt.show()

I have also tried adding timeline=range(0,61,12) to kmf.fit, but this does not change the at_risk_counts interval. For ex:

kmf.fit(D, E, label="Kaplan-Meier Estimate", timeline=range(0,61,12))
ax = kmf.plot(ci_show=False, at_risk_counts=True) ## want at_risk_counts to be 12 month intervals
plt.tight_layout()

ax.set_xticks(range(0,61,12)) ## set x-axis ticks to represent 12 month intervals
ax.set_xlabel('Months')
ax.grid(b=True, axis='y')
plt.show()

Thank you in advance for any help.

@CamDavidsonPilon
Copy link
Owner

Hi @kadufendach, try this:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from lifelines import KaplanMeierFitter

df = pd.DataFrame({
    'durations': [5, 32, 14, 55, 18, 29, 28, 39, 2, 1, 60, 7],
    'events': [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0]})

D = df['durations']
E = df['events']

kmf = KaplanMeierFitter() 
kmf.fit(D, E,label="Kaplan-Meier Estimate")

ax = kmf.plot(ci_show=False) 
plt.tight_layout()

ax.set_xticks(range(0,61,12)) 

# now add the table:
from lifelines.plotting import add_at_risk_counts
add_at_risk_counts(kmf, ax=ax)

ax.set_xlabel('Months')
ax.grid(b=True, axis='y')
plt.show()

@kadufendach
Copy link
Author

Worked perfectly @CamDavidsonPilon, thanks for your help!

@CamDavidsonPilon
Copy link
Owner

Glad to hear - I'm going to reopen this so it remind me to add a note to the documentation on this.

@chrispoptic
Copy link

Hi @CamDavidsonPilon I'm in a similar situation where I'm passing in the at_risk_counts parameter to True in the call to plot_survival_function() rather than calling the add_at_risk_counts() on that KMfitter.

Is it possible to specify the xticks intervals for the survival table in the plot_survival_function() function call?

@CamDavidsonPilon
Copy link
Owner

Hi @chrispoptic,

I'm hestitant to add a new kwarg to plot_survival_function - but here's something you can try to specify the interval:

interval = 5
ax = plt.subplot()
ax.set_xticks(np.arange(0, 100, interval))

kmf = KaplanMeierFitter(...)
kmf.plot(ax=ax, at_risk_counts=True)

plt.show()

Something like that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants