[英]R survival analysis coxph call multiple column
I am trying to use coxph
function under survival
package. 我正在尝试在
survival
包下使用coxph
函数。 Normally it will be called as: 通常将其称为:
coxph(Surv(time,event) ~ age+gender+salary, data=THEDATA)
. coxph(Surv(time,event) ~ age+gender+salary, data=THEDATA)
。
However, I have multiple columns in THEDATA
. 但是,我在
THEDATA
有多个列。 How can I call them easily? 我如何轻松地给他们打电话? Eg, I want to build Cox model based on variables on column 8 - 12. Compared with doing
例如,我想基于第8-12列中的变量构建Cox模型。
coxph(Surv(THEDATA$time,THEDATA$event)~ THEDATA[,8] + THEDATA[,9] + THEDATA[,10] + THEDATA[,11] + THEDATA[,12])
, how to use a more efficient code to do the job? ,如何使用更高效的代码来完成这项工作?
This is why variable names should be as short as possible. 这就是为什么变量名应尽可能短的原因。
library(survMisc)
### reproducible data
set.seed(1)
### 12 variables (no factors for simplicity)
df1 <- genSurvDf(f=0, c=10)$df
c2 <- colnames(df1)[1:12]
### loop through each variable
for (i in 1:length(c2)){
print(c2[i])
print(coxph(Surv(t1, e) ~ get(c2[i]), data=df1))
}
This is adapted from ?formula
: 这是从
?formula
改编而来的:
f1 <- as.formula(paste("Surv(t1, e) ~ ",
paste(c2, collapse= "+")))
coxph(f1, data=df1)
You should be able to modify the above to suit your needs, eg 您应该能够修改以上内容以适合您的需求,例如
f1 <- as.formula(paste("Surv(t1, e) ~ ",
paste(c2[8:12], collapse= "+")))
If you want to do all combinations (up to a certain no.), which is practical for small datasets, this may be more efficient: 如果要对小型数据集进行所有组合(最多确定一个编号),这可能会更有效:
c1 <- coxph(Surv(t1, e) ~ ., data=df1)
### check all combinations of up to 3
### sort by information criteria
multi(c1, maxCoef=3, how="all", confSetSize=Inf)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.