I am writing a program in which I would like to take in a formula, change the
response (Y) variable into something else, and then pass the formula, with the
new Y variable to another function. That is, I am starting with
formula <- Y~X1+X2+X3
and I'd like to do something like
Y <- formula$Y
newY <- f(Y)
lm(newY~X1+X2+X3)
So far, it seems that my only option will be a very complicated sequence of
steps involving match.call(). Is there a simpler way to change the response
variable in a formula?
Thanks in advance!
Rebecca
--
Rebecca Sela
Statistics Department
Stern School of Business
New York University
Try this:
fo <- y ~ x
fo[[2]] <- as.name("z")
fo
On 10/1/07, Rebecca Sela <rsela at stern.nyu.edu>
wrote:> I am writing a program in which I would like to take in a formula, change
the response (Y) variable into something else, and then pass the formula, with
the new Y variable to another function. That is, I am starting with
> formula <- Y~X1+X2+X3
> and I'd like to do something like
> Y <- formula$Y
> newY <- f(Y)
> lm(newY~X1+X2+X3)
> So far, it seems that my only option will be a very complicated sequence of
steps involving match.call(). Is there a simpler way to change the response
variable in a formula?
>
> Thanks in advance!
>
> Rebecca
>
> --
> Rebecca Sela
> Statistics Department
> Stern School of Business
> New York University
>
> ______________________________________________
> 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.
>
Rebecca,
you can keep formula as a character vector that can be easily manipulated
formula1 <- "Y~X1+X2+X3"
formula2 <- gsub("Y","newY",formula1)
lm(as.formula(formula1),data=mydata)
lm(as.formula(formula2),data=mydata)
best,
peter
On 10/1/07, Rebecca Sela <rsela@stern.nyu.edu>
wrote:>
> I am writing a program in which I would like to take in a formula, change
> the response (Y) variable into something else, and then pass the formula,
> with the new Y variable to another function. That is, I am starting with
> formula <- Y~X1+X2+X3
> and I'd like to do something like
> Y <- formula$Y
> newY <- f(Y)
> lm(newY~X1+X2+X3)
> So far, it seems that my only option will be a very complicated sequence
> of steps involving match.call(). Is there a simpler way to change the
> response variable in a formula?
>
> Thanks in advance!
>
> Rebecca
>
> --
> Rebecca Sela
> Statistics Department
> Stern School of Business
> New York University
>
> ______________________________________________
> 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.
>
--
Peter Salzman, PhD
Department of Biostatistics and Computational Biology
University of Rochester
[[alternative HTML version deleted]]
maybe I didn't understand the problem, but you can do sth like formula<-y~X1+X2+X3 lm(update.formula(formula,f(Y)~.)) maybe ?all.vars or ?terms will help you too. hth. Rebecca Sela schrieb:> I am writing a program in which I would like to take in a formula, change the response (Y) variable into something else, and then pass the formula, with the new Y variable to another function. That is, I am starting with > formula <- Y~X1+X2+X3 > and I'd like to do something like > Y <- formula$Y > newY <- f(Y) > lm(newY~X1+X2+X3) > So far, it seems that my only option will be a very complicated sequence of steps involving match.call(). Is there a simpler way to change the response variable in a formula? > > Thanks in advance! > > Rebecca > > -- > Rebecca Sela > Statistics Department > Stern School of Business > New York University > > ______________________________________________ > 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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790
I tried formula<-y~X1+X2+X3 update.formula(formula,.-NewRandomEffects~.) and this runs without an error. So I still didn't get the point. You may also need to insulate your new response variable if you want to estimate a model with in fact only one response variable, which ist done by I() as in update.formula(formula,I(.-NewRandomEffects)~.) hrth. Rebecca Sela schrieb:> Update looks like the function I want to use, but it still isn't working, possibly because I am doing something more complicated than my message implied. > > What I want to do is subtract a vector of random effects (determined within the function) from Y. I hoped that something like this: > tree <- rpart(update(formula,.-NewRandomEffects~.)) > would work, but I got the message > Error in update.default(formula, . - NewRandomEffects ~ .) : > need an object with call component > > I have considered creating a function that would subtract the random effect from each Y individually, but there is no identifier on Y. > > Is there an obvious fix I'm missing? > > Thanks for your suggestions! > > Rebecca > > > > ----- Original Message ----- > From: "Eik Vettorazzi" <E.Vettorazzi at uke.uni-hamburg.de> > To: "Rebecca Sela" <rsela at stern.nyu.edu> > Cc: r-help at r-project.org > Sent: Monday, October 1, 2007 11:34:13 AM (GMT-0500) America/New_York > Subject: Re: [R] Disentagling formulas > > maybe I didn't understand the problem, but you can do sth like > > formula<-y~X1+X2+X3 > lm(update.formula(formula,f(Y)~.)) > > maybe > ?all.vars > or > ?terms > will help you too. > > hth. > > > Rebecca Sela schrieb: > >> I am writing a program in which I would like to take in a formula, change the response (Y) variable into something else, and then pass the formula, with the new Y variable to another function. That is, I am starting with >> formula <- Y~X1+X2+X3 >> and I'd like to do something like >> Y <- formula$Y >> newY <- f(Y) >> lm(newY~X1+X2+X3) >> So far, it seems that my only option will be a very complicated sequence of steps involving match.call(). Is there a simpler way to change the response variable in a formula? >> >> Thanks in advance! >> >> Rebecca >> >> -- >> Rebecca Sela >> Statistics Department >> Stern School of Business >> New York University >> >> ______________________________________________ >> 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. >> >> > > >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790