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

R survplot function with confidence bars not working correctly

我正在使用 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 包制作了很多生存图,所以现在改变包会导致很多额外的工作。

我知道你说使用 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))

这是您要找的剧情吗?

如果您厌倦了等待 mods 到 rms 使其进入 CRAN,您可以只考虑以下值的逆向:

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

嘿,刚注意到 (0,1) 处的 "dot"。

如果不是使用对象中的三个向量的黑客攻击,而是使用 fun 参数:

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
     }