Search code examples
latexexportstatacovariance

Exporting covariance table from Stata to LaTeX


I am trying to export a covariance table from Stata to LaTeX. The covariance is between democracy and oil, with democracy as my Y and oil as my primary x variable

Here is the Stata code I ran, and it runs without producing any errors, but once the table is exported into LaTeX, it does not appear in the format that I am looking for

ssc install estout
dataex democracy oilincome
corr democracy oilincome, cov
eststo clear
eststo table_cov
esttab, cells("democracy oilincome") noobs nonumb nomtitle collabels("Democracy" "Oil Reliance"), using Covariance_table.tex

Output:

dataex democracy oilincome

* Example generated by -dataex-. For more info, type help dataex
clear
input byte democracy float oilincome
 4     0
 4     0
 4     0
 4     0
 4     0
 4     0
 4     0
 4     0
 4     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
 9     0
10     0
10     0
10     0
10     0
10     0
 9     0
 9     0
 9     0
 9     0
 8     0
 8     0
 8     0
 8     0
 8     0
 8     0
 8     0
 8   .74
 8  2.02
 8  4.09
 8  6.52
 8  6.27
 8  5.24
 8  3.21
 8  5.42
 8  6.14
 8  8.38
10  9.59
10  9.49
10  7.32
10  5.34
10  6.79
10  9.91
10  13.4
10  8.21
10  7.78
10 10.71
10  9.95
10  9.72
10  9.68
10  8.51
10  7.86
10  7.97
10  7.41
10   9.3
10 12.39
10 14.64
10 13.09
10  9.97
10 10.72
10 14.61
10 25.85
10 25.37
10 16.55
10 17.14
10  24.6
end

And here is the covariance output

democ~cy oilinc~e
    
democracy     49.04
oilincome   -1662.3  6.3e   06

The LaTeX code I receive from my Stata code is:

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{cc}}
\hline\hline
            &   Democracy&Oil Reliance\\
\hline
\hline\hline
\end{tabular}
}

But the Latex table only shows the following:

enter image description here


Solution

  • You need to store the estimates, estpost is designed for this (see help estpost). You also should not clear the estimates before tabulating them. This line: eststo clear should come at the end of the code. Try something like the following:

    sysuse auto
    estpost correlate price turn foreign rep78, matrix listwise
    esttab using "Corr.tex", unstack not noobs compress
    

    It's worth reviewing the excellent documentation for the estout family of commands.