0

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

Community
  • 1
  • 1
mql4beginner
  • 2,193
  • 5
  • 34
  • 73
  • you should make a function that takes your var as argument and outputs what you want to keep. Then use lapply on c('birds', 'wolfs' , 'Country') with your function to get all results in a list. – Karl Forner Oct 14 '15 at 14:44
  • Hello @Karl Forner, I'm not that familiar with creating functions, Can you elaborate your comment a bit? – mql4beginner Oct 15 '15 at 08:03

0 Answers0