[R] Arbitrary number of covariates in a formula
Erik Iverson
eriki at ccbr.umn.edu
Wed Aug 11 19:00:35 CEST 2010
Are you for some reason against writing your function to accept a single
argument, a formula, that you simply pass on to coxph?
Mendolia, Franco wrote:
> 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
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list