gpt4 book ai didi

r - ggsurvplot : unable to use survfit when called from a function

转载 作者:行者123 更新时间:2023-12-02 01:16:32 25 4
gpt4 key购买 nike

我正在尝试绘制生存图,并在尝试将我的 survfit 函数移动到主函数中时遇到了问题,我可以在主函数中为不同的数据集调用它。当我运行代码时

fit<- survfit(Surv(time, status) ~ sex, data = lung)

allsurv <- function(fit){
ggsurvplot(
fit,
pval = TRUE,
pval.coord = c(200, 0.10),
conf.int = TRUE,
xlab = "Days",
ggtheme = theme_light(),
surv.median.line = "hv",
legend.labs = c("Female","Male"),
legend.title = "",
palette = c("#8C3F4D","#3E606F")) +
scale_y_continuous(expand = c(0.02, 0.02),breaks = seq(from = 0, to = 1, by = 0.1),labels=percent) +
scale_x_continuous(expand = c(0.006, 0.006),
limits = c(0,366*12), breaks = seq(0, 4392, 100))
}

allsurv(fit)

函数正常绘制enter image description here

但是,当我从函数调用 survfit 时:

fit_all <- function(x){
survfit(Surv(time, status) ~ sex, data = x)
}

allsurv(fit_all(lung))

我收到错误:“eval(fit$call$data) 中的错误:未找到对象“x””

对我做错了什么有什么想法吗?

最佳答案

Survminer 包含一个函数 surv_fit,它充当 survfit 的包装器。如果您使用 surv_fit 而不是 survfit,则返回对象的“调用”将包含整个数据框,而不仅仅是 data = x。在函数内调用 ggsurvplot 时效果更好:

https://www.rdocumentation.org/packages/survminer/versions/0.4.6/topics/surv_fit

allsurv <- function(fit){
ggsurvplot(
fit,
pval = TRUE,
pval.coord = c(200, 0.10),
conf.int = TRUE,
xlab = "Days",
ggtheme = theme_light(),
surv.median.line = "hv",
legend.labs = c("Female","Male"),
legend.title = "",
palette = c("#8C3F4D","#3E606F"))
}

fit_all <- function(x){
surv_fit(Surv(time, status) ~ sex, data = x)
}

allsurv(fit_all(lung))

关于r - ggsurvplot : unable to use survfit when called from a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50795040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com