Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear Regression loop for each independent variable individually against dependent

Tags:

r

statistics

I want to figure out how to create a loop or using one of the apply functions to get individual 1:1 regression information for each variable in a dataset against the dependent variable.

Lets say I am using mtcars. How would I write in R code that takes each variable in the data frame and regresses it against MPG?

Even better would be getting a summary of each independent variable with and having some sort of name assignment such as x1=, x2=etc

summary(lm(mpg~eachvar,data=mtcars))
like image 334
runningbirds Avatar asked Jul 30 '14 11:07

runningbirds


Video Answer


2 Answers

This will do it for you.

lapply( mtcars[,-1], function(x) summary(lm(mtcars$mpg ~ x)) )

A data.frame object is a list with some other features so this will go through each column of mtcars excluding the first one and perform the regressions. If you save the resulting list in something like L then you can access each one easily by just using the same name or number as the column in the original data.frame. So L$cyl gives the regression summary for mpg on cyl.

like image 197
John Avatar answered Oct 25 '22 11:10

John


A data.table version of Johns solution

library(data.table)
Fits <- 
    data.table(mtcars)[, 
              .(MyFits = lapply(.SD, function(x) summary(lm(mpg ~ x)))), 
              .SDcols = -1]

Some explanations of the code

  • data.table will convert mtcars to a data.table object
  • .SD is also a data.table object which contains the columns one wants to operate on
  • .SDcols = -1 tells .SD not to use first column (as we don't want to fit lm(mpg ~ mpg)
  • lapply just runs the model over all the columns in .SD (except the one we skipped) and returns objects of class list

Fit will a be list of summaries, you can inspect them using

Fits$MyFits

But you can also operate on them, for example, applying coef function on each fit

Fits[, lapply(MyFits, coef)]

Or getting the r.squered

Fits[, lapply(MyFits, `[[`, "r.squared")]
like image 28
David Arenburg Avatar answered Oct 25 '22 11:10

David Arenburg