tags:

views:

51

answers:

1

I have a data.frame, df, with n variables in it. The variables can take any name.

I want to be able to fit coxph(Surv(time_to_event,bad_outcome)~var1+var2+...+varn,data=df)

But df is a variable, its length can change. How do I write a piece of code that can fit all the variables in df regardless of the number of variables in df and the names of the variables?

Thank you so much.

+2  A: 

Read ?formula:

There are two special interpretations of ‘.’ in a formula. The usual one is in the context of a ‘data’ argument of model fitting functions and means ‘all columns not otherwise in the formula’: see ‘terms.formula’. In the context of ‘update.formula’, only, it means ‘what was previously in this part of the formula’.

Joshua Ulrich
# Thanks I've done it!# suppose names contains names = names(df)s = Surv(time_to_event$time_to_event ,bad_outcome$bad_outcome )fmla = as.formula(paste("s ~",paste(names,collapse="+")))coxph(fmla,method="breslow",data=df)
xiaodai
Why not use '.'?coxph("s ~ .", method="breslow", data=df)
Joshua Ulrich
@Joshua: Great to have you on stackoverflow!
Shane
@Shane, thanks for the welcome!
Joshua Ulrich