1

I have a list of multiple regressions completed via this code using the standard dataset mtcars.

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 = "~")

Where I now have a list of multiple regressions against the first column "mpg". From here I am trying to export certain summary statistics such as; intercept, coefficient and r.squared.

I have tried using a loop which I've included below.

for (i in 1:length(res.models))
{
  res <- res.models[[i]]
  res_bound <- NULL
  intercept <- res$coefficients[1]
  coef <- res$coefficients[2]
  r <- res$r.squared
  res_bound <- cbind(intercept, coef, r)
}

Although this gets me a dataframe it only includes the results from the last regression model, a 1 row by 3 column dataframe. Furthermore, I would like to have the "terms" of each regression in the table to distinguish between which model I am looking at (e.g. mpg vs cyl or mpg vs hp).

Am I simply missing a step in my loop? The ultimate goal is to write.csv the final dataframe.

5
  • 1
    I would use lapply to do what you're trying to do with for, then use do.call(rbind, list) or dplyr::bind_rows(list) to combine the list elements in a single table. Basically, write a function that works for one model, use lapply to iterate it over the models, then combine the results.
    – ulfelder
    May 10, 2017 at 19:51
  • Just as a suggestion, did you ever consider to use broom::tidy to turn the lm-output into nice dataframes? May 10, 2017 at 19:54
  • @coffeinjunky I have used that function yes, but it does not output r.squared. That certainly would be nicer to use if r.squared could be included
    – Harmzy15
    May 10, 2017 at 19:56
  • broom::glance does that, which you could merge back to the dataframe from broom::tidy. May 10, 2017 at 19:58
  • Also, to adress your actual question, in every loop iteration, you have ` res_bound <- NULL` and then later ` res_bound <- cbind(intercept, coef, r), leaving you with res_bound` only for the last iteration. May 10, 2017 at 20:00

2 Answers 2

5

You can use purrr::map_df to apply broom::glance to each model and then collect the results into a data.frame:

purrr::map_df(res.models, broom::glance, .id = 'formula')
#>     formula r.squared adj.r.squared    sigma statistic      p.value df
#> 1   mpg~cyl 0.7261800     0.7170527 3.205902 79.561028 6.112687e-10  2
#> 2  mpg~disp 0.7183433     0.7089548 3.251454 76.512660 9.380327e-10  2
#> 3    mpg~hp 0.6024373     0.5891853 3.862962 45.459803 1.787835e-07  2
#> 4  mpg~drat 0.4639952     0.4461283 4.485409 25.969645 1.776240e-05  2
#> 5    mpg~wt 0.7528328     0.7445939 3.045882 91.375325 1.293959e-10  2
#> 6  mpg~qsec 0.1752963     0.1478062 5.563738  6.376702 1.708199e-02  2
#> 7    mpg~vs 0.4409477     0.4223126 4.580827 23.662241 3.415937e-05  2
#> 8    mpg~am 0.3597989     0.3384589 4.902029 16.860279 2.850207e-04  2
#> 9  mpg~gear 0.2306734     0.2050292 5.373695  8.995144 5.400948e-03  2
#> 10 mpg~carb 0.3035184     0.2803024 5.112961 13.073646 1.084446e-03  2

You could do something similar with broom::tidy for the coefficients, or broom::augment for the residuals. Note that broom functions are intended to be called on the models themselves, not the summaries, but you can keep the whole thing in a pipeline, if you like:

library(purrr)

names(mtcars)[-1] %>% 
    paste('mpg ~', .) %>%    # or start with `models` at this point
    map(lm, data = mtcars) %>% 
    map_df(broom::glance, .id = 'formula')
#>    formula r.squared adj.r.squared    sigma statistic      p.value df
#> 1        1 0.7261800     0.7170527 3.205902 79.561028 6.112687e-10  2
#> 2        2 0.7183433     0.7089548 3.251454 76.512660 9.380327e-10  2
#> 3        3 0.6024373     0.5891853 3.862962 45.459803 1.787835e-07  2
#> 4        4 0.4639952     0.4461283 4.485409 25.969645 1.776240e-05  2
#> 5        5 0.7528328     0.7445939 3.045882 91.375325 1.293959e-10  2
#> 6        6 0.1752963     0.1478062 5.563738  6.376702 1.708199e-02  2
#> 7        7 0.4409477     0.4223126 4.580827 23.662241 3.415937e-05  2
#> 8        8 0.3597989     0.3384589 4.902029 16.860279 2.850207e-04  2
#> 9        9 0.2306734     0.2050292 5.373695  8.995144 5.400948e-03  2
#> 10      10 0.3035184     0.2803024 5.112961 13.073646 1.084446e-03  2
#>       logLik      AIC      BIC deviance df.residual
#> 1  -81.65321 169.3064 173.7036 308.3342          30
#> 2  -82.10469 170.2094 174.6066 317.1587          30
#> 3  -87.61931 181.2386 185.6358 447.6743          30
#> 4  -92.39996 190.7999 195.1971 603.5667          30
#> 5  -80.01471 166.0294 170.4266 278.3219          30
#> 6  -99.29406 204.5881 208.9853 928.6553          30
#> 7  -93.07356 192.1471 196.5443 629.5193          30
#> 8  -95.24219 196.4844 200.8816 720.8966          30
#> 9  -98.18192 202.3638 206.7611 866.2980          30
#> 10 -96.59033 199.1807 203.5779 784.2711          30

Note you get a few extra variables that can't aren't contained in the summary.

3

If you want to do it in base R:

res <- lapply(seq_along(res.models), function(i) {

  data.frame(model = names(res.models)[i],
             intercept = res.models[[i]]$coefficients[1],
             coef = res.models[[i]]$coefficients[2],
             r = res.models[[i]]$r.squared,
             stringsAsFactors = FALSE)

})

do.call(rbind, res)

Output:

      model intercept        coef         r
1   mpg~cyl 37.884576 -2.87579014 0.7261800
2  mpg~disp 29.599855 -0.04121512 0.7183433
3    mpg~hp 30.098861 -0.06822828 0.6024373
4  mpg~drat -7.524618  7.67823260 0.4639952
5    mpg~wt 37.285126 -5.34447157 0.7528328
6  mpg~qsec -5.114038  1.41212484 0.1752963
7    mpg~vs 16.616667  7.94047619 0.4409477
8    mpg~am 17.147368  7.24493927 0.3597989
9  mpg~gear  5.623333  3.92333333 0.2306734
10 mpg~carb 25.872334 -2.05571870 0.3035184

The reason for seq_along(res.models) instead of just res.models is so we can also grab the name for the associated slot in the list and drop it into the data frame you're making.

3
  • 1
    Nice! If you use Map, you can pass the names as a second variable and avoid the indexing.
    – alistaire
    May 10, 2017 at 20:14
  • This is nice and clean, exactly what I am looking for @ulfelder. This creates a list of dataframes, is there a quick way in this function to write the list of dataframes into one .csv?
    – Harmzy15
    May 10, 2017 at 20:22
  • No, if you want the whole table in one .csv, you'll have to do that after the do.call part where you make a single table.
    – ulfelder
    May 10, 2017 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.