[R] refering to variable names in lm where the variable name is in another variable

From: Daniel O'Shea <dan.oshea_at_dnr.state.mn.us>
Date: Sun, 30 Dec 2007 11:49:26 -0600


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. Received on Sun 30 Dec 2007 - 17:52:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 30 Dec 2007 - 19:31:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.