平滑使用ggsurvplot生成的生存曲线?

问题描述 投票:2回答:1

我是stackoverflow的新手,这是我的第一个问题:)我想知道是否有与“ geom_smooth”类似的功能,但是对于使用“ggsurvplot”。这是我要使用R“卵巢”数据集进行操作的示例:

创建“生存对象”:

library(survival)
surv_object <- Surv(time = ovarian$futime, event = ovarian$fustat)

根据先前拟合的模型创建“生存曲线”:

fit1 <- survfit(surv_object ~ rx, data = ovarian)

绘制“ survfit”对象:

library(survminer)    
ggsurvplot(fit1, data = ovarian, pval = TRUE)

非常感谢,瓦莱里昂

r plot survival-analysis smoothing
1个回答
1
投票

一种“在点之间绘制对角线的简单方法”可以轻松地从我们对"survfit"time和地层感兴趣的surv对象中提取。两个层的长度相同,因此我们只需重复每个层的ID length(fit1$surv) / 2

# survfit object
library(survival)
fit1 <- survfit(Surv(time=ovarian$futime, event=ovarian$fustat) ~ rx, data=ovarian)

# extraction
d1 <- with(fit1, data.frame(time, surv, strata=rep(1:2, each=length(fit1$surv) / 2)))

然后我们可以分别绘制每个层的估计值。

cols <- c("red", "blue")
plot(d1$time, d1$surv, type="n", ylim=0:1)
sapply(1:2, function(x) with(d1[d1$strata == x, ], lines(time, surv, type="l", col=cols[x])))
legend("topright", legend=c("rx1", "rx2"), lty=1, col=cols, title="Strata")

enter image description here

或使用ggplot2,如下所示:

ggplot2::ggplot(d1, aes(x=time, y=surv, group=strata, col=strata)) +
  geom_line() +
  ylim(0:1) + 
  scale_colour_identity()

enter image description here

这仅是编程问题的一部分,可能需要对要进行的平滑假设进行一些讨论,例如在Cross Validated上。

© www.soinside.com 2019 - 2024. All rights reserved.