Feeding variables into model in R

133 views Asked by At

I am building survival model and my code looks something like this:

library('survival')
my.model <- coxhp(Surv(time, event) ~ var_1+var_2+var_3+var_4+var_5, data =df)

The problem is that I have too many variables and set of variables is always changing, I wonder if it would be possible to create list of variables and feed it into model. Something like this:

 my.var <- c(var_1+var_2+var_3+var_4+var_5)
 my.model <- coxhp(Surv(time, event) ~ my.var, data =df)

I found similar post for linear model linear model solution, but do not know how adapt it to coxph.

1

There are 1 answers

0
user2554330 On BEST ANSWER

You can use "." to mean "all variables not used already". So for your example,

my.model <- coxph(Surv(time, event) ~ ., data = df[,c("time", "event", my.var)])

should work.