Search code examples
regressionstatainteraction

Create regression tables with estout/esttab for interactions in Stata


In one of my models I use the standard built-in notation for interaction terms in Stata, in another, I have to manually code this. In the end, I would like to present nice regression tables, using esttab. How can I show identical, but slightly different coded, interaction terms in the same row? Or imagine, it's actually another interaction, how can I force esttab to ignore that?

// interaction model 1
sysuse auto, clear
regress price weight c.mpg##c.mpg foreign 
estimates store model1

// interaction model 2
gen int_mpg_mpg = mpg*mpg
regress price weight mpg int_mpg_mpg foreign
estimates store model2  

// make nice regression table sidy-by-side

// manual label manual interactions
label variable int_mpg_mpg "Mileage (mpg) # Mileage (mpg)"
esttab model1 model2, label

// export to latex
label variable int_mpg_mpg "Mileage (mpg) $\times$ Mileage (mpg) "
esttab model1 model2 using "table.tex", /// 
label nobaselevels beta not interaction(" $\times$ ") style(tex) replace

Output to console:

enter image description here

Output to LaTeX:

enter image description here

In both cases the manual variable label shows up as a name in regression tables. But identical variables names are not aligned in the same row. I am more interested in the solution for the LaTeX output, but the problem seems to be unrelated to LaTeX.


Solution

  • The issue should be addressed on the level "how Stata names the equations and coefficients across estimators". I adapted the code from Andrew:

    https://www.statalist.org/forums/forum/general-stata-discussion/general/1551586-align-nls-and-mle-estimates-for-the-same-variable-in-the-same-row-esttab

    He is using Ben Jann's program erepost from SSC (ssc install erepost).

    * model 1
    sysuse auto, clear
    eststo clear
    gen const=1
    qui regress price weight c.mpg##c.mpg foreign
    mat b=e(b)
    * store estimates 
    eststo model1 
    
    * model 2 
    gen int_mpg_mpg = mpg*mpg // generate interaction manually 
    qui regress price weight mpg int_mpg_mpg foreign 
    
    * rename interaction with additional package erepost 
    local coln "b:weight b:mpg b:c.mpg#c.mpg b:foreign b:_cons"
    mat colnames b= `coln'
    capt prog drop replace_b
    program replace_b, eclass
    erepost b= b, rename
    end
    replace_b
    eststo model2
    
    esttab model1 model2, mtitle("Interaction V1" "Interaction V2")
    

    Now, all interactions (automatic and manual) are aligned:

    --------------------------------------------
                          (1)             (2)   
                 Interactio~1    Interactio~2   
    --------------------------------------------
    main                                        
    weight              3.038***        3.038***
                       (3.84)          (3.84)   
    
    mpg                -298.1          -298.1   
                      (-0.82)         (-0.82)   
    
    c.mpg#c.mpg         5.862           5.862   
                       (0.90)          (0.90)   
    
    foreign            3420.2***       3420.2***
                       (4.62)          (4.62)   
    
    _cons              -527.0          -527.0   
                      (-0.08)         (-0.08)   
    --------------------------------------------
    N                      74              74   
    --------------------------------------------