簡體   English   中英

使用 survminer::ggsurvplot 在 r 中以編程方式繪制許多生存曲線的問題

[英]issues using survminer::ggsurvplot to plot many survival curves programmatically in r

我可以使用 ggsurvplot 繪制如下所示的單個 Kaplan-Meier 圖:

library(survminer)
library(survival)
fit1 = survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit1, data = lung)

但是,我需要以編程方式繪制許多 KM 圖。 我需要將不同的變量作為字符串傳遞。 我在下面試過。

fml = as.formula(paste('Surv(time, status)~', 'sex'))
fit2 = survfit(fml, data = lung)
ggsurvplot(fit2, data = lung)

令人驚訝的是,這不起作用。 我收到以下錯誤消息:

Error: object of type 'symbol' is not subsettable

我不知道為什么會發生這種情況。 有誰知道如何解決這一問題? 非常感謝。

由於在鏈接建議艾丹的評論,你需要使用的功能survminer::surv_fit()這大約是一個包裝survival::survfit() 所以,在你的例子中,

library(survminer)
library(survival)
 # lung is distributed as an object, survival::lung
fit1 = surv_fit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit1, data = lung)

然后可以繪制來自surv_fit(object.formula)的輸出

fml = as.formula(paste('Surv(time, status)~', 'sex'))
fit2 = surv_fit(fml, data = lung)
ggsurvplot(fit2, data = lung)

surv_fit幫助頁面中,它還顯示了如何擬合公式列表。

暫無
暫無

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

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