Home > OS >  Looping `lm` objects using `purrr::map` into `broom::tidy`
Looping `lm` objects using `purrr::map` into `broom::tidy`

Time:04-11

I have a several lm objects that I would like to loop into broom::tidy using purrr::map. Is this possible to do?

library(broom)
library(purrr)

model1 <- lm(cyl ~ hp, data = mtcars)
model2 <- lm(mpg ~ cyl, data = mtcars)
map(c(model1, model2), tidy)
#> Warning: 'tidy.numeric' is deprecated.
#> See help("Deprecated")
#> Warning: `data_frame()` was deprecated in tibble 1.1.0.
#> Please use `tibble()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
#> Warning: 'tidy.numeric' is deprecated.
#> See help("Deprecated")

#> Warning: 'tidy.numeric' is deprecated.
#> See help("Deprecated")

#> Warning: 'tidy.numeric' is deprecated.
#> See help("Deprecated")

#> Warning: 'tidy.numeric' is deprecated.
#> See help("Deprecated")

#> Warning: 'tidy.numeric' is deprecated.
#> See help("Deprecated")
#> Error: No tidy method for objects of class qr

Created on 2022-04-10 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS  10.16                
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2022-04-10                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version     date       lib source                             
#>  assertthat    0.2.1       2019-03-21 [1] CRAN (R 4.0.2)                     
#>  backports     1.1.10      2020-09-15 [1] CRAN (R 4.0.2)                     
#>  blob          1.2.1       2020-01-20 [1] CRAN (R 4.0.2)                     
#>  broom       * 0.7.0       2020-07-09 [1] CRAN (R 4.0.2)                     
#>  cli           3.0.1       2021-07-17 [1] CRAN (R 4.0.2)                     
#>  crayon        1.4.1       2021-02-08 [1] CRAN (R 4.0.2)                     
#>  DBI           1.1.0       2019-12-15 [1] CRAN (R 4.0.0)                     
#>  digest        0.6.28      2021-09-23 [1] CRAN (R 4.0.2)                     
#>  dplyr         1.0.7       2021-06-18 [1] CRAN (R 4.0.2)                     
#>  ellipsis      0.3.2       2021-04-29 [1] CRAN (R 4.0.2)                     
#>  evaluate      0.14        2019-05-28 [1] CRAN (R 4.0.0)                     
#>  fansi         0.5.0       2021-05-25 [1] CRAN (R 4.0.2)                     
#>  fastmap       1.1.0       2021-01-25 [1] CRAN (R 4.0.2)                     
#>  fs            1.5.0       2020-07-31 [1] CRAN (R 4.0.2)                     
#>  generics      0.1.2       2022-01-31 [1] CRAN (R 4.0.5)                     
#>  glue          1.4.2       2020-08-27 [1] CRAN (R 4.0.2)                     
#>  highr         0.9         2021-04-16 [1] CRAN (R 4.0.2)                     
#>  htmltools     0.5.2       2021-08-25 [1] CRAN (R 4.0.2)                     
#>  knitr         1.36        2021-09-29 [1] CRAN (R 4.0.2)                     
#>  lifecycle     1.0.1       2021-09-24 [1] CRAN (R 4.0.2)                     
#>  magrittr      2.0.1       2020-11-17 [1] CRAN (R 4.0.2)                     
#>  pillar        1.6.3       2021-09-26 [1] CRAN (R 4.0.2)                     
#>  pkgconfig     2.0.3       2019-09-22 [1] CRAN (R 4.0.0)                     
#>  purrr       * 0.3.4       2020-04-17 [1] CRAN (R 4.0.2)                     
#>  R.cache       0.15.0      2021-04-30 [1] CRAN (R 4.0.2)                     
#>  R.methodsS3   1.8.1       2020-08-26 [1] CRAN (R 4.0.2)                     
#>  R.oo          1.24.0      2020-08-26 [1] CRAN (R 4.0.2)                     
#>  R.utils       2.10.1      2020-08-26 [1] CRAN (R 4.0.2)                     
#>  R6            2.5.1       2021-08-19 [1] CRAN (R 4.0.2)                     
#>  reprex        2.0.1       2021-08-05 [1] CRAN (R 4.0.2)                     
#>  rlang         0.4.11      2021-04-30 [1] CRAN (R 4.0.2)                     
#>  rmarkdown     2.11        2021-09-14 [1] CRAN (R 4.0.2)                     
#>  rstudioapi    0.13.0-9000 2021-05-12 [1] Github (rstudio/rstudioapi@b0ab332)
#>  sessioninfo   1.1.1       2018-11-05 [1] CRAN (R 4.0.0)                     
#>  stringi       1.7.4       2021-08-25 [1] CRAN (R 4.0.2)                     
#>  stringr       1.4.0       2019-02-10 [1] CRAN (R 4.0.0)                     
#>  styler        1.6.2       2021-09-23 [1] CRAN (R 4.0.2)                     
#>  tibble        3.1.5       2021-09-30 [1] CRAN (R 4.0.2)                     
#>  tidyr         1.1.4       2021-09-27 [1] CRAN (R 4.0.2)                     
#>  tidyselect    1.1.1       2021-04-30 [1] CRAN (R 4.0.2)                     
#>  utf8          1.2.2       2021-07-24 [1] CRAN (R 4.0.2)                     
#>  vctrs         0.3.8       2021-04-29 [1] CRAN (R 4.0.2)                     
#>  withr         2.4.2       2021-04-18 [1] CRAN (R 4.0.2)                     
#>  xfun          0.26        2021-09-14 [1] CRAN (R 4.0.2)                     
#>  yaml          2.2.1       2020-02-01 [1] CRAN (R 4.0.2)                     
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

CodePudding user response:

You need to keep your models in a list (list()), not in a vector (c()):

library(broom)
library(purrr)

model1 <- lm(cyl ~ hp, data = mtcars)
model2 <- lm(mpg ~ cyl, data = mtcars)

list(
  model1, 
  model2
) %>% 
  map(tidy) 
#> [[1]]
#> # A tibble: 2 × 5
#>   term        estimate std.error statistic       p.value
#>   <chr>          <dbl>     <dbl>     <dbl>         <dbl>
#> 1 (Intercept)   3.01     0.425        7.07 0.0000000741 
#> 2 hp            0.0217   0.00264      8.23 0.00000000348
#> 
#> [[2]]
#> # A tibble: 2 × 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)    37.9      2.07      18.3  8.37e-18
#> 2 cyl            -2.88     0.322     -8.92 6.11e-10

Created on 2022-04-10 by the reprex package (v2.0.1)

  • Related