Skip to content

multiple regressions on columns

6 messages · GOUACHE David, PIKAL Petr, Greg Snow

#
R-helpers,

A quick question regarding my wanting to run multiple regressions without writing a loop.
Looking at a previous discussion : 
http://tolstoy.newcastle.edu.au/R/e2/help/07/02/9740.html

my objective is to do the "opposite", i.e. instead of having the same independent variable and testing it against multiple dependent variables, my goal is to test multiple independent variables against the same dependent variable.

Using the iris dataset:

iris4 <- as.matrix(iris[,-c(1,5)])
summary(lm(iris4 ~ Sepal.Length, iris))

what I would have liked is to do the following :

summary(lm(Sepal.Length ~ iris4, iris))

and obtain the results from 3 separate regressions, as above, instead of one multiple regression...

Any clues ?

Tanks in advance

David Gouache
ARVALIS - Institut du v?g?tal
Station de La Mini?re
78280 Guyancourt
Tel: 01.30.12.96.22 / Port: 06.86.08.94.32
#
The add1 function might be what you want, there is also addterm in the MASS package and the leaps package can do some things along this line (plus more).

But before doing this, you may want to ask yourself what question you are really trying to answer, then explore if this answers that question or not.
#
Hello and thanks for your reply, but as you said, this is not really what I'm trying to do.
My purpose is not one of variable selection within a model with multiple predictors, but simply fitting a large number of models with only one predictor.
I was just hoping there would be a solution as simple as the one given in my example which gives the results of many regression models of the type Yi~x where i spans all the colums in a matrix and x is one predictor. My objective being the fitting of many regression models of the type y~Xi where i spans all the columns in a matrix and y is one dependent variable.

Best regards,

David Gouache
ARVALIS - Institut du v?g?tal
Station de La Mini?re
78280 Guyancourt
Tel: 01.30.12.96.22 / Port: 06.86.08.94.32


-----Message d'origine-----
De?: Greg Snow [mailto:Greg.Snow at imail.org] 
Envoy??: mardi 24 f?vrier 2009 18:22
??: GOUACHE David; r-help at stat.math.ethz.ch
Objet?: RE: multiple regressions on columns

The add1 function might be what you want, there is also addterm in the MASS package and the leaps package can do some things along this line (plus more).

But before doing this, you may want to ask yourself what question you are really trying to answer, then explore if this answers that question or not.
#
Hi

If you do not insist on matrix and use data frame instead

lapply(iris4,function(x) lm(iris$Sepal.Length~x))

can do it

Regards
Petr

r-help-bounces at r-project.org napsal dne 25.02.2009 09:56:25:
what I'm
predictor.
in my
Yi~x
objective
spans all
MASS
more).
are
not.
http://www.R-project.org/posting-guide.html
#
of course ! that was so obvious I didn't see it...
thanks very much and sorry for the bother

David Gouache
ARVALIS - Institut du v?g?tal
Station de La Mini?re
78280 Guyancourt
Tel: 01.30.12.96.22 / Port: 06.86.08.94.32


-----Message d'origine-----
De?: Petr PIKAL [mailto:petr.pikal at precheza.cz] 
Envoy??: mercredi 25 f?vrier 2009 10:09
??: GOUACHE David
Cc?: r-help at stat.math.ethz.ch
Objet?: Odp: [R] RE : multiple regressions on columns

Hi

If you do not insist on matrix and use data frame instead

lapply(iris4,function(x) lm(iris$Sepal.Length~x))

can do it

Regards
Petr

r-help-bounces at r-project.org napsal dne 25.02.2009 09:56:25:
what I'm
predictor.
in my
Yi~x
objective
spans all
MASS
more).
are
not.
http://www.R-project.org/posting-guide.html
#
Well add1 (and others) does fit the regressions you asked about if you give it the base model only including the intercept and the scope including the x variables of interest.  Unfortunately it only returns certain statistics on those models, not the whole object, but if you were interested in the test of significance, then the F that you could ask to be returned would give that to you (you could also modify the function to return any additional information that you want/need).  You did not specify what information from the fits you wanted, so add1 was a possibility if it matched with what you wanted.

I have made a note to my future self asking future me to use the timetravel package to send a copy of the ESP package back in time to past me to help with answering posts.  However, present me has not received it yet.  Possibly near future me does something that results in far future me not wanting to cooperate, I guess I should stick to my diet a little better.

It looks like you found the lapply solution, another option would be to stack the data and use the lmList function from the nlme package, or the by function, or ..., but if lapply works for you, it is probably not worth the effort to try the others.