question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KM at_risk_counts intervals do not align with manually selected plot interval

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CamDavidsonPiloncommented, Dec 3, 2020

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()
0reactions
CamDavidsonPiloncommented, Dec 7, 2021

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kaplan-Meier Survival Plotting Macro %NEWSURV
Confidence intervals are automatically added for graphs with no CLASS variable. Confidence intervals can be added as a filled background, as ...
Read more >
lifelines Documentation - Read the Docs
We can call plot() on the KaplanMeierFitter itself to plot both the KM estimate and its confidence intervals: kmf.plot_survival_function().
Read more >
sts graph, risktable(numlist) - Title Description Quick start Menu
atrisk show numbers at risk at beginning of each interval censored(single) show one hash mark at each censoring time, no matter what number...
Read more >
lifelines/CHANGELOG.md at master - GitHub
Fixed an annoying bug where at_risk-table label's were not aligning properly when data spanned ... with intervals when no events would occur in...
Read more >
AGENDA - KEYS Energy Services
a) Approve Resolution No. 21-01, Adopting the Monroe County and Incorporated. Municipalities Local Mitigation Strategy, 2020 Update.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found