Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to collect / aggregate Stata output from multiple analyses to a file?

    Hi folks,

    I am interested in aggregating / collecting values from analyses in order to output them to a file. The motivation behind this is to keep the analysis process as much "hands off" as possible so as to avoid typing mistakes and be more efficient in producing results (i.e., not winnowing around a plain text file for a bunch of values and then retyping those into a document...).

    As an example, I would like to run three hierarchical regressions, and save the marginal predicted value of SEX on the outcome variable TOTALSCORE.

    I know I could start a log file and save all the output, but I would like to avoid having to retype by hand.



    Code:
    use http://www.stata-press.com/data/r13/depression.dta , replace
    
    
    
    foreach v of varlist * {
    rename `v' `=lower("`v'")'
    }
    ****
    
    
    
    anova totalscore i.sex
    ereturn list , all
    return list , all
    estat esize 
    return list, all
    margins i.sex, at( (mean) _c (asobserved) _f)
    return list , all
    matrix list r(b)
    
    
    anova totalscore i.sex i.race
    ereturn list , all
    estat esize
    margins i.sex,  at( (mean) _c (asobserved) _f)
    matrix list r(b)
    
    
    
    anova totalscore i.sex i.race c.age
    ereturn list , all
    estat esize
    margins i.sex,  at( (mean) _c (asobserved) _f)
    matrix list r(b)
    
    
    /*
    would ultimately like to produce something like 
    this and save to a file :  
    
    
    Model           0.sex                 1.sex               est_name
    model 1        57.237                57.840            anova totalscore i.sex
    model 2        57.243                57.825            anova totalscore i.sex  i.race
    model 3        57.228                57.864            anova totalscore i.sex  i.race  c.age
    
    
    
    */


    Note that this question is cross-listed on StackOverflow : here



    Running :


    Stata/SE 13.1 for Windows (64-bit x86-64)
    Revision 19 Dec 2014


  • #2
    You could try using the file write command to save results in a text file.

    Comment


    • #3
      Here is an example:

      Code:
      use http://www.stata-press.com/data/r13/depression.dta , replace
      
      
      
      foreach v of varlist * {
      rename `v' `=lower("`v'")'
      }
      ****
      
      // store the file in my temp directory
      // I will delete it soon, you obviously want to store it somewhere else
      cd c:/temp
      
      // give it a unique handle
      tempname output
      
      // start writing
      file open `output' using models.txt, write text replace
      
      // the first line
      file write `output' "model" _col(10) "female" _col(20) "male"
      file write `output' _col(30) "command" _n
      
      // model number
      local mod = 1
      
      anova totalscore i.sex
      
      // the cmdline will be overwritten by -margins- with the -post- option
      // so store first
      local cmdline `e(cmdline)'
      
      margins i.sex, at( (mean) _c (asobserved) _f) post
      
      // write first result
      file write `output' "model `mod++'" _col(10) %7.3f (_b[0.sex])
      file write `output' _col(20) %7.3f (_b[1.sex]) _col(30) "`cmdline'" _n
      
      anova totalscore i.sex i.race
      local cmdline `e(cmdline)'
      margins i.sex,  at( (mean) _c (asobserved) _f) post
      
      // write second result
      file write `output' "model `mod++'" _col(10) %7.3f (_b[0.sex])
      file write `output' _col(20) %7.3f (_b[1.sex]) _col(30) "`cmdline'" _n
      
      anova totalscore i.sex i.race c.age
      local cmdline `e(cmdline)'
      margins i.sex,  at( (mean) _c (asobserved) _f) post
      
      // write third result
      file write `output' "model `mod++'" _col(10) %7.3f (_b[0.sex])
      file write `output' _col(20) %7.3f (_b[1.sex]) _col(30) "`cmdline'" _n
      
      file close `output'
      
      // see what was stored
      type models.txt
      
      
      /*
      would ultimately like to produce something like
      this and save to a file :  
      
      
      Model           0.sex                 1.sex               est_name
      model 1        57.237                57.840            anova totalscore i.sex
      model 2        57.243                57.825            anova totalscore i.sex  i.race
      model 3        57.228                57.864            anova totalscore i.sex  i.race  c.age
      */
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Cross-posting carries an obligation to keep us informed of replies elsewhere. Roberto Ferrer replied at http://stackoverflow.com/questions/2...yses-to-a-file

        Comment


        • #5
          Thanks Maarten, much appreciated.

          Also, thanks to Nick as well, you beat me to it!

          Comment


          • #6
            I have come full circle to this issue once again. I ended up using the -statsby- command to address my problem, and it worked well

            Comment

            Working...
            X