Announcement

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

  • Exporting regression results (different regressions) to latex


    Dear Stata Users,
    I want export regression results (different regressions) to a table in latex. For instance, I am running following regressions:
    Code:
    reghdfe a b , ab(year firm) vce(cluster firm)
    reghdfe a c , ab(year firm) vce(cluster firm)
    reghdfe a d, ab(year firm) vce(cluster firm)
    How to I create a table like this?
    Variables Coeff
    b
    c
    d
    I use outreg2 and it exports results as follows:
    Variables Coeff Coeff Coeff
    b
    c
    d

  • #2
    As advised in FAQ Advice #12, state the provenance of any commands that you reference that are not official Stata commands. outreg2 and reghdfe are from SSC. outreg2 is a dead end as it will not allow you to append models. Its -append- option is a merge option. What you want is achieved by Ben Jann's mergemodels routine (code enclosed below), after which you can use esttab from the Stata Journal/ SSC to export the output.

    Code:
    capt prog drop mergemodels
    prog mergemodels, eclass
     version 8
     syntax namelist
     tempname b V tmp
     foreach name of local namelist {
       qui est restore `name'
       mat `b' = nullmat(`b') , e(b)
       mat `b' = `b'[1,1..colsof(`b')-1]
       mat `tmp' = e(V)
       mat `tmp' = `tmp'[1..rowsof(`tmp')-1,1..colsof(`tmp')-1]
       capt confirm matrix `V'
       if _rc {
         mat `V' = `tmp'
       }
       else {
         mat `V' = ///
          ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
          ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
       }
     }
     local names: colfullnames `b'
     mat coln `V' = `names'
     mat rown `V' = `names'
     eret post `b' `V'
     eret local cmd "reghdfe"
    end
    
    webuse grunfeld, clear
    eststo m1: reghdfe invest mvalue, ab(company) vce(cluster company)
    eststo m2: reghdfe invest kstock, ab(company) vce(cluster company)
    eststo m3: reghdfe invest year, ab(company) vce(cluster company)
    mergemodels m1 m2 m3
    esttab ., noobs
    Res.:

    Code:
    . esttab ., noobs
    
    ----------------------------
                          (1)  
                                
    ----------------------------
    mvalue              0.190***
                       (5.04)  
    
    kstock              0.371***
                       (5.69)  
    
    year                8.461*  
                       (2.10)  
    ----------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      Thank you for your reply! Do I have to run the whole code in one do file? I did this and I get that:

      mergemodels m1 m2 m3
      ( is not a valid command name

      Comment


      • #4
        Yes, there is a line-break.

        ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
        ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )

        Comment


        • #5
          @Alberto Alvarez If you use Stata 17,You can use the new collection system to export the table.I will give you an example here.

          Code:
          webuse grunfeld,clear
          collect clear
          collect _r_b _r_se:reghdfe invest year,a(company) nocons
          collect _r_b _r_se:reghdfe invest mvalue,a(company) nocons
          collect _r_b _r_se:reghdfe invest kstock,a(company) nocons
          collect layout (colname#result) (cmdset) 
          collect style header result,level(hide)
          collect style header,level(value)
          collect remap cmdset[2 3]=cmdset[1 1]
          collect style row stack, nobinder spacer
          collect style column, extraspace(1)
          collect style cell,border(right,pattern(nil))
          collect style cell result[_r_se],sformat("(%s)")
          collect style cell,nformat(%5.3f)
          collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ",attach(_r_b)
          collect preview
          
          ----------------
                         1
          ----------------
          year    8.461***
                   (1.186)
                          
          mvalue  0.190***
                   (0.018)
                          
          kstock  0.371***
                   (0.019)
          ----------------
          
          collect export myreg.tex,replace
          Best
          Raymond
          Best regards.

          Raymond Zhang
          Stata 17.0,MP

          Comment


          • #6
            You can also modify the header "1" to "invest".
            Code:
            webuse grunfeld,clear
            collect clear
            collect _r_b _r_se:reghdfe invest year,a(company) nocons
            collect _r_b _r_se:reghdfe invest mvalue,a(company) nocons
            collect _r_b _r_se:reghdfe invest kstock,a(company) nocons
            collect layout (colname#result) (cmdset)
            collect style header result,level(hide)
            collect style header colname,level(value)
            collect remap cmdset[2 3]=cmdset[1 1]
            collect style row stack, nobinder spacer
            collect style column, extraspace(1)
            collect style cell,border(right,pattern(nil))
            collect style cell result[_r_se],sformat("(%s)")
            collect style cell,nformat(%5.3f)
            collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ",attach(_r_b)
            collect label levels cmdset 1 "invest"
            collect preview
            
            ----------------
                      invest
            ----------------
            year    8.461***
                     (1.186)
                            
            mvalue  0.190***
                     (0.018)
                            
            kstock  0.371***
                     (0.019)
            ----------------
            
            
            collect export myreg.tex,replace
            Last edited by Raymond Zhang; 23 May 2021, 00:24.
            Best regards.

            Raymond Zhang
            Stata 17.0,MP

            Comment


            • #7
              If you don't want to use "nocons" ,you can use the codes below:

              Code:
              webuse grunfeld,clear
              collect clear
              collect _r_b _r_se:reghdfe invest year,a(company) cl(company)
              collect _r_b _r_se:reghdfe invest mvalue,a(company) cl(company)
              collect _r_b _r_se:reghdfe invest kstock,a(company) cl(company)
              collect layout (colname[mvalue kstock year]#result) (cmdset) 
              collect style header result,level(hide)
              collect style header,level(value)
              collect remap cmdset[2 3]=cmdset[1 1]
              collect style row stack, nobinder spacer
              collect style column, extraspace(1)
              collect style cell,border(right,pattern(nil))
              collect style cell result[_r_se],sformat("(%s)")
              collect style cell,nformat(%5.3f)
              collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ",attach(_r_b)
              collect label levels cmdset 1 "invest"
              collect style header cmdset,level(label)
              collect preview
              
              ----------------
                        invest
              ----------------
              year    8.461***
                       (1.186)
                              
              mvalue  0.190***
                       (0.018)
                              
              kstock  0.371***
                       (0.019)
              ----------------
              
              
              collect export myreg.tex,replace
              Best regards.

              Raymond Zhang
              Stata 17.0,MP

              Comment

              Working...
              X