Skip to content

Adding terms to a function

3 messages · Brooke LaFlamme, Simon Blomberg, Gabor Grothendieck

#
Hi all,

I am running R version 2.4.0 on Windows XP. I am new and have the following question:

I have a dataset of columns named x1, x2, x3...xn. I would like to write a linear regression using lm that looks like this:

lm(y~x1+x2+x3+...+xn)

If I try to use the following code, I only get the model for y~x1+xn:

 n<-ncol(dataset)
 	 model<-lm(y~x1)
	for(i in 1:n) {
		model.new<-update(model, .~.+dataset[,i])
		            }
The purpose of this is so I can use stepAIC with model.new as the upper scope and model as the lower. 

I know there must be a simple way to do this, but I am not yet familiar with much syntax. Any help appreciated!
--
Brooke LaFlamme
Cornell University
#
How about this:

form <- formula(paste("y ~", paste("x", 1:n, sep="",  collapse=" + ")))
model <- lm(form)

HTH,

Simon.
Brooke LaFlamme wrote:

  
    
#
Using the builtin anscombe data set try this where we note that
the first 4 columns are x1, ..., x4 and the fifth column is y1.

   for(i in 1:4) print(coef(lm(y1 ~., anscombe[c(1:i, 5)])))
On 12/6/06, Brooke LaFlamme <bal44 at cornell.edu> wrote: