Announcement

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

  • How to adjust a table exported with estout matrix()

    Hi there!

    I want to check whether a treatment effect differs across population subgroups. To this aim, I estimate several models that include an interaction terms and store their results in a matrix. Since I want to show the estimates for the interaction terms and the subgroup indicator in a joint row, I use the matrix-approach instead of exporting stored estimation results direclty with esttab, which would put the estimates for every variable in a new row. While this is neat in most situtations, I need a more condensed table that shows the estimation results for every subgroup in a new column.

    But there are a few details that I haven't yet figured out:
    1. How can I add significance stars to the table?
    2. Is there an automatic way to put parentheses around the standard errors?
    3. I would prefer the second, fourth and last row to have no labels and put a respective explanation on the standard errors in the footnote instead. How can I do that?
    Does anyone have any advice on these points? Below is a minimum working example that reproduces the problem.

    Best wishes,
    Kaja

    Code:
    *MWE
    use http://www.stata.com/data/jwooldridge/eacsap/nls81_87
    
    *prepare matrix:
    cap qui mat drop Mat
    mat Mat = J(6, 2,.)
    matrix colnames Mat = "Black" "In Manufacturing"
    mat rownames Mat = "Work experience" " " "Subgroup effect" " " "Interaction effect" " "
    mat list Mat
    
    *estimate whether treatment effect differs for specific population subgroups:
    areg wage c.exper##i.black i.year, absorb(id)
        matrix Mat[1,1]=e(b)[1,1] //b
        matrix Mat[2,1]=r(table)[2,1] //se
        *subgroup:
        matrix Mat[3,1]=r(table)[1,3] //b
        matrix Mat[4,1]=r(table)[2,3] //se
        *interaction:
        matrix Mat[5,1]=r(table)[1,5] //b
        matrix Mat[6,1]=r(table)[2,5] //se
    
    areg wage c.exper##i.manuf i.year, absorb(id)
        matrix Mat[1,2]=e(b)[1,1] //b
        matrix Mat[2,2]=r(table)[2,1] //se
        *subgroup:
        matrix Mat[3,2]=r(table)[1,3] //b
        matrix Mat[4,2]=r(table)[2,3] //se
        *interaction:
        matrix Mat[5,2]=r(table)[1,5] //b
        matrix Mat[6,2]=r(table)[2,5] //se
        
    *export table:
    estout matrix(Mat, fmt(%9.2fc)) using "MWE.tex", replace ///
        style(tex) label mlabels(,none) collabels("Black" "In manufacturing")

  • #2
    estout is from SSC, as you are asked to explain in FAQ Advice #12. Thanks for the reproducible example. Your problem can be viewed simply as a renaming / relabeling problem, rather than a case where you need to manually construct and export matrices. The key point is that estout/esttab already provide all the functionality you want (significance stars, automatic parentheses for standard errors, row suppression, and custom footnotes) as long as you work with stored estimation results instead of hand-built matrices. Creating matrices is therefore unnecessary here.

    Code:
    *MWE
    use http://www.stata.com/data/jwooldridge/eacsap/nls81_87, clear
    
    *prepare matrix:
    cap qui mat drop Mat
    mat Mat = J(6, 2,.)
    matrix colnames Mat = "Black" "In Manufacturing"
    mat rownames Mat = "Work experience" " " "Subgroup effect" " " "Interaction effect" " "
    mat list Mat
    
    *estimate whether treatment effect differs for specific population subgroups:
    areg wage c.exper##i.black i.year, absorb(id)
        matrix Mat[1,1]=e(b)[1,1] //b
        matrix Mat[2,1]=r(table)[2,1] //se
        *subgroup:
        matrix Mat[3,1]=r(table)[1,3] //b
        matrix Mat[4,1]=r(table)[2,3] //se
        *interaction:
        matrix Mat[5,1]=r(table)[1,5] //b
        matrix Mat[6,1]=r(table)[2,5] //se
    
    areg wage c.exper##i.manuf i.year, absorb(id)
        matrix Mat[1,2]=e(b)[1,1] //b
        matrix Mat[2,2]=r(table)[2,1] //se
        *subgroup:
        matrix Mat[3,2]=r(table)[1,3] //b
        matrix Mat[4,2]=r(table)[2,3] //se
        *interaction:
        matrix Mat[5,2]=r(table)[1,5] //b
        matrix Mat[6,2]=r(table)[2,5] //se
    
    *export table:
    estout matrix(Mat, fmt(%9.2fc)), replace ///
        label mlabels(,none) collabels("Black" "In manufacturing")
    
    
    *SUGGESTED SOLUTION
    *estimate whether treatment effect differs for specific population subgroups:
    rename black subgroup 
    eststo m1: areg wage c.exper##i.subgroup i.year, absorb(id)
    rename subgroup black
    
    rename manuf subgroup
    eststo m2: areg wage c.exper##i.subgroup i.year, absorb(id)
    rename subgroup manuf
    
    esttab m1 m2, se keep(exper 1.subgroup 1.subgroup#c.exper) ///
     varlab(exper "Work experience"  1.subgroup "Subgroup effect" 1.subgroup#c.exper "Interaction effect")    ///
     mlab("Black" "ln Manufacturing") starlevel(* 0.10 ** 0.05 *** 0.01) varwidth(20) modelwidth(20)
    Res.:

    Code:
    . *MWE
    
    . estout matrix(Mat, fmt(%9.2fc)), replace ///
    >     label mlabels(,none) collabels("Black" "In manufacturing")
    
    ----------------------------------------------
                                Black In manufac~g
    ----------------------------------------------
    Work experience          1,448.11     1,294.10
    r2                         726.83       742.84
    Subgroup effect              0.00       749.01
    r4                              .     1,415.12
    Interaction effect      -1,126.27        -2.17
    r6                         338.96       331.15
    ----------------------------------------------
    
    .
    .
    . *SUGGESTED SOLUTION
    .
    . esttab m1 m2, se keep(exper 1.subgroup 1.subgroup#c.exper) ///
    >  varlab(exper "Work experience"  1.subgroup "Subgroup effect" 1.subgroup#c.exper "Interaction effect")    ///
    >  mlab("Black" "ln Manufacturing") starlevel(* 0.10 ** 0.05 *** 0.01) varwidth(20) modelwidth(20)
    
    --------------------------------------------------------------------
                                          (1)                     (2)  
                                        Black        ln Manufacturing  
    --------------------------------------------------------------------
    Work experience                    1448.1**                1294.1*  
                                      (726.8)                 (742.8)  
    Subgroup effect                         0                   749.0  
                                          (.)                (1415.1)  
    Interaction effect                -1126.3***               -2.173  
                                      (339.0)                 (331.1)  
    --------------------------------------------------------------------
    N                                    2263                    2256  
    --------------------------------------------------------------------
    Standard errors in parentheses
    * p<0.10, ** p<0.05, *** p<0.01

    Comment


    • #3
      If you have Stata 17 or later, you could also use the inbuilt etable command along with the collect suite of commands:

      Code:
      use http://www.stata.com/data/jwooldridge/eacsap/nls81_87, clear
      
      collect clear
      
      rename black subgroup
      areg wage c.exper##i.subgroup i.year, absorb(id)
      estimates store black_model
      
      rename subgroup black
      rename manuf subgroup
      areg wage c.exper##i.subgroup i.year, absorb(id)
      estimates store manuf_model
      
      qui etable, est(black_model manuf_model) ///
              cstat(_r_b, nformat("%9.2fc")) cstat(_r_se, nformat("%9.2fc")) ///
              stars(.10 "*" .05 "**" .01 "***", attach(_r_b) decreasing) showstarsnote
              
      collect label levels etable_depvar 1 "Black" 2 "In Manufacturing", modify
      collect style header etable_depvar, level(label)
      collect label levels colname exper "Work experience" 1.subgroup "Subgroup Effect"  exper#1.subgroup "Interaction Effect", modify
      collect layout (coleq#colname[exper 1.subgroup exper#1.subgroup]#result[_r_b _r_se]) (etable_depvar#stars)
      collect export "MWE.tex", replace tableonly
      In the Results window, this produces:
      Code:
      . collect preview
      
      -------------------------------------------------
                             Black     In Manufacturing
      -------------------------------------------------
      Work experience     1,448.11 **      1,294.10 *  
                          (726.83)         (742.84)    
      Subgroup Effect                        749.01    
                                         (1,415.12)    
      Interaction Effect -1,126.27 ***        -2.17    
                          (338.96)         (331.15)    
      -------------------------------------------------
      * p<.1, ** p<.05, *** p<.01
      Last edited by Hemanshu Kumar; 06 Feb 2026, 11:58.

      Comment


      • #4
        Thank you, Andrew and Hemanshu! Your suggestions work wonderfully. Initially, I opted for the matrix approach because I considered renaming all subgroup indicators to be tedious work - turns out, the matrix approach was even more laborious

        Comment

        Working...
        X