lolibility
lolibility

Reputation: 2187

R survival analysis coxph call multiple column

I am trying to use coxph function under survival package. Normally it will be called as:

coxph(Surv(time,event) ~ age+gender+salary, data=THEDATA).

However, I have multiple columns in THEDATA. How can I call them easily? E.g, I want to build Cox model based on variables on column 8 - 12. Compared with doing

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?

Upvotes: 0

Views: 3101

Answers (1)

dardisco
dardisco

Reputation: 5274

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:

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, e.g.

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)

Upvotes: 2

Related Questions