Search code examples
matrixlatexstata

Tabulate a combined matrix of coefficients / t-statistics and export in LaTeX


I have many regressions (one per year, over fifteen years) for four outcome variables.

I have created two 15x4 matrices: one with the coefficients and one with the t-statistics. I am stuck now that I want to tabulate them in the classic way, each row having the coefficients for each model and under them the respective t-statistics between brackets.

I can tabulate a single matrix but even using estadd I was not able to obtain said result.

This is my code:

*creates matrix of all results row(year) column(outcome)
matrix OLS_years = r_Mall, r_hotMall, r_totMall, r_irpMall
matrix list OLS_years
matrix TSTATS = ex_Tall, hot_Tall, tot_Tall, irp_Tall
matrix list TSTATS 

Using the community-contributed command estout only produces a table with the coefficients:

    estout matrix(OLS_years), ///
     cells(OLS_years TSTATS(par)) ///
     stats(N, fmt(0) labels ("No. of Obs." )) ///
     starlevels(* 0.1 ** 0.05 *** 0.001) ///
     varwidth(20) ///
     modelwidth(12) ///
     delimiter(&) ///
     end(\\) ///
     prehead(`"\begin{tabular}{l*{4}{c}}"' `"\toprule"') ///
     posthead("\midrule") ///
     prefoot("\midrule") ///
     postfoot(`"\bottomrule"' `"\multicolumn{@span}{l}{\footnotesize t-statistics in parentheses}\\"' `"\multicolumn{@span}{l}{\footnotesize *** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.}\\"' `"\end{tabular}"') ///
     varlabels(_cons Constant, end("" \addlinespace) nolast) ///
     eqlabels(, begin("\midrule" "") nofirst) ///
     substitute(_ \_ "\_cons " \_cons) ///
     interaction(" $\times$ ") ///
     notype ///
     level(95) ///
     style(esttab)

Can anyone help?


Solution

  • My challenge was to apply the method explained by @Pearly Spencer to a more complex settings, where I had to stack regression estimates from 4 different specification and 15 years in one ready-to-input Latex table. I solved that by using -esttab- with the -noisily- option, which is very handy because it shows you the estout commands behind it (esttab is a wrapper for estout). That way I managed to personalise the three parts of the table (top, mid, bottom) so that I could nicely stack them. I am posting the code in a general fashion, where y1, y2, y3, y4 are different outcome variables and T is a treatment variable, for which you want to see the effect on the y's.

    set more off
    
    *-----------------FIRST YEAR I.E. TOP PART OF THE TABLE
    eststo clear
        label var T "T 2002"
    
                    qui reg Y1 T `x_prop' ///
                    if year==2002 
                    eststo y1_2002
    
                    qui reg Y2 T `x_prop' ///
                    if year==2002 
                    eststo y2_2002
    
                    qui reg Y3 T `x_prop' ///
                    if year==2002 
                    eststo y3_2002
    
                    qui reg Y4 T `x_prop' ///
                    if year==2002 
                    eststo y4_2002
    
    qui cap erase "C:\Users\...\tabz.tex"   // the replace option did not seem to work, so I used this way instead
    
    estout using `"C:\Users\...\tabz.tex"' , ///
     cells(b(fmt(a3) star) t(fmt(2) par)) ///
     starlevels(* 0.1 ** 0.05 *** 0.001) ///
     varwidth(20) ///
     modelwidth(12) ///
     delimiter(&) ///
     end(\\) ///
     prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{4}{c}} \toprule  &\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}&\multicolumn{1}{c}{(8)}\\") ///
     posthead("\midrule") ///
     label ///
     varlabels(_cons Constant, end("" \addlinespace) nolast) ///
     mlabels(, depvar span prefix(\multicolumn{@span}{c}{) suffix(})) ///
     collabels(none) ///
     eqlabels(, begin("\midrule" "") nofirst) ///
     substitute(_ \_ "\_cons " \_cons) ///
     interaction(" $\times$ ") ///
     notype ///
     level(95) ///
     style(esttab) ///
     keep(T) 
    
     *-----------------OTHER YEARS I.E. MID PART OF THE TABLE
    forval i=2003/2015 { 
    eststo clear
    
        label var T "T `i'"
                    qui reg Y1 T `x_prop' ///
                    if year==`i'
                    eststo y1_`i'
    
                    qui reg Y2 T `x_prop' ///
                    if year==`i'
                    eststo y2_`i'
    
                    qui reg Y3 T `x_prop' ///
                    if year==`i'
                    eststo y3_`i'
    
                    qui reg Y4 T `x_prop' ///
                    if year==`i'
                    eststo y4_`i'
    
    esttab using "C:\Users\...\tabz.tex\tabz.tex", keep(T) append f label nomtitle nonumber collabels(none) ///
    starlevels(* 0.1 ** 0.05 *** 0.001) booktabs gaps noline ///
        notes addnotes("t-statistics in parentheses" "*** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.") ///
        noobs
    }
    
     *-----------------OTHER YEARS I.E. BOTTOM PART OF THE TABLE
    
    eststo clear
    
        label var T "T 2016"
                    qui reg Y1 T `x_prop' ///
                    if year==2016 & violat_2016==0
                    eststo y1_2016
    
                    qui reg Y2 T `x_prop' ///
                    if year==2016 & violat_2016==0
                    eststo y2_2016
    
                    qui reg Y3 T `x_prop' ///
                    if year==2016 & violat_2016==0
                    eststo y3_2016
    
                    qui reg Y4 T `x_prop' ///
                    if year==2016 & violat_2016==0
                    eststo y4_2016
    
    esttab using "C:\Users\...\tabz.tex", keep(T) append f label nomtitle nonumber collabels(none) ///
    starlevels(* 0.1 ** 0.05 *** 0.001) booktabs gaps noline ///
        notes addnotes("t-statistics in parentheses" "*** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.") ///
         prefoot("\midrule") ///
         postfoot("\bottomrule \multicolumn{5}{l}{\footnotesize t-statistics in parentheses}\\ \multicolumn{5}{l}{\footnotesize *** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.}\\ \end{tabular}")
    

    I hope this will be useful for someone else, as it took me long long hours.

    This was the result (bold text modified manually