计算多个变量的生存 p 值

Calculate survival p values for multiple variables

我有一长串变量,我想计算每个变量的生存差异(p 值)。我使用 survfit() 和 surv_pvalue() 函数来获取结果,但我在循环变量

时遇到了一些问题
library(survminer)

set.seed(2020)
data <- data.frame(Months = 10 + rnorm(1:10),
                   Status = c(rep((0),5),rep((1),5)),
                   clin = rep("bla bla", 10),
                   Var1 = sample(0:1, 10, replace=T,prob=c(0.5,0.5)),
                   Var2 = sample(0:1, 10, replace=T,prob=c(0.5,0.5)),
                   Var3 = sample(0:1, 10, replace=T,prob=c(0.5,0.5)))

fit.list <- list()

for (i in (4:ncol(data))){
  fit <- survfit(Surv(Months, Status) ~ colnames(data)[3+i], data = data)
  fit2 <- surv_pvalue(fit)
  fit.list[[i]] <- fit2
}

结果:

Error in model.frame.default(formula = Surv(Months, Status) ~ colnames(data)[3 +  : 
  variable lengths differ (found for 'colnames(data)[3 + i]')

可能意味着 4:ncol(data)colnames(data)[3 + i] 的长度不一致,但我必须如何指定它们呢?预先感谢您提供解决方案!

您可以使用 lapply 而不是迭代和附加到列表:

lapply(data[4:6], function(i) {
  surv_pvalue(eval(call("survfit", 
                        formula = reformulate(names(data)[i], 
                                              "Surv(Months, Status)"), 
                        data = data)))
})
#> $Var1
#>   variable      pval   method pval.txt
#> 1   Months 0.3371479 Log-rank p = 0.34
#> 
#> $Var2
#>   variable      pval   method pval.txt
#> 1   Months 0.3371479 Log-rank p = 0.34
#> 
#> $Var3
#>   variable      pval   method pval.txt
#> 1   Months 0.3371479 Log-rank p = 0.34

Allan Cameron 的回答直截了当,而且效果很好。我几乎完美地使用了它:

library(survminer)

set.seed(2020)
dat <- data.frame(Months = 10 + rnorm(1:10),
                   Status = c(rep((0),5),rep((1),5)),
                   clin = rep("bla bla", 10),
                   Var1 = sample(0:1, 10, replace=T,prob=c(0.5,0.5)),
                   Var2 = sample(0:1, 10, replace=T,prob=c(0.5,0.5)),
                   Var3 = sample(0:1, 10, replace=T,prob=c(0.5,0.5)))

fit.list <- list()

for (i in (4:length(dat))){
  fit <- survfit(Surv(Months, Status) ~ dat[,i], data = dat)
  fit2 <- surv_pvalue(fit)
  fit.list[[i]] <- fit2
}