我正在努力寻找最好的GLModel(目前仅基于opon AIC分数)。我可以访问我使用的数据:
https://drive.google.com/open?id=0B5IgiR_svnKcY25TQ29ZMGN3NVE
以下是无法返回最佳模型的代码:
rm(list = ls())
mod_data <- read.csv("mod_data.csv", header = T)
mod_headers <- names(mod_data[3:ncol(mod_data)-1])
f <- function(mod_headers){
null_model <- glm(newcol ~ 1, data=mod_data, family = binomial(link = "logit"), control = list(maxit = 50))
best_model <- null_model
best_aic <- AIC(null_model)
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, "newcol")
model <- glm(newcol ~., data=mod_data[c(mod_tab_new)], family = binomial(link = "logit"), control = list(maxit = 50000))
if(AIC(model) < best_aic){
best_model <- model
best_aic <- AIC(model)
}
}
return(best_model)
}
}
f(mod_headers)
由于某种原因,它总是卡在只有一个自变量的模型上(&#34; var5&#34; AIC得分为678.8),但我知道至少有一个更好的模型(&#34; var1&#34;, &#34; var5&#34;和&#34; var8&#34; AIC得分677.6)由这段代码返回:
rm(list = ls())
mod_data <- read.csv("mod_data.csv", header = T)
mod_headers <- names(mod_data[3:ncol(mod_data)-1])
library(tidyverse)
library(iterators)
whichcols <- Reduce("c", map(1:length(mod_headers), ~lapply(iter(combn(mod_headers,.x), by="col"),function(y) c(y))))
models <- map(1:length(whichcols), ~glm(newcol ~., data=mod_data[c(whichcols[[.x]], "newcol")], family = binomial(link = "logit")))
print(models[[which.min(sapply(1:length(models),function(x)AIC(models[[x]])))]])
我不知道为什么第一段代码无法返回所需的结果。
感谢您的提示!