I use a code (link to the code is written below)that produces lists of all variables combinations and their glm prediction results.The code works great but I need the output in a file. How can I do it? I tried to create a list inside the loop so I would be able to export it to csv by using this code but It gives me the last variables combination while I need all the combinations:
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)))
}
link to the original code: How to create a loop that will add new variables to a pre define glm model