[R] Arbitrary number of covariates in a formula

Mendolia, Franco fmendolia at mcw.edu
Wed Aug 11 18:54:30 CEST 2010


Hello!

I have something like this:

test1 <- data.frame(intx=c(4,3,1,1,2,2,3), 
                   status=c(1,1,1,0,1,1,0), 
                   x1=c(0,2,1,1,1,0,0), 
                   x2=c(1,1,0,0,2,2,0), 
                   sex=c(0,0,0,0,1,1,1)) 
                   
and I can easily fit a cox model:

library(survival)
coxph(Surv(intx,status) ~ x1 + x2 + strata(sex),test1)

However, I want to write my own function, fit the model inside this function and then do some further computations.

f <- function(time, event, stratum, covar )
{

  fit <- coxph(Surv(time,event) ~ covar[[1]] + covar[[2]] + strata(stratum))
  fit
  #... do some other stuff
}

attach(test1)
f(intx, status, sex, list(x1,x2))

This works fine when I have exactly two covariates. However, I would like to have something that I can use with an arbitrary number of covariates. More precisely, I need something more general than covar[[1]] + covar[[2]].

Any ideas?

Thanks,
Franco


More information about the R-help mailing list