Hi My goal is to do a (multiple) regression, just knowing that my Y variables will be the say k first variables of a matrix/data frame. I thought I should do it with eval(parse)) but encounter a strange problem. See: lm(y~.-y, data=freeny) #that's what I want to do in the one equation case #Problem is I don't know name of the variable... only that it is the first one... #so idea is to just take first name a<-colnames(freeny) #and then use eval(parse(text=a[1])) #it works if I replace y on either the left or right side: lm(eval(parse(text=a[1]))~.-y, data=freeny) #does the same lm(y~.-eval(parse(text=a[1])), data=freeny) #but not if I do this call twice: lm(eval(parse(text=a[1]))~.-eval(parse(text=a[1])), data=freeny) #variable I wanted to remove (y) ist still there Do you understand why I can call eval(parse) only once? Should I try a update workaround? Or have idea of any other solution? Maybe there is something much simpler I'm missing:-( Thanks a lot!!! Matthieu Stigler
lm: eval(parse(text=)) works on one side y/x but not on both?
8 messages · Matthieu Stigler, Gabor Grothendieck
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091129/e208f2eb/attachment-0001.pl>
Thanks for answering so fast!!
lm(freeny)
:-) Ok that's working for the one equation case :-) Was example case... But now I want to have not only first column of freeny on the left but both first? And I don't know their names a priori... Thanks!
On Sun, Nov 29, 2009 at 8:49 AM, Matthieu Stigler
<matthieu.stigler at gmail.com <mailto:matthieu.stigler at gmail.com>> wrote:
Hi
My goal is to do a (multiple) regression, just knowing that my Y
variables will be the say k first variables of a matrix/data
frame. I thought I should do it with eval(parse)) but encounter a
strange problem.
See:
lm(y~.-y, data=freeny) #that's what I want to do in the one
equation case
#Problem is I don't know name of the variable... only that it is
the first one...
#so idea is to just take first name
a<-colnames(freeny)
#and then use eval(parse(text=a[1]))
#it works if I replace y on either the left or right side:
lm(eval(parse(text=a[1]))~.-y, data=freeny) #does the same
lm(y~.-eval(parse(text=a[1])), data=freeny)
#but not if I do this call twice:
lm(eval(parse(text=a[1]))~.-eval(parse(text=a[1])), data=freeny)
#variable I wanted to remove (y) ist still there
Do you understand why I can call eval(parse) only once? Should I
try a update workaround? Or have idea of any other solution? Maybe
there is something much simpler I'm missing:-(
Thanks a lot!!!
Matthieu Stigler
______________________________________________
R-help at r-project.org <mailto: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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091129/15bd7fa0/attachment-0001.pl>
Gabor Grothendieck a ?crit :
Try this: ix <- 1:2 lm(as.matrix(freeny[ix]) ~., freeny[-ix])
clean and clever!!! Thanks a lot!! You really simplified the code!!! Just for curiosity, do you see why parse(eval)) was not working twice in same formula? thanks a lot!! Matthieu
On Sun, Nov 29, 2009 at 8:56 AM, Matthieu Stigler
<matthieu.stigler at gmail.com <mailto:matthieu.stigler at gmail.com>> wrote:
Thanks for answering so fast!!
lm(freeny)
:-)
Ok that's working for the one equation case :-) Was example case...
But now I want to have not only first column of freeny on the left
but both first? And I don't know their names a priori...
Thanks!
On Sun, Nov 29, 2009 at 8:49 AM, Matthieu Stigler
<matthieu.stigler at gmail.com
<mailto:matthieu.stigler at gmail.com>
<mailto:matthieu.stigler at gmail.com
<mailto:matthieu.stigler at gmail.com>>> wrote:
Hi
My goal is to do a (multiple) regression, just knowing that
my Y
variables will be the say k first variables of a matrix/data
frame. I thought I should do it with eval(parse)) but
encounter a
strange problem.
See:
lm(y~.-y, data=freeny) #that's what I want to do in the one
equation case
#Problem is I don't know name of the variable... only that
it is
the first one...
#so idea is to just take first name
a<-colnames(freeny)
#and then use eval(parse(text=a[1]))
#it works if I replace y on either the left or right side:
lm(eval(parse(text=a[1]))~.-y, data=freeny) #does the same
lm(y~.-eval(parse(text=a[1])), data=freeny)
#but not if I do this call twice:
lm(eval(parse(text=a[1]))~.-eval(parse(text=a[1])),
data=freeny)
#variable I wanted to remove (y) ist still there
Do you understand why I can call eval(parse) only once?
Should I
try a update workaround? Or have idea of any other
solution? Maybe
there is something much simpler I'm missing:-(
Thanks a lot!!!
Matthieu Stigler
______________________________________________
R-help at r-project.org <mailto:R-help at r-project.org>
<mailto:R-help at r-project.org <mailto: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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091129/4fdc70ab/attachment-0001.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091129/4efa4827/attachment-0001.pl>
Yes, was wondering that other code line did not change so much :-) Thanks a lot! 2009/11/30 Gabor Grothendieck <ggrothendieck at gmail.com>:
On Sun, Nov 29, 2009 at 9:16 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
By the way, if you really do want to create the formula anyways then:
?? ix <- 1:2
?? left <- paste(names(freeny)[ix], collapse = ",")
?? fo <- as.formula(paste("cbind(", left, ") ~ ."))
?? lm(fo, freeny)
or possibly replace last line with:
?? eval(substitute(lm(fo, freeny))
which will cause the formula to appear in the lm output.
This last line should have been: This last line should have been: ?eval(substitute(lm(fo, freeny), list(fo = fo)))