Linear regression in order to get AIC and BIC?

Hello,
I'm trying to obtain the AIC and BIC from a linear regression using purr (I'm learning purrr...)
This is what I can do:

Getting the AIC

mtcars %>%
group_split(cyl)%>%
map(~ lm(wt~qsec, data = .)) %>% 
map(AIC)

Getting the BIC

mtcars %>%
group_split(cyl)%>%
map(~ lm(wt~qsec, data = .)) %>% 
map(BIC)

But I don't know how to insert the two criteria in one line. Something like:
Getting the AIC and BIC together

mtcars %>%
group_split(cyl)%>%
map(~ lm(wt~qsec, data = .)) %>% 
map(AIC,BIC)

Another issue, It's I don't know how to export the coefficients from each model :

mtcars %>%
group_split(cyl)%>%
map(~ lm(wt~qsec, data = .)) %>% 
map("coefficients")

How can store that code in order to export It to an excel file?
(I know how to export to excel, but I can't transform the list in something more easy to manipulate?)

Thanks for your time and interest.
Also thank you for your patience.

You could use the broom package:

library(tidyverse)
library(broom)

# aic and bic
mtcars %>%
  group_split(cyl)%>%
  map(~glance(lm(wt~qsec, data = .)))  %>% 
  set_names() %>% 
  map_df(bind_rows, .id = "model")

# A tibble: 3 × 13
  model                                                             r.squared adj.r.squared sigma statistic p.value    df logLik    AIC    BIC deviance df.residual  nobs
  <chr>                                                                 <dbl>         <dbl> <dbl>     <dbl>   <dbl> <dbl>  <dbl>  <dbl>  <dbl>    <dbl>       <int> <int>
1 list(r.squared = 0.407071270040772, adj.r.squared = 0.3411903000…     0.407         0.341 0.462      6.18  0.0347     1  -6.02 18.0   19.2      1.92            9    11
2 list(r.squared = 0.749889147028081, adj.r.squared = 0.6998669764…     0.750         0.700 0.195     15.0   0.0117     1   2.68  0.639  0.477    0.191           5     7
3 list(r.squared = 0.287884462711336, adj.r.squared = 0.2285415012…     0.288         0.229 0.667      4.85  0.0479     1 -13.1  32.2   34.2      5.34           12    14

# coefficients
mtcars %>%
  group_split(cyl)%>%
  map(~tidy(lm(wt~qsec, data = .))) %>% 
  set_names() %>% 
  map_df(bind_rows, .id = "model")

# A tibble: 6 × 6
  model                                                                                                                    term      estimate std.error statistic p.value
  <chr>                                                                                                                    <chr>        <dbl>     <dbl>     <dbl>   <dbl>
1 "list(term = c(\"(Intercept)\", \"qsec\"), estimate = c(-1.84776049207306, 0.215991475050134), std.error = c(1.66871372… (Interce…   -1.85     1.67      -1.11   0.297 
2 "list(term = c(\"(Intercept)\", \"qsec\"), estimate = c(-1.84776049207306, 0.215991475050134), std.error = c(1.66871372… qsec         0.216    0.0869     2.49   0.0347
3 "list(term = c(\"(Intercept)\", \"qsec\"), estimate = c(-0.132915666904211, 0.18078837943682), std.error = c(0.84264638… (Interce…   -0.133    0.843     -0.158  0.881 
4 "list(term = c(\"(Intercept)\", \"qsec\"), estimate = c(-0.132915666904211, 0.18078837943682), std.error = c(0.84264638… qsec         0.181    0.0467     3.87   0.0117
5 "list(term = c(\"(Intercept)\", \"qsec\"), estimate = c(-1.71471416603641, 0.340679691344107), std.error = c(2.60035841… (Interce…   -1.71     2.60      -0.659  0.522 
6 "list(term = c(\"(Intercept)\", \"qsec\"), estimate = c(-1.71471416603641, 0.340679691344107), std.error = c(2.60035841… qsec         0.341    0.155      2.20   0.0479
1 Like

Thanks, williaml
It works great your code.
I have to say that I'm still interested in providing a list of functions in purrr.

1 Like

There is a list of functions here: Function reference • purrr and this is a good guide: Lessons and Examples

I'll read them. I hope I can make some progress.
I read that broom was built in order to print/retrieve information in a tidy way.
Nice package broom. It seems simple and direct.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.