Dear R community, I have a dataframe with say 100 different variables. I wish to regress variable 1 separately on every other variable (2-100) in a linear regression using lm. There must be an easy way to do this without loops, but I have difficulties figuring this out... Can you please help? Thank you and best regards, Georg. ***************************************** Georg Ehret Johns Hopkins University Institute of Genetic Medicine [[alternative HTML version deleted]]
I just quickly draft one with boston housing data. and it should be close to what you need. # REMOVE ALL OBJECTS rm(list = ls()); # DATA PREPARATION library(MASS); data(Boston); X<-I(as.matrix(Boston[-14])); Y<-I(as.matrix(Boston[14])); for(i in 1:13) { X2 <- X[, i] data <- data.frame(Y, X2) lm<-glm(Y~X, data = data) name<-paste("glm", i, sep = ""); assign(name, lm); }; # LIST ALL OBJECTS WITH "glm" IN THE NAMES ls(pat = "glm"); On Sun, Sep 20, 2009 at 2:05 PM, Georg Ehret <georgehret at gmail.com> wrote:> Dear R community, > ? I have a dataframe with say 100 different variables. I wish to regress > variable 1 separately on every other variable (2-100) in a linear regression > using lm. There must be an easy way to do this without loops, but I have > difficulties figuring this out... Can you please help? > Thank you and best regards, Georg. > ***************************************** > Georg Ehret > Johns Hopkins University > Institute of Genetic Medicine > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- =============================WenSui Liu Blog : statcompute.spaces.live.com Tough Times Never Last. But Tough People Do. - Robert Schuller ==============================
apologize, there is a typo in the glm() :-) On Sun, Sep 20, 2009 at 2:05 PM, Georg Ehret <georgehret at gmail.com> wrote:> Dear R community, > ? I have a dataframe with say 100 different variables. I wish to regress > variable 1 separately on every other variable (2-100) in a linear regression > using lm. There must be an easy way to do this without loops, but I have > difficulties figuring this out... Can you please help? > Thank you and best regards, Georg. > ***************************************** > Georg Ehret > Johns Hopkins University > Institute of Genetic Medicine > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- =============================WenSui Liu Blog : statcompute.spaces.live.com Tough Times Never Last. But Tough People Do. - Robert Schuller ==============================
On Sun, Sep 20, 2009 at 2:38 PM, Wensui Liu <liuwensui at gmail.com> wrote:> I just quickly draft one with boston housing data. and it should be > close to what you need. > > # REMOVE ALL OBJECTS > rm...WARNING!!! Running the code in this post could wipe out your entire workspace!!!! Please do NOT post such code.
Georg for example:>data(swiss) >data=swiss >lapply(2:length(data),function(x) lm(data[,1]~data[,x]))HTH Schalk Heunis On Sun, Sep 20, 2009 at 2:05 PM, Georg Ehret <georgehret at gmail.com> wrote:> Dear R community, > I have a dataframe with say 100 different variables. I wish to regress > variable 1 separately on every other variable (2-100) in a linear regression > using lm. There must be an easy way to do this without loops, but I have > difficulties figuring this out... Can you please help? > Thank you and best regards, Georg. > ***************************************** > Georg Ehret > Johns Hopkins University > Institute of Genetic Medicine > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >