1

I would like to create a procedure that will add per each loop a new variable (from a pool of variables) to a glm model that allready contains few of the variables that need to be part of the final model.I than would like to have the results of the loop in a list that will contain the glm formula and results.I know how to do it manually (code is written below) but I would be happy to know how to do it automaticaly. Here is a toy dataset and the relevant code to do the task manually:

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)
#birds is a mandatory variable so I'll need to add one of the other   variables in addition to birds
 glm<-glm(target~birds,data=dat)
 dat$glm_predict_response <- ifelse(predict(glm,newdata=dat,   type="response")>.5, 1, 0)
xtabs(~target + glm_predict_response, data = dat)
      glm_predict_response
target 0 1
     0 1 2
     1 0 5
    glm_predict_response
prop.table(xtabs(~target + glm_predict_response, data = dat), 2)
    target         0         1
         0 1.0000000 0.2857143
         1 0.0000000 0.7142857

#manually I would add the next variable (wolfs) to the model and look at the results:
 glm<-glm(target~birds+wolfs,data=dat)
 dat$glm_predict_response <- ifelse(predict(glm,newdata=dat, type="response")>.5, 1, 0)
 xtabs(~target + glm_predict_response, data = dat)
      glm_predict_response
target 0 1
     0 3 0
     1 0 5
 prop.table(xtabs(~target + glm_predict_response, data = dat), 2)
      glm_predict_response
target 0 1
     0 1 0
     1 0 1

In the next loop I would add the variable "country" and do the same procedure, In the real life I have hundreds of variables so turning it to an automatic proccess would be great.

mql4beginner
  • 2,193
  • 5
  • 34
  • 73
  • i think you can simply `glm<-glm(target~.,data=dat[,1:i]` where `i` its loop parametr ( but you need to order you col before) – Batanichek Oct 13 '15 at 09:21
  • @Hello Batanichek, I did as you suggested but with no success so I guess I'm mising somthing.Here is the code that I used:for (i in names(dat)) function(x) {glm<-glm(target~.,data=dat[,1:i]) dat$glm_predict_response <- ifelse(predict(glm,newdata=dat, type="response")>.5, 1, 0) xtabs(~target + glm_predict_response, data = dat) } – mql4beginner Oct 13 '15 at 09:33
  • Sorry, I wrote the comment before refreshing.I do have two questions to your answer I'll write them below it. – mql4beginner Oct 13 '15 at 09:36

2 Answers2

3

I would do it using update to update the formula each time in the loop:

#initiate formula
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)
    print(myform)
    print(xtabs(~ target + glm_predict_response, data = dat))
    print(prop.table(xtabs(~target + glm_predict_response, data = dat), 2))

}

Output:

target ~ birds
      glm_predict_response
target 0 1
     0 1 2
     1 0 5
      glm_predict_response
target         0         1
     0 1.0000000 0.2857143
     1 0.0000000 0.7142857

target ~ birds + wolfs
      glm_predict_response
target 0 1
     0 3 0
     1 0 5
      glm_predict_response
target 0 1
     0 1 0
     1 0 1

target ~ birds + wolfs + Country
      glm_predict_response
target 0 1
     0 3 0
     1 0 5
      glm_predict_response
target 0 1
     0 1 0
     1 0 1
LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • Hello @LyzandeR, I have a question regarding your code If I may.If I need to save 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 comnitaio while I need all the combinations,code:newlist <- list(print(myform),print(xtabs(~ target + glm_predict_response, data = dat)),print(prop.table(xtabs(~target + glm_predict_response, data = dat), 2))) – mql4beginner Oct 13 '15 at 15:20
  • Can you please post as a new question? It is very hard for me to read the code from the comment and reply. You can share the link here too if you like. – LyzandeR Oct 13 '15 at 15:24
  • I added it as an update to the original question.Thank you. – mql4beginner Oct 13 '15 at 15:40
  • @mql4beginner But this way you re mixing up the questions. This is not how the site is meant to be used. This is a completely different question that needs special attention and proper coding. We shouldn't be editing answers to updates. The reason of this site is for many people to benefit so one question should only be in a post... – LyzandeR Oct 13 '15 at 15:47
  • [This](http://stackoverflow.com/questions/2436688/append-an-object-to-a-list-in-r-in-amortized-constant-time) will help actually – LyzandeR Oct 13 '15 at 15:54
  • 1
    Got it @LyzandeR, I'll remove the update and will post it as a new question. – mql4beginner Oct 14 '15 at 07:30
1

You can try something like

    list_1=list(NA)
    list_2=list(NA)
    for (i in 2 :ncol(dat)){
      dat1=dat[,1:i]
      glm<-glm(target~.,data=dat1)
      dat1$glm_predict_response <- ifelse(predict(glm,newdata=dat1,   type="response")>.5, 1, 0)

      list_1[[i-1]]=xtabs(~target + glm_predict_response, data = dat1)
      names(list_1)[i-1]=do.call(paste,as.list(colnames(dat1)[c(-1,-ncol(dat1))]))

      list_2[[i-1]]=prop.table(xtabs(~target + glm_predict_response, data = dat1), 2)
      names(list_2)[i-1]=do.call(paste,as.list(colnames(dat1)[c(-1,-ncol(dat1))]))}

But you need to have col in right order.

Batanichek
  • 7,761
  • 31
  • 49