Announcement

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

  • Can you define outreg2 options as a macro?

    I want to print estimation output with outreg2 as a tex file. I combine several results for which only some of the options change. I want to use macros for the non-changing options in order to make the script more sparse.

    Here is a simple example:

    Code:
    loc outputFile "myOutputFile"
    loc outputOpt "tex(frag) nocons label e(r2 ll) keep(`vars') adds(AIC, `AIC', BIC, `BIC') "
    
    glm y `vars', fa(bin) link(probit)
    
    outreg2 using `outputFile', `outputOpt' addt(model, Probit)
    Only the addtext() part changes by regression, and I want to avoid repeating other options for each output. Any idea how to make this work? Or do I just have write out all the options for each outreg2 line?

  • #2
    I don't see why it wouldn't. This is something that you should try out before you ask. This works for me:

    Code:
    sysuse auto, clear
    regress price mpg weight turn
    local opts "keep(mpg weight)"
    outreg2 using myfile, replace `opts'

    Comment


    • #3
      I tried it and it didn't work. That's why I asked.

      It still doesn't work. I get 'invalid syntax' error again although I didn't apply macro for the output file. Very unhelpful error message, actually, because there is no indication where this happens.

      Please note that your example is not equivalent with mine - some options need to be defined separately. Removing addt() didn't make through either.

      Comment


      • #4
        Just to make myself clear:

        1) writing the options part as such in the code works as it should

        2) doing the same with the equivalent local macro does not work - that ends with 'invalid syntax' error

        I have no idea why this happens. And I just don't like this W.E.T. approach to programming.

        Comment


        • #5
          That is why we emphasize that you need to present a reproducible example (refer to the FAQ 12.2). For the record, -replace- in my syntax is an option - so I do have options contained in a local macro as well as one specified explicitly.

          Comment


          • #6
            Hi Antti,

            Not sure why you are getting that error, but I use it all the time. Here is a sample of my code:

            Code:
            local rhsvars "acq_exp lnalliances_3yr lnpatents_3yr  acq_re lnassets acq_cash_assets acq_q target_age target_public"
            
            
            local outreg_file "cvc3_table3_main_regressions"
            local log_file "cvc3_table3_main_regressions.log"
            capture log using `log_file', replace smcl
            local outreg_line1 "excel bdec(4) rdec(3) se bracket addstat(F test: , e(F)) adec(3)"  // The outreg options
            local do_file "C:\Project 1\Tables for R&R\Table 3 - Main Tables - No Fixed Effects.do"
            
            
            
            *** First regression
            reg car4_250_100 `rhsvars' if good==1, cluster(acq_gvkey)
            outreg2 using `outreg_file', `outreg_line1' ctitle("Everything - so lines up") ///
                addnote("", do file is `do_file', "Run on $S_DATE", Log file is `log_file', Using data from $S_FN) replace
            
                * Note $S_FN could also be written as c(filename) and $S_DATE could be written as c(current_date)
                * c(filename) returns a string containing the filename last specified with a use or save, such as "C:\Data\auto.dta".  c(filename) is equal to $S_FN.
                * c(filedate) returns a string containing the date and time the file in c(filename) was last saved, such as "7 Jul 2014 13:51".  c(filedate) is equal to $S_FNDATE.
                * Lookup "creturn"
            
            
            reg car4_250_100 `rhsvars' if good==1, cluster(acq_gvkey)
            outreg2 using `outreg_file', `outreg_line1' ctitle("Controls only") append
            
            reg car4_250_100 cvc_investor `rhsvars' if good==1, cluster(acq_gvkey)
            outreg2 using `outreg_file', `outreg_line1' ctitle("Did they ever invest") append
            
            reg car4_250_100 active_cvc_1yr `rhsvars' if good==1, cluster(acq_gvkey)
            outreg2 using `outreg_file', `outreg_line1' ctitle("Did they invest in that year") append

            Comment


            • #7
              The code in post #1 is incomplete and does not show definitions for the local macros vars, AIC, and BIC. (That's one reason Statalist asks for a reproducible example.)

              Were they defined? And in particular, were AIC and BIC defined before or after defining outputOpt?

              I'm guessing you defined AIC and BIC after your glm, using e(AIC) and e(BIC). This is not going to work, because the local macros AIC and BIC are evaluated at the time outputOpt is defined.

              To cause AIC and BIC to be evaluated at the time outputOpt is evaluated, change
              Code:
              adds(AIC, `AIC', BIC, `BIC')
              to
              Code:
              adds(AIC, \`AIC', BIC, \`BIC')
              Here is an example, which also shows debugging using the macro list command.
              Code:
              . // fails
              . local options "adds(GNXL, `gnxl')"
              
              . macro list _options
              _options:       adds(GNXL, )
              
              . local gnxl foo
              
              . macro list _gnxl _options
              _gnxl:          foo
              _options:       adds(GNXL, )
              
              . display "The options are `options')
              The options are adds(GNXL, ))
              
              . // works
              . local options "adds(GNXL, \`gnxl')"
              
              . macro list _options
              _options:       adds(GNXL, `gnxl')
              
              . local gnxl bar
              
              . macro list _gnxl _options
              _gnxl:          bar
              _options:       adds(GNXL, `gnxl')
              
              . display "The options are `options')
              The options are adds(GNXL, bar))
              I'm not a user of outreg2 or glm, but the output of help outreg2 and help glm leads me to believe that
              Code:
              adds(AIC, e(aic), BIC, e(bic))
              might work for you, if your outreg2 follows the glm with no intervening e-class commands, eliminating the need for the local macros AIC and BIC.

              Comment


              • #8
                Thank you William! Your suggestion solved my problem.

                I'm sorry about a bit incomplete example. I try to do better next time.

                I indeed had defined ICs by:

                Code:
                getICs
                loc AIC = round(`r(aic)', .01)
                loc BIC = round(`r(bic)', .01)
                where 'getICs' is a program:

                Code:
                program getICs, rclass
                    qui estat ic        
                    mat l r(S)
                    mat S = r(S)
                    ret sca aic = S[1,5]    
                    ret sca bic = S[1,6]
                end
                and then added them to 'margins' output.

                I really couldn't guess that this definition could have an effect.

                Comment

                Working...
                X