首页 > 解决方案 > 带有置信度条的 R survplot 函数无法正常工作

问题描述

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

标签: rsurvival-analysisconfidence-interval

解决方案


我知道您说过使用该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))

这是你要找的情节吗? 在此处输入图像描述


推荐阅读