簡體   English   中英

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

[英]smooth the survival curves generated with ggsurvplot?

我是 stackoverflow 的新手,這是我的第一個問題 :) 我想知道是否有與“geom_smooth”類似的函數,但是對於使用“ggsurvplot”生成的圖。 這是我想要做的一個例子,使用 R "ovarian" 數據集:

創建一個“生存對象”:

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)

非常感謝,瓦萊里安

“繪制點之間對角線的簡單方法”可以很容易地從我們對timesurv和地層感興趣的"survfit"對象中提取出來。 兩個層都具有相同的長度,因此我們只需重復每個層 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(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")

在此處輸入圖片說明

或者使用ggplot2 ,像這樣:

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

在此處輸入圖片說明

請注意,這只是編程問題的范圍,可能需要一些關於平滑假設的討論,例如在Cross Validated 上

您不想平滑數據,而是繪制曲線而不是線條。

該方法的全部功勞歸於 @Z.Lin ( https://stackoverflow.com/a/54900769/9406040 )。 還要感謝@jay.sf 從 survfit 對象中提取數據。

數據

library(tidyverse)
library(survival)

surv_object <- Surv(time = ovarian$futime, event = ovarian$fustat)
fit1 <- survfit(surv_object ~ rx, data = ovarian)

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

d2 <- d1 %>%
  group_by(strata) %>%
  summarise(x = list(spline(time, surv, n = 200, method = "natural")[["x"]]),
            y = list(spline(time, surv, n = 200, method = "natural")[["y"]])) %>%
  tidyr::unnest(cols = c("x", "y"))

陰謀

ggplot() + 
  geom_point(data = d1,
             aes(time, surv, color = strata)) +
  geom_line(data = d2,
            aes(x, y, color = strata))

1


與平滑的比較

ggplot() + 
  geom_point(data = d1,
             aes(time, surv, color = strata)) +
  geom_smooth(data = d1,
              aes(time, surv, color = strata),
              se = FALSE)

2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM