Daniel O'Shea
2007-Dec-30 17:49 UTC
[R] refering to variable names in lm where the variable name is in another variable
I am trying to refer to a variable name in a lm regression where the variable name is in another variable, but it does seem to work. Here is an example: y<-rnorm(10) dat<-data.frame(x1=rnorm(10),x2=rnorm(10),x3=rnorm(10)) nam<-c('x1','x2','x3') library(gtools) com<-combinations(3,2,1:3) mod<-lm(y~nam[com[1,1]],data=dat) #error in model frame....:variable lengths differ(). I also get the error if i just refer to variable x1 as nam[1] in the lm. any suggestions. I am trying to set up a for loop that will perform an all subsets regression and calculate the AIC for each. Dan
Charilaos Skiadas
2007-Dec-30 18:22 UTC
[R] refering to variable names in lm where the variable name is in another variable
On Dec 30, 2007, at 12:49 PM, Daniel O'Shea wrote:> I am trying to refer to a variable name in a lm regression where > the variable name is in another variable, but it does seem to > work. Here is an example: > > y<-rnorm(10) > dat<-data.frame(x1=rnorm(10),x2=rnorm(10),x3=rnorm(10)) > nam<-c('x1','x2','x3') > library(gtools) > com<-combinations(3,2,1:3) > mod<-lm(y~nam[com[1,1]],data=dat) > > #error in model frame....:variable lengths differ(). > > I also get the error if i just refer to variable x1 as nam[1] in > the lm. any suggestions. I am trying to set up a for loop that > will perform an all subsets regression and calculate the AIC for each.There's probably a number of ways to go about it. The problem is that nam[1] is a string vector, while you want the underlying "symbol". In your case, I think the simplest solution would be: frm <- formula(paste("y~",nam[1])) mod<-lm(frm,data=dat) This would also work: frm <- bquote(y~.(var1), list(var1=as.name(nam[1]))) mod<-lm(frm,data=dat) This however does have a possible sideeffect, as in the following: frm <- bquote(y~.(var1), list(var1=as.name(nam[2]))) mod2 <- update(mod) mod2 is now the model for x2, even though we didn't give it an explicit new formula (The old formula had kept the difference in frm. You can probably avoid this by: mod<-lm(bquote(y~.(var1), list(var1=as.name(nam[1]))),data=dat) Though again the name of the formula is not very pretty. The best one, from the point of view of getting the correct call in lm, probably is: eval(bquote(lm(y~.(var1), data=dat), list(var1=as.name(nam[1]))))> DanHope this helps, Haris Skiadas Department of Mathematics and Computer Science Hanover College
John Kane
2008-Jan-02 17:27 UTC
[R] refering to variable names in lm where the variable name is in another variable
Have a look at "nam[com[1,1]]" , You're regressing against a character. Try mod<-lm(y~dat[, com[1,1]],data=dat) --- Daniel O'Shea <dan.oshea at dnr.state.mn.us> wrote:> I am trying to refer to a variable name in a lm > regression where the variable name is in another > variable, but it does seem to work. Here is an > example: > > y<-rnorm(10) >dat<-data.frame(x1=rnorm(10),x2=rnorm(10),x3=rnorm(10))> nam<-c('x1','x2','x3') > library(gtools) > com<-combinations(3,2,1:3) > mod<-lm(y~nam[com[1,1]],data=dat) > > #error in model frame....:variable lengths differ(). > > I also get the error if i just refer to variable x1 > as nam[1] in the lm. any suggestions. I am trying > to set up a for loop that will perform an all > subsets regression and calculate the AIC for each. > > Dan > > ______________________________________________ > 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. >