2
votes

Hello How can I create a histogram for the difference of the AICs of each models to the AIC of the full model.?

#AIC of the full model
Y <- modelTT$aic
#AICs for each of the n models.
X <- lapply(listOfModels,function(xx) xx$aic)

so basically I want to do the X - Y first. Then I need to create the histogram of each of the difference values from largest to smallest.

2

2 Answers

2
votes

Another alternative using broom()

df =  data.frame(a =  sample(1:10, replace = TRUE, 24), 
                 b = sample(25:40, replace = TRUE, 24),
                 c = sample(0:1, replace = TRUE, 24))

model1 = lm(a ~ b + c, df)
model2 = lm(b ~ c, df ) 
model3 = lm(a ~  c, df)

library(broom)
library(ggplot2)
library(dplyr)

mod1 = glance(model1) %>% mutate(model = "m1")
mod2 = glance(model2) %>% mutate(model = "m2")
mod3 = glance(model3) %>% mutate(model = "m3")

models = bind_rows(mod1, mod2, mod3)


models %>% ggplot(aes(model,AIC)) + geom_bar(stat = "identity")

Gives the following

enter image description here

0
votes

A generic data.frame

db<-data.frame(y=c(1,2,3,4,5,6,7,8,9),x1=c(9,8,7,6,5,4,3,2,1),x2=c(9,9,7,7,5,5,3,3,1))

A list of lm models

LM_modesl<-NULL
LM_modesl[[1]]<-lm(y ~ x1+x2 , data = db)
LM_modesl[[2]] <- lm(y ~ x1 , data = db)
LM_modesl[[3]] <- lm(y ~ x2 , data = db)

AIC calculation

AIC<-lapply(LM_modesl,AIC)

Decreasing plot

plot(sort(unlist(AIC),decreasing = T),type="h")