[R] Adding terms to a function

Simon Blomberg blomsp at ozemail.com.au
Thu Dec 7 00:30:07 CET 2006


How about this:

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

HTH,

Simon.

Brooke LaFlamme wrote:
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>   


-- 
Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. 
Centre for Resource and Environmental Studies
The Australian National University              
Canberra ACT 0200                               
Australia                                       
T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au
F: +61 2 6125 0757
CRICOS Provider # 00120C

The combination of some data and an aching desire for 
an answer does not ensure that a reasonable answer 
can be extracted from a given body of data.
- John Tukey.




More information about the R-help mailing list