-1

It is possible to compare the multiple regression models using AIC scores, with the models ordered from best-supported to worst-supported?

Here is my code

 library(data.table)

 Regressions<- 
 data.table(February)[, 
                      .(Lm = lapply(.SD, function(x) summary(lm(February$PPNA ~ February$Acum1 + x)))),
                      .SDcols = 80:157]
Pablo
  • 41
  • 4
  • yes it works, because i can fix the variables that i need, but i can't compare the regressions models using AIC scores. I want to ranking de models and i can do it, this is my problem – Pablo Apr 07 '20 at 00:37

1 Answers1

1

We can extract the AIC values and order based on the 'AIC' values

library(data.table)
dt <- as.data.table(February)
dt1 <- dt[, .(Lm = lapply(.SD, function(x) lm(February$PPNA ~ February$Acum1 + x))),
          .SDcols = 80:157]
dt2 <-  dt1[, .(Lm = Lm[order(unlist(lapply(Lm, AIC)))])]

Or using a reproducible example

dt1 <- as.data.table(iris)[, .(Lm = lapply(.SD, function(x) 
          lm(iris$Petal.Length ~ iris$Species + x)))]
dt2 <-  dt1[, .(Lm = Lm[order(unlist(lapply(Lm, AIC)))])]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I try both and when you click on dt2 in the gloval environment it gives you the following extensive `list (coefficients = c (` (Intercept) `= ....` not an AIC value for each regression – Pablo Apr 07 '20 at 01:33
  • @Pablo For me it is giving the `AIC` – akrun Apr 07 '20 at 01:34