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

plotting confidence intervals for coxph using ggsurvplot #393

Closed
kharknes opened this issue May 27, 2019 · 6 comments
Closed

plotting confidence intervals for coxph using ggsurvplot #393

kharknes opened this issue May 27, 2019 · 6 comments

Comments

@kharknes
Copy link

Expected behavior

Using the example provided here (http://www.sthda.com/english/wiki/cox-proportional-hazards-model), I have been trying to plot two strata with confidence intervals after running a coxph. This is the example code, with data=lung added to the ggsurvplot as that was indicated as a needed fix:

data("lung")
res.cox <- coxph(Surv(time, status) ~ sex, data = lung)
res.cox
res.cox <- coxph(Surv(time, status) ~ age + sex + ph.ecog, data = lung)
summary(res.cox)
ggsurvplot(survfit(res.cox), data=lung, color = "#2E9FDF",
ggtheme = theme_minimal())
sex_df <- with(lung,
data.frame(sex = c(1, 2),
age = rep(mean(age, na.rm = TRUE), 2),
ph.ecog = c(1, 1)
)
)
sex_df
fit <- survfit(res.cox, newdata = sex_df)
ggsurvplot(fit, data=lung, conf.int = TRUE, legend.labs=c("Sex=1", "Sex=2"),
ggtheme = theme_minimal())

It should produce a nice graph like this:

Screen Shot 2019-05-27 at 6 32 01 PM

Actual behavior

However, the code is producing this, with the confidence intervals all bunched up at the top:

Screen Shot 2019-05-27 at 6 33 18 PM

When digging into what the survfit function is doing, it appears that the error is located there:

res.sum <- surv_summary(fit)
head(res.sum)

time n.risk n.event n.censor surv std.err upper lower strata
1 5 227 1 0 0.9949674 0.005051885 1 0.9851643 1
2 11 226 3 0 0.9799013 0.005051885 1 0.9851643 1
3 12 223 1 0 0.9748685 0.005051885 1 0.9851643 1
4 13 222 2 0 0.9647492 0.005051885 1 0.9851643 1
5 15 220 1 0 0.9596720 0.005051885 1 0.9851643 1
6 26 219 1 0 0.9546036 0.005051885 1 0.9851643 1

It then passes on a uniform confidence interval instead of one that changes by step. Is there a fix for this? I would really like to be able to graph confidence intervals for the coxph.

Steps to reproduce the problem

session_info()

# please paste here the result of
devtools::session_info()
@camposfa
Copy link

I have the same issue. It appears to be a bug introduced in version 0.4.4 (2019-05-21), as reverting to version 0.4.3 fixed the problem for me.

@stefani-odonoghue
Copy link

I reverted to version 0.4.3, but this states the R version 3.6 and 3.53 are not compatible. What R source version are you using?

@camposfa
Copy link

I'm using R 3.6.0. Try using:

require(devtools)
devtools::install_version("survminer", version = "0.4.3", repos = "http://cran.us.r-project.org")

And then restart your R session.

@kassambara
Copy link
Owner

kassambara commented Jun 10, 2019

Hi,

Thank you for pointing this issue out.

A bug has been introduced by the pull request #335

In this pull request, ifelse() has been used to test if the upper and the lower conf.int are null :

 res <- cbind(res, surv = .flat(x$surv),
                   std.err = ifelse(is.null(x$std.err), NA, .flat(x$std.err)),
                   upper = ifelse(is.null(x$upper), NA, .flat(x$upper)),
                   lower = ifelse(is.null(x$lower), NA, .flat(x$lower)))

The documentation of ifelese states that :

ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.

So, since we are passing test values of length 1, we are getting results of length 1, leading to a uniform confidence interval for all rows.

I'm fixing that

@kassambara
Copy link
Owner

Fixed now, thanks.

Install the latest dev version as follow:

devtools::install_github("kassambara/survminer", build_vignettes = FALSE)

@olgasparyan
Copy link

Hi! I installed the last package version provided in here. But I still have a flat confidence interval at the value=1. Thanks!

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

5 participants