查找R中AIC最低的模型(从for循环返回)

查找R中AIC最低的模型(从for循环返回),r,for-loop,glm,model-comparison,R,For Loop,Glm,Model Comparison,我正在努力寻找AIC最低的型号。模型从两个for循环返回,这两个for循环使列的组合成为可能。我无法制作AIC最低的函数返回模型。下面的代码演示了我被卡住的地方: rm(list = ls()) data <- iris data <- data[data$Species %in% c("setosa", "virginica"),] data$Species = ifelse(data$Species == 'virginica', 0, 1) mod_headers &l

我正在努力寻找AIC最低的型号。模型从两个for循环返回,这两个for循环使列的组合成为可能。我无法制作AIC最低的函数返回模型。下面的代码演示了我被卡住的地方:

rm(list = ls())

data <- iris

data <- data[data$Species %in% c("setosa", "virginica"),]

data$Species = ifelse(data$Species == 'virginica', 0, 1)

mod_headers <- names(data[1:ncol(data)-1])

f <- function(mod_headers){
    for(i in 1:length(mod_headers)){
    tab <- combn(mod_headers,i)
    for(j in 1:ncol(tab)){
      tab_new <- c(tab[,j])
      mod_tab_new <- c(tab_new, "Species")
      model <- glm(Species ~., data=data[c(mod_tab_new)], family = binomial(link = "logit"))
    }
    }
  best_model <- model[which(AIC(model)[order(AIC(model))][1])]
  print(best_model)
}

f(mod_headers)
rm(list=ls())

数据我用矢量化的备选方案替换了for循环

library(tidyverse)
library(iterators)
# Column names you want to use in glm model, saved as list
whichcols <- Reduce("c", map(1:length(mod_headers), ~lapply(iter(combn(mod_headers,.x), by="col"),function(y) c(y))))

# glm model results using selected column names, saved as list
models <- map(1:length(whichcols), ~glm(Species ~., data=data[c(whichcols[[.x]], "Species")], family = binomial(link = "logit")))

# selects model with lowest AIC
best <- models[[which.min(sapply(1:length(models),function(x)AIC(models[[x]])))]]

使用循环,只需将所有模型放在一个列表中。 然后计算所有这些模型的AIC。 最后返回AIC最小的模型

f <- function(mod_headers) {

  models <- list()
  k <- 1
  for (i in 1:length(mod_headers)) {
    tab <- combn(mod_headers, i)
    for(j in 1:ncol(tab)) {
      mod_tab_new <- c(tab[, j], "Species")
      models[[k]] <- glm(Species ~ ., data = data[mod_tab_new], 
                         family = binomial(link = "logit"))
      k <- k + 1
    }
  }

  models[[which.min(sapply(models, AIC))]]
}
fglm()使用迭代的重新加权最小二乘算法。算法在收敛之前达到最大迭代次数-更改此参数有助于您的情况:

 glm(Species ~., data=data[mod_tab_new], family = binomial(link = "logit"), control = list(maxit = 50))
使用
还有另一个问题,我用
if
替换了它,在每个模型拟合后,与迄今为止最低的AIC进行比较。然而,我认为有比这种
for loop
方法更好的解决方案

f <- function(mod_headers){
  lowest_aic <- Inf     # added
  best_model <- NULL    # added

  for(i in 1:length(mod_headers)){
    tab <- combn(mod_headers,i)
    for(j in 1:ncol(tab)){
      tab_new <- tab[, j]
      mod_tab_new <- c(tab_new, "Species")
      model <- glm(Species ~., data=data[mod_tab_new], family = binomial(link = "logit"), control = list(maxit = 50))
      if(AIC(model) < lowest_aic){ # added
        lowest_aic <- AIC(model)   # added
        best_model <- model        # added
      }
    }
  }
  return(best_model)
}

f您应该读到,这实际上是有效的,而且似乎是用一堆COL找到我的最佳模型的最有效方法。谢谢
f <- function(mod_headers){
  lowest_aic <- Inf     # added
  best_model <- NULL    # added

  for(i in 1:length(mod_headers)){
    tab <- combn(mod_headers,i)
    for(j in 1:ncol(tab)){
      tab_new <- tab[, j]
      mod_tab_new <- c(tab_new, "Species")
      model <- glm(Species ~., data=data[mod_tab_new], family = binomial(link = "logit"), control = list(maxit = 50))
      if(AIC(model) < lowest_aic){ # added
        lowest_aic <- AIC(model)   # added
        best_model <- model        # added
      }
    }
  }
  return(best_model)
}