每个型号的AIC直方图

时间:2018-02-09 08:03:23

标签: r plot model-comparison

您好如何为每个模型的AIC与完整模型的AIC之间的差异创建直方图。?

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

基本上我想先做X - Y.然后我需要创建从最大到最小的每个差值的直方图。

2 个答案:

答案 0 :(得分:2)

使用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")

提供以下内容

enter image description here

答案 1 :(得分:0)

通用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))

lm模型列表

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计算

AIC<-lapply(LM_modesl,AIC)

减少情节

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