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
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.
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. ________________________________________ 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.
Henrique Dallazuanna
2010-Aug-11 18:26 UTC
[R] Arbitrary number of covariates in a formula
This shoul work: coxph(Surv(intx, status) ~ . + strata(sex) - sex, test1) On Wed, Aug 11, 2010 at 1:54 PM, Mendolia, Franco <fmendolia@mcw.edu> 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
suppose covnames is a vector containing your covariate names (e.g. as
character strings)
Then (warning: untested)
rhs <- paste(covnames, collapse="+")
## makes them into a single string separated by "+"-es and
form <- formula(paste("y", rhs, sep="~"))
## creates your formula.
?substitute can also be useful for this.
-- Bert Gunter
Genentech Nonclinical Biostatistics
On Wed, Aug 11, 2010 at 10:53 AM, Mendolia, Franco <fmendolia at mcw.edu>
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.
>
> ________________________________________
> 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.
>