带置信度的R survplot函数无法正常工作

时间:2019-02-01 17:59:46

标签: r survival-analysis confidence-interval

我正在使用survival软件包中的survplot函数。具有置信区间的生存图很好地生成,但是现在我面临着将图转换为累积入射曲线的问题。曲线本身可以正确生成,但是使用conf = "bars"函数时,置信区间将保留在生存设置中。 "bands""diffbands"正常工作。

我给你举个简单的例子:

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")

问题出在这里

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

但是,它们可以正常工作:

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

有没有可能解决此问题的方法?我猜想ggplot2软件包可以正确地做到这一点,但是我已经使用survival软件包生成了许多生存图,因此现在更改软件包将导致很多额外的工作。

2 个答案:

答案 0 :(得分:1)

我知道您说过使用ggplot2包可能不是最好的选择,但它确实会以最少的额外编码产生答案:

library(survival)
library(rms)
library(broom)
library(dplyr)
set.seed(123)
Data <- data.frame("time" = sample(1:500), "death" = sample(c(FALSE, TRUE), 500, replace = TRUE))

Data$SurvObj <- with(Data, Surv(time, death == 1))
km.as.one <- npsurv(SurvObj ~ 1, data = Data, conf.type = "log-log")
survplot(km.as.one, fun=function(y) 1 - y, conf = "bars")

tidydf <- tidy(km.as.one) %>% 
          mutate(estimate = 1- estimate,
                 #invert estimates
                 conf.low = 1- conf.low,
                 conf.high = 1- conf.high,
                 #get points and CIs at specific timepoints
                 pointest = ifelse(row_number()%%50 != 0, NA,estimate),
                 confestlow = ifelse(row_number()%%50 != 0, NA,conf.high),
                 confesthigh = ifelse(row_number()%%50 != 0, NA,conf.low))

#plot
ggplot(tidydf)+
    geom_line(aes(x=time, y = estimate,group = 1))+
    geom_point(aes(x=time, y = pointest))+
    geom_errorbar(aes(x=time, ymin = confestlow, ymax = confesthigh))

这是您要找的情节吗? enter image description here

答案 1 :(得分:0)

如果您厌倦了等待mods到rms才能进入CRAN,则可以考虑以下相反的值:

fun

enter image description here

嘿,刚刚注意到(0,1)处的“点”。

如果不是使用对象中的三个向量,而是使用survplot(km.as.one, fun=function(y) 1-y, conf = "bars") 参数:

 getAnywhere(survplot.npsurv)

...人们发现直线已经变形,但是点和误差线却没有。如果您修改代码:

fun

....并复制到代码编辑器,然后将 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 } 的值应用于该较长函数的主体末端附近的点和错误条:

sed

enter image description here