r - 带有置信度条的 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
包制作了很多生存图,所以现在改变这个包会导致很多额外的工作。
解决方案
我知道您说过使用该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))
推荐阅读
- python - 如何在 tkinter 上使用填充圆角按钮
- excel - ADO 连接到打开的 Excel 工作簿
- html - 在Vue中输入文本字段时光标跳出
- tableau-api - 使用 REST API 将 PLM 工具连接到 Tableau
- javascript - 使用 JS 在悬停时显示文本
- javascript - 替换所有 HREF 标记的字符串将从 HREF 内部取值
- reactjs - 将对象传递给子组件会丢失其属性和方法
- reactjs - 抽屉标题中的antd浮动图标
- here-api - HERE 交通 api - 获取历史交通数据
- c# - IWorkbook.SaveToStream 抛出 System.IndexOutOfRangeException