Creating a regression loop for a model and store results

See the FAQ: How to do a minimal reproducible example reprex for beginners to get a more directed answer.

To begin, replace column names with labels that do not include blanks or other special characters. The time for long, descriptive labels is when preparing presentation tables.

The example below illustrates the process for extracting the mean of intercept terms of several models.

fit <- lm(mpg ~ hp + drat, data = mtcars)
summary(fit)
#> 
#> Call:
#> lm(formula = mpg ~ hp + drat, data = mtcars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -5.0369 -2.3487 -0.6034  1.1897  7.7500 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 10.789861   5.077752   2.125 0.042238 *  
#> hp          -0.051787   0.009293  -5.573 5.17e-06 ***
#> drat         4.698158   1.191633   3.943 0.000467 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.17 on 29 degrees of freedom
#> Multiple R-squared:  0.7412, Adjusted R-squared:  0.7233 
#> F-statistic: 41.52 on 2 and 29 DF,  p-value: 3.081e-09
str(summary(fit))
#> List of 11
#>  $ call         : language lm(formula = mpg ~ hp + drat, data = mtcars)
#>  $ terms        :Classes 'terms', 'formula'  language mpg ~ hp + drat
#>   .. ..- attr(*, "variables")= language list(mpg, hp, drat)
#>   .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. ..$ : chr [1:3] "mpg" "hp" "drat"
#>   .. .. .. ..$ : chr [1:2] "hp" "drat"
#>   .. ..- attr(*, "term.labels")= chr [1:2] "hp" "drat"
#>   .. ..- attr(*, "order")= int [1:2] 1 1
#>   .. ..- attr(*, "intercept")= int 1
#>   .. ..- attr(*, "response")= int 1
#>   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   .. ..- attr(*, "predvars")= language list(mpg, hp, drat)
#>   .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
#>   .. .. ..- attr(*, "names")= chr [1:3] "mpg" "hp" "drat"
#>  $ residuals    : Named num [1:32] -2.42 -2.42 -1.26 1.84 2.17 ...
#>   ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#>  $ coefficients : num [1:3, 1:4] 10.78986 -0.05179 4.69816 5.07775 0.00929 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:3] "(Intercept)" "hp" "drat"
#>   .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
#>  $ aliased      : Named logi [1:3] FALSE FALSE FALSE
#>   ..- attr(*, "names")= chr [1:3] "(Intercept)" "hp" "drat"
#>  $ sigma        : num 3.17
#>  $ df           : int [1:3] 3 29 3
#>  $ r.squared    : num 0.741
#>  $ adj.r.squared: num 0.723
#>  $ fstatistic   : Named num [1:3] 41.5 2 29
#>   ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
#>  $ cov.unscaled : num [1:3, 1:3] 2.57 -3.04e-03 -5.81e-01 -3.04e-03 8.59e-06 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:3] "(Intercept)" "hp" "drat"
#>   .. ..$ : chr [1:3] "(Intercept)" "hp" "drat"
#>  - attr(*, "class")= chr "summary.lm"
coef(fit)
#> (Intercept)          hp        drat 
#> 10.78986122 -0.05178665  4.69815776
coef(fit)[1]
#> (Intercept) 
#>    10.78986

get_intercept <- function(x) coef(x)[[1]]
get_intercept(fit)
#> [1] 10.78986

make_fit <- function(x) lm(mpg ~ mtcars[,x], data = mtcars)

get_intercept(make_fit(4))
#> [1] 30.09886

do_both <- function(x) get_intercept(make_fit(x))

the_intercepts <- Map(do_both,2:11)
mean(unlist(the_intercepts))
#> [1] 18.74895