[R] Arbitrary number of covariates in a formula

Mendolia, Franco fmendolia at mcw.edu
Wed Aug 11 20:15:13 CEST 2010


Thanks for your hint. This works:
form <- formula(paste("Surv( time, event,) ~",paste(covar, collapse="+"),"+strata(stratum)", sep=" ") )

Franco
________________________________________
From: David Winsemius [dwinsemius at comcast.net]
Sent: Wednesday, August 11, 2010 1:05 PM
To: Mendolia, Franco
Cc: Erik Iverson; r-help at r-project.org
Subject: Re: [R] Arbitrary number of covariates in a formula

On Aug 11, 2010, at 1:53 PM, Mendolia, Franco wrote:

>
> I could do that. However, the function f that I mentioned below is
> part of a bigger program and is nested inside another function, say
> function A. In function A I determine the covariates that I want to
> use and then call my function f. So even if I use a formula as
> single argument, I would still need to construct the formula with
> the arbitrary number of covariates which then leads to my original
> problem.

Your original example does not lend itself well to testing a
demonstration but you can make a formula along these lines;

form <- formula(paste("Surv(", time, event, ") ~",
                        paste(covar, collapse="+"),
                        "+strata(", stratum, ")",
                        sep=" ") ) )
coxph(form, data=dfrm)

I'm not claiming it will work properly with attach()ed dataframes. I
consider the attach function (at least as used with data objects) to
be a device of the Devil.

--
David.
>
> ________________________________________
> From: Erik Iverson [eriki at ccbr.umn.edu]
> Sent: Wednesday, August 11, 2010 12:00 PM
> To: Mendolia, Franco
> Cc: r-help at r-project.org
> Subject: Re: [R] Arbitrary number of covariates in a formula
>
> 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.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list