创建一个将其参数传递给 Surv 函数(或任何其他函数)的函数
Creating a function passing its arguments to the Surv function (or any other)
请考虑以下几点:
要创建生存曲线,可以使用 survival
包的 survfit
函数。
我的目标是编写一个函数(除其他外)创建这样一条曲线,但该函数应该适用于不同的 data.frames
,其列名也不同。此外,分组变量将取决于相应的数据集。
我设法将不同的 data.frame
名称传递给函数,但为 survfit
和 Surv
函数提供列名称对我不起作用。
非常感谢任何帮助。
在我看来,这是一个不同于简单地将 data.frame
列名称传递给函数的问题,如下所述:Pass a data.frame column name to a function
# required libraries
library(survival)
library(flexsurv)
#### Examples that work without own function ===================================
# survfit wit lung data
survfit(Surv(time = time, event = status) ~ 1, data = lung)
#> Call: survfit(formula = Surv(time = time, event = status) ~ 1, data = lung)
#>
#> n events median 0.95LCL 0.95UCL
#> 228 165 310 285 363
survfit(Surv(time = time, event = status) ~ sex, data = lung)
#> Call: survfit(formula = Surv(time = time, event = status) ~ sex, data = lung)
#>
#> n events median 0.95LCL 0.95UCL
#> sex=1 138 112 270 212 310
#> sex=2 90 53 426 348 550
# survfit with bc data
survfit(Surv(time = rectime, event = censrec) ~ 1, data = bc)
#> Call: survfit(formula = Surv(time = rectime, event = censrec) ~ 1,
#> data = bc)
#>
#> n events median 0.95LCL 0.95UCL
#> 686 299 1807 1587 2030
# Create variable function that takes on data specific arguments
SurvFun <- function(fun.time, fun.event, grouping = 1, fun.dat){
survfit(Surv(time = fun.time, event = fun.event) ~ grouping, data = fun.dat)
}
#### Own function that doesn't work ============================================
# This should work for data = lung
SurvFun(fun.time = time, fun.event = status, grouping = 1, fun.dat = lung)
#> Error in Surv(time = fun.time, event = fun.event): Time variable is not numeric
由 reprex 创建于 2018-07-05
包 (v0.2.0).
当列名未被引号括起来时,它们将作为符号传递。传递符号比传递简单变量要困难得多。这也适用于公式。你需要做一些元编程才能工作。这是重新编写函数以使其正常工作的一种方法
SurvFun <- function(fun.time, fun.event, grouping = 1, fun.dat) {
params <- list(fun.time = substitute(fun.time),
fun.event = substitute(fun.event),
grouping = substitute(grouping),
fun.dat = substitute(fun.data))
expr <- substitute(survfit(Surv(time = fun.time, event = fun.event) ~ grouping,
data = fun.dat), params)
eval.parent(expr)
}
请考虑以下几点:
要创建生存曲线,可以使用 survival
包的 survfit
函数。
我的目标是编写一个函数(除其他外)创建这样一条曲线,但该函数应该适用于不同的 data.frames
,其列名也不同。此外,分组变量将取决于相应的数据集。
我设法将不同的 data.frame
名称传递给函数,但为 survfit
和 Surv
函数提供列名称对我不起作用。
非常感谢任何帮助。
在我看来,这是一个不同于简单地将 data.frame
列名称传递给函数的问题,如下所述:Pass a data.frame column name to a function
# required libraries
library(survival)
library(flexsurv)
#### Examples that work without own function ===================================
# survfit wit lung data
survfit(Surv(time = time, event = status) ~ 1, data = lung)
#> Call: survfit(formula = Surv(time = time, event = status) ~ 1, data = lung)
#>
#> n events median 0.95LCL 0.95UCL
#> 228 165 310 285 363
survfit(Surv(time = time, event = status) ~ sex, data = lung)
#> Call: survfit(formula = Surv(time = time, event = status) ~ sex, data = lung)
#>
#> n events median 0.95LCL 0.95UCL
#> sex=1 138 112 270 212 310
#> sex=2 90 53 426 348 550
# survfit with bc data
survfit(Surv(time = rectime, event = censrec) ~ 1, data = bc)
#> Call: survfit(formula = Surv(time = rectime, event = censrec) ~ 1,
#> data = bc)
#>
#> n events median 0.95LCL 0.95UCL
#> 686 299 1807 1587 2030
# Create variable function that takes on data specific arguments
SurvFun <- function(fun.time, fun.event, grouping = 1, fun.dat){
survfit(Surv(time = fun.time, event = fun.event) ~ grouping, data = fun.dat)
}
#### Own function that doesn't work ============================================
# This should work for data = lung
SurvFun(fun.time = time, fun.event = status, grouping = 1, fun.dat = lung)
#> Error in Surv(time = fun.time, event = fun.event): Time variable is not numeric
由 reprex 创建于 2018-07-05 包 (v0.2.0).
当列名未被引号括起来时,它们将作为符号传递。传递符号比传递简单变量要困难得多。这也适用于公式。你需要做一些元编程才能工作。这是重新编写函数以使其正常工作的一种方法
SurvFun <- function(fun.time, fun.event, grouping = 1, fun.dat) {
params <- list(fun.time = substitute(fun.time),
fun.event = substitute(fun.event),
grouping = substitute(grouping),
fun.dat = substitute(fun.data))
expr <- substitute(survfit(Surv(time = fun.time, event = fun.event) ~ grouping,
data = fun.dat), params)
eval.parent(expr)
}