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

kmf.plot() last point extra line to zero #327

Closed
avkitex opened this issue Aug 23, 2017 · 8 comments
Closed

kmf.plot() last point extra line to zero #327

avkitex opened this issue Aug 23, 2017 · 8 comments

Comments

@avkitex
Copy link

avkitex commented Aug 23, 2017

Last point censored could result in extra line from last value to zero (see red line on picture at ~5000 days)
image

@avkitex
Copy link
Author

avkitex commented Aug 23, 2017

I'd expect red line to end at point (4990, 0.2), not at (4990, 0)

@CamDavidsonPilon
Copy link
Owner

@avkitex This is very interesting to me. I haven't been able to reproduce the error though. Here's my test data:

        T = np.arange(0, 50, 0.5)
        E = np.random.binomial(1, 0.7, 100)
        E[-1] = 0
        kmf = KaplanMeierFitter()
        kmf.fit(T, E)

The survival curve at the last point is > 0. Are you able to send me your data for group 2, or provide a reproducible example?

@CamDavidsonPilon
Copy link
Owner

screen shot 2017-08-27 at 12 06 19

Looking at the plot too, the last point is censored, but the curve does not drop to 0.

@avkitex
Copy link
Author

avkitex commented Aug 28, 2017

image

Try this:

groups = pd.Series([1, 1, 1, 2, 2, 2, 3, 3, 3, 3])
survival = pd.DataFrame({'events': [0, 1, 0, 0, 1, 1, 0, 1, 0, 0], 'duration': [10, 20, 55, 10, 30, 40, 10, 40, 60, 70]})
palette={1:'b', 2:'r', 3:'g'}

from lifelines import KaplanMeierFitter
kmf = KaplanMeierFitter()

_, ax = plt.subplots(figsize=(8, 4))

for group in groups.groupby(groups):
    kmf.fit(survival.duration[group[1].index], survival.events[group[1].index], label=str(group[0]))
    kmf.plot(ax=ax, ci_show=False, show_censors=True, color=palette[group[0]])
ax.set_ylim((0, 1))
plt.show() 

Green and blue line end censored. While red line ends with an event. This results in extra stretch down.

@CamDavidsonPilon
Copy link
Owner

Last point censored could result in extra line from last value to zero (see red line on picture at ~5000 days)

Green and blue line end censored. While red line ends with an event. This results in extra stretch down.

Is your original post mistyped? Do you mean to say last point uncensored?

@CamDavidsonPilon
Copy link
Owner

CamDavidsonPilon commented Aug 28, 2017

R seems to agree with lifelines: it should hit 0

t <- c(10, 30, 40)
e <- c(0,1,1)
s = survfit(Surv(t, e) ~ 1)
summary(s)
Call: survfit(formula = Surv(t, e) ~ 1, type = "kaplan-meier")

 time n.risk n.event survival std.err lower 95% CI upper 95% CI
   30      2       1      0.5   0.354        0.125            1
   40      1       1      0.0     NaN           NA           NA

@avkitex
Copy link
Author

avkitex commented Sep 12, 2017

Is your original post mistyped? Do you mean to say last point uncensored?

Yes, I mistyped.

I just wanted to point, that event at the middle of the curve cases small height decrease while the single last one (the last point if uncensored) cases many times more height decrease.
As for myself I'd end the curve with small vertical line (same length as others on event). But it's my own opinion.
I've searched pubmed and found that almost all articles use r survfit and so similar to yours line end.

I guess the issue could be closed. Thanks for conversation!

@CamDavidsonPilon
Copy link
Owner

thanks for the follow up ❤️

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

No branches or pull requests

2 participants