[R] Adding terms to a function

Gabor Grothendieck ggrothendieck at gmail.com
Thu Dec 7 03:41:38 CET 2006


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:
> 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.
>




More information about the R-help mailing list