Announcement

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

  • frmttable in a loop

    Dear Statalisters,

    First, thanks to John Luke Gallup for writing frmttable! I wrote to him first before posting here.

    I need help getting something to work; I realize it is probably small but have not been able to figure it out after 2 days, so here we go.

    Please find attached an example do file using the auto data. statalist_qn_frmttable.do

    My problem is that if you run it twice in the same stata session, the outputted table contains the previous results even though I have added in the beginning of the loop an erase command for the doc file and a matrix drop command for the matrix that I am formatting using frmttable.

    I suspect that there is another matrix that gets created by frmttable and it stays in memory, but I don’t know how to get rid of it. Also possibly I could do something with the option clear, but I tried a few things and still couldn’t get it right.

    Thank you so much,
    Maria

  • #2
    Maria, please don't upload or attach .do-files but enclose Stata code (or output) between CODE delimiters as described in the Stata Forum FAQ (section #12.3).

    Comment


    • #3
      Sorry for missing this and thank you, Dirk, for pointing it out.

      Please, don't focus on why I am doing all the rest apart from frtmttable - it is only for the sake of the example.

      I use Stata 14.1 on Windows 7.

      Here we go:

      Code:
      capture log close
      log using statalist_qn_frmttable, replace
      set more off
      
      capture erase out.doc
      capture matrix drop out
      
      local vars mpg gear_ratio
      
      foreach v of local vars {
      
      forvalues y = 1/6 {
          
          forvalues f = 0/1 {
          display "5-year perdiod: " `y' " Foreign: " `f'
          
          sysuse auto, clear
          
          expand 30
          gen year = .
          bysort make: replace year = 1985 if _n == 1
          bysort make: replace year = year[_n-1] + 1 if year == .
      
          gen price_new = floor((.5*price+1)*runiform() + price)
          gen `v'_new = floor((.1*mpg+1)*runiform() + mpg)
          local lbe : var label `v'
          display "`lbe'"
          la var `v'_new "`lbe'"
      
      
          recode year (1985/1989 = 1) (1990/1994 = 2) (1995/1999 = 3) (2000/2004 = 4) ///
                      (2005/2009 = 5) (2010/2014 = 6) , g(year_fives)
          la var year_fives "5-year period 1985-2014"
      
          keep if year_fives == `y' & foreign == `f'
          
          qui reg price_new `v'_new
          
          matrix out_`y'_`f' = e(b)'
          matlist out_`y'_`f'
      
      }
      }
      *
      
      matrix define out = (out_1_0, out_1_1, out_2_0, out_2_1, out_3_0, out_3_1, ///
                           out_4_0, out_4_1, out_5_0, out_5_1, out_6_0, out_6_1)
                          
      
      matlist out
      
      frmttable using "out", statmat(out) ///
          sdec(3) coljust(c) append ///
          title("Regressions by 5-year period")  ///
          rtitles("`lbe'" \ "Constant") ///
          ctitles("", "1985/1989", "", "1990/1994" , "", "1995/1999" , "", "2000/2004" , "" , "2005/2009" , "" , "2010/2014" , ""  \ ///
                  "", "Domestic", "Foreign", "Domestic", "Foreign", "Domestic", "Foreign", "Domestic", "Foreign", ///
                      "Domestic", "Foreign", "Domestic", "Foreign") ///
          multicol(1,2,2;1,4,2;1,6,2;1,8,2;1,10,2;1,12,2) ///
          vlines(010) vlstyle(a) basefont(arial fs10)
      }
      *
      
      log close
      clear
      exit
      Thank you again.
      Last edited by Maria Boutchkova; 15 May 2016, 01:14.

      Comment


      • #4
        Welcome to Statalist, Maria, and thank you for your contributions to other topics!

        I have not used frmttable, but have just looked at the help file. Two things stand out in my reading. As you surmised, frmttable stores results in memory so that additional results can be merged or appended. From help frmttable we see

        frmttable converts the Stata matrix of statistics in statmat(), along with various text additions like titles, into a Mata struct of string matrices that persists in memory. The persistent table data can be reused to merge or append new results or written to a Word or TeX file with new formatting directives. The persistent frmttable data can be assigned names, so multiple tables can be manipulated simultaneously.
        So the question seems to be, how to recycle this "persistent table". It seems to me that the clear option will result in eliminating the contents of the existing table before storing the new results. Alternatively, several of the options, including merge, append, and clear, will accept a "sub-option" which, if I understand it correctly, specifies a name for the formatted table, so that you can simultaneously build several formatted tables. This would be another way of having new results not be include with previous results.

        Have you tried either of these approaches?

        Comment


        • #5
          Thank you, William for the pointers and noticing that I tried to help earlier. Will look again and report.

          Comment


          • #6
            Solution found.
            The frmttable help file points to an example with outreg (help outreg_complete##xmpl16) and there it shows how we need to use "outreg, clear" before the loop. Similarly, with frmttable, we should put "frtmtable, clear" with the "capture erase ..." and "capture matrix drop ..." lines (referring to my example do-file above). Same logic as with eststo clear.


            Comment


            • #7
              Thank you for telling us the outcome of your testing.

              This discussion has been helpful to me: I previously thought frmttable was more complicated that I needed for my work, but after looking at it more carefully in the process of trying to understand your problem, I now think it will be worth learning more about. That's what I like about Statalist: the process of working through a problem or of seeing an expert's solution helps me enlarge my knowledge of Stata. I can read the manuals all I want, but there's nothing like doing a little "homework" to reinforce the learning process.

              Comment


              • #8
                William, your thoughts helped me to look more closely at the exact right place in the help file. Happy to hear you will be adopting frmttable, it is awesome! As a first-time statlist poster, I must report that it was a very gratifying experience.

                Comment

                Working...
                X