Linear regression cycle for each independent variable separately from the dependent

I want to figure out how to create a loop or use one of the functions used to get individual 1: 1 regression information for each variable in the data set relative to the dependent variable.

Let's say I use mtcars. How will I write in R code that takes every variable in a data frame and regresses it against MPG?

Better yet, get a summary of each independent variable c and have some kind of name assignment such as x1 =, x2 = etc

summary(lm(mpg~eachvar,data=mtcars)) 
+7
r statistics
source share
3 answers

Hi try something like this:

 models <- lapply(paste("mpg", names(mtcars)[-1], sep = "~"), formula) res.models <- lapply(models, FUN = function(x) {summary(lm(formula = x, data = mtcars))}) names(res.models) <- paste("mpg", names(mtcars)[-1], sep = "~") res.models[["mpg~disp"]] # Call: # lm(formula = x, data = mtcars) # Residuals: # Min 1Q Median 3Q Max # -4.8922 -2.2022 -0.9631 1.6272 7.2305 # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 29.599855 1.229720 24.070 < 2e-16 *** # disp -0.041215 0.004712 -8.747 9.38e-10 *** # --- # Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # Residual standard error: 3.251 on 30 degrees of freedom # Multiple R-squared: 0.7183, Adjusted R-squared: 0.709 # F-statistic: 76.51 on 1 and 30 DF, p-value: 9.38e-10 
+3
source share

This will do it for you.

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

The data.frame object is a list with some other functions, so it will go through each mtcars column, excluding the first one, and performing regressions. If you save the resulting list in something like L , you can easily access each of them simply by using the same name or number as the column in the source data.frame file. So L$cyl gives a regression summary for mpg on cyl .

+10
source share

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 code explanations

  • data.table converts mtcars to a data.table object
  • .SD also a data.table object that contains the columns you need to work with
  • .SDcols = -1 tells .SD not to use the first column (since we don't want to put lm(mpg ~ mpg)
  • lapply just runs the model across all columns in .SD (except for the one we skipped) and returns list objects of classes

Fit resume list, you can check them using

 Fits$MyFits 

But you can also work on them, for example, using the coef function on each attack

 Fits[, lapply(MyFits, coef)] 

Or get r.squered

 Fits[, lapply(MyFits, '[[', "r.squared")] 
+5
source share

All Articles