Search code examples
rsurvival-analysisconfidence-interval

R survplot function with confidence bars not working correctly


I'm using survplot function from the survival package. The survival plots with confidence intervals are producing nicely, but now I've faced a problem with transforming the plots to cumulative incidence curves. The curve itself is producing correctly, but when using the conf = "bars" function the confidence intervals remain in the survival setting. The "bands" and "diffbands", however, are working correctly.

I'll bring you a simple reproducible example:

library(survival)
library(rms)
Data <- data.frame("time" = sample(1:500), "death" = sample(c(TRUE, FALSE), 500, replace = TRUE))
Data$SurvObj <- with(Data, Surv(Data$time, Data$death == 1))
km.as.one <- npsurv(SurvObj ~ 1, data = Data, conf.type = "log-log")

Here's the problem:

survplot(km.as.one, fun=function(y) 1 - y, conf = "bars")

However, these are working correctly:

survplot(km.as.one, conf = "bars")
survplot(km.as.one, fun=function(y) 1 - y, conf = "bands")

Is there any possible solutions for this problem? I guess the ggplot2 package would do this correctly, but I've produced quite a number of survival plots already with the survival package, so changing the package now would cause a lot of extra work.


Solution

  • If you are tired of waiting for teh mods to rms to make it to CRAN, you can just take the converse of the values under consideration:

    km.as.one$surv <- 1-km.as.one$surv
    km.as.one$lower <- 1-km.as.one$lower
    km.as.one$upper <- 1-km.as.one$upper
    
    survplot(km.as.one, fun=function(y) y, conf = "bars")
    

    enter image description here

    Heh, just noticed the "dot" at (0,1).

    If instead of using the hacking of the three vectors in the object one instead uses the fun parameter:

    survplot(km.as.one, fun=function(y) 1-y, conf = "bars")
    

    ... one discovers that the line gets transformed but the points and errorbars do not. If you modify the code:

     getAnywhere(survplot.npsurv)
    

    .... and copy to a code editor, then apply the value of fun to the points and error bars near the end of the body of that rather long function:

        surv.plot.npsurv <- <- function (fit, xlim, ylim, xlab, ylab, time.inc, state = NULL, 
    
        # lines 2-289 of original code  suppressed
    
        ss <- fun(v$surv[j])    # lines 290-292 when doing this in Rstudio's code editor.
        lower <- fun(v$lower[j])
        upper <- fun(v$upper[j])
    
         # rest of original code
         }
    

    enter image description here