我使用代码(下面写的代码链接)生成所有变量组合的列表及其glm预测结果。代码工作得很好,但我需要输出文件。我该怎么做? 我试图在循环中创建一个列表,所以我可以使用这个代码将它导出到csv但它给了我最后的变量组合,而我需要所有的组合:
dat <- read.table(text = "target birds wolfs Country
0 21 7 a
0 8 4 b
1 2 5 c
1 2 4 a
0 8 3 a
1 1 12 a
1 7 10 b
1 1 9 c",header = TRUE)
myform <- target~1
for ( i in c('birds', 'wolfs' , 'Country')) {
#update formula each time in the loop with the above variables
#this line below is practically the only thing I changed
myform <- update(myform, as.formula(paste('~ . +', i)))
glm<-glm(myform,data=dat)
dat$glm_predict_response <- ifelse(predict(glm,newdata=dat,
type="response")>.5, 1, 0)
newlist <- list(print(myform),print(xtabs(~ target + glm_predict_response, data = dat)),print(prop.table(xtabs(~target + glm_predict_response, data = dat), 2)))
}
链接到原始代码:How to create a loop that will add new variables to a pre define glm model