mql4beginner
mql4beginner

Reputation: 2233

How to make an output txt file look better

I'm using the following code to generate a file that contains all the results of glm models.
I would like to save the results of each model and the formula that was used per each model.
The output that I get is a bit of a mess because each model is printed as two line instead of 4 rows in the generated txt file.

How can I solve this issue?
Toy data:

df <- read.table(text = "target birds    wolfs     
                        0       21         7  
                        0        8         4  
                        1        2         5 
                        1        2         4 
                        0        8         3 
                        1        1         12  
                        1       7          10 
                        1        1         9 ",header = TRUE)

code:

myform <-NULL
 myform <- target~1
 dd<-NULL
 for ( i in c('birds', 'wolfs' )) { 
     myform <- update(myform,  as.formula(paste('~ birds +', i)))
     glm<-glm(myform,data=df)
     df$glm_predict_response <- ifelse(predict(glm,newdata=df ,   type="response")>.5, 1, 0)
    ff<-print(myform)
    dd<-print(xtabs(~ target + glm_predict_response, data = df ))
     print(prop.table(xtabs(~target + glm_predict_response, data = df ), 2) )
     e<-capture.output(ff,append = TRUE)
    e1<-capture.output(dd,append = TRUE)
    capture.output(e,e1, file = "myform2.txt",append = TRUE)
 }

Output from the txt file:

[1] "target ~ birds"
[1] "      glm_predict_response" "target 0 1"                 "     0 1 2"                 "     1 0 5"                
[1] "target ~ birds + wolfs"
[1] "      glm_predict_response" "target 0 1"                 "     0 3 0"                 "     1 0 5"                
[1] "target ~ birds + Country"
[1] "      glm_predict_response" "target 0 1"                 "     0 3 0"                 "     1 0 5"

Upvotes: 2

Views: 204

Answers (1)

LyzandeR
LyzandeR

Reputation: 37879

Personally, I wouldn't use capture.output unless absolutely necessary. There are definitely better choices like write.table if you are using a data.frame or even sink when you are just sending printed output to a file. That said, a fast fix for your code would be:

myform <-NULL
myform <- target~1
dd<-NULL
for ( i in c('birds', 'wolfs')) { 

  myform <- update(myform,  as.formula(paste('~ birds +', i)))
  glm<-glm(myform,data=dat)
  dat$glm_predict_response <- ifelse(predict(glm,newdata=dat,   type="response")>.5, 1, 0)
  ff<-print(myform)
  dd<-print(xtabs(~ target + glm_predict_response, data = dat))
  print(prop.table(xtabs(~target + glm_predict_response, data = dat), 2) )
  e<-capture.output(ff,append = TRUE)
  #you really don't need the second capture.output since you can
  #just print dd to the file directly
  capture.output(e, dd, file = "myform2.txt",append = TRUE)
}

Note I am not using country above as it is not included in your example data.frame. This outputs in the text file:

[1] "target ~ birds"
      glm_predict_response
target 0 1
     0 1 2
     1 0 5
[1] "target ~ birds + wolfs"
      glm_predict_response
target 0 1
     0 3 0
     1 0 5

The same using sink would be like (seems easier to me):

myform <-NULL
myform <- target~1
dd<-NULL
for ( i in c('birds', 'wolfs')) { 

  myform <- update(myform,  as.formula(paste('~ birds +', i)))
  glm<-glm(myform,data=dat)
  dat$glm_predict_response <- ifelse(predict(glm,newdata=dat,   type="response")>.5, 1, 0)
  dd<-xtabs(~ target + glm_predict_response, data = dat)

  #start sinking
  sink(file='myform2.txt', append=TRUE)
  print(myform)
  cat('\n\n')
  print(dd)
  cat('\n\n')
  #stop sinking
  sink()
}

Upvotes: 3

Related Questions