I use estout to generate tables of summary statistics and regression results that can be easily imported into LaTeX. The advantage is that the whole system is dynamic. If you change something in your do-file (e.g. you omit a particular group), you don’t have to change anything: the results get automatically updated in LaTeX. That has saved me a lot of time, but setting everything up took a long long time. So hopefully this post is helpful for aspiring applied econometricians who want to automate output reporting from Stata into LaTeX.
I think it is easiest to explain everything with examples, for which I use some tables from my current working paper. First install estout in Stata (ssc install estout
), then we can jump right into the examples. I explain three things: creating tables for 1. descriptive statistics and 2. regression output and 3. putting everything into LaTeX. Ready? Let’s start.
Edit: Have a look at my follow-up post if you encounter any problems!
Edit 2: Another follow-up to improve the design.
1. Descriptive Statistics
The principle of estout is simple: you run a command in Stata that generates some statistics, you tell estout to (temporarily) store those results and then you create a table.
Consider Table 2, which is simply a bunch of summarised variables, split into three categories. You cannot see it, but these are actually three tables appended together. Why? Because the first part (Age to Housing) is percentages and has therefore 2 decimal points, the second part (Household Finances) are income variables with 0 decimal points, and the last part has 2 decimal points again. The complete code that generates this table is then:
* TABLE 2
estpost su $dem if coholder100==1
est store A
estpost su $dem if coholder500==1
est store B
estpost su $dem if coholder1500==1
est store C
esttab A B C using table2.tex, replace ///
refcat(age18 "\emph{Age}" male "\emph{Demographics}" educationage "\emph{Education}" employeddummy "\emph{Employment}" oowner "\emph{Housing}", nolabel) ///
mtitle("> \pounds100" "> \pounds500" "> \pounds1500") ///
cells(mean(fmt(2))) label booktabs nonum collabels(none) gaps f noobs
estpost su $fin if coholder100==1
est store A
estpost su $fin if coholder500==1
est store B
estpost su $fin if coholder1500==1
est store C
esttab A B C using table2.tex, append ///
refcat(hhincome "\emph{Household Finances}", nolabel) ///
nomtitles ///
cells(mean(fmt(0))) label booktabs nonum f collabels(none) gaps noobs plain
estpost su $risk if coholder100==1
est store A
estpost su $risk if coholder500==1
est store B
estpost su $risk if coholder1500==1
est store C
esttab A B C using table2.tex, append ///
nomtitles ///
refcat(redundant "\emph{Income and Expenditure Risk}" literacyscore "\emph{Behavioural Characteristics}", nolabel) ///
stats(N, fmt(%18.0g) labels("\midrule Observations")) ///
cells(mean(fmt(2))) label booktabs nonum f collabels(none) gaps plain
Some little explanation (for a full list of commands see the estout/esttab manual)
refcat
includes a heading for a group of variables
mtitle
specifies the columns heading
cells
specifies the cell content (in the first part “mean” with 2 decimal place)
f
creates a fragment of the table, i.e. only the table content is exported to the .tex (see below for more information)
2. Regression Results
Reporting regression results is not as simple, but we are jumping right into a fairly complicated example (Table 4).
As you can see, we are reporting coefficients, standard errors and marginal effects, hence each specification has two columns and two rows. At the bottom we add a few additional statistics (estout can add every statistics that is saved in the e()
matrix).
This table is generated by the following code:
* TABLE 4
quietly probit coholder $dem $fin $bev $risk
predict pr2_coholder, pr
quietly su pr2_coholder
estadd scalar pr = r(mean)
estadd margins, dydx(*) atmeans
est store A
quietly probit coholder500 $dem $fin $bev $risk
predict pr2_coholder500, pr
quietly su pr2_coholder500
estadd scalar pr = r(mean)
estadd margins, dydx(*) atmeans
est store B
quietly probit coholder1500 $dem $fin $bev $risk
predict pr2_coholder1500, pr
quietly su pr2_coholder1500
estadd scalar pr = r(mean)
estadd margins, dydx(*) atmeans
est store C
esttab A B C using table4.tex, replace f ///
label booktabs b(3) p(3) eqlabels(none) alignment(S S) collabels("\multicolumn{1}{c}{$\beta$ / SE}" "\multicolumn{1}{c}{Mfx}") ///
drop(_cons spouse* ) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star) margins_b(star)" "se(fmt(3)par)") ///
refcat(age18 "\emph{Age}" male "\emph{Demographics}" educationage "\emph{Education}" employeddummy "\emph{Employment}" oowner "\emph{Housing}" hhincome_thou "\emph{Household Finances}" reduntant "\emph{Income and Expenditure Risk}" literacyscore "\emph{Behavioural Characteristics}", nolabel) ///
stats(N r2_p chi2 p pr, fmt(0 3) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{S}{@}") labels(`"Observations"' `"Pseudo \(R^{2}\)"' `"LR chi2"' `"Prob > chi2"' `"Baseline predicted probability"'))
What do we do here? First we quietly run a probit model, then we generate the baseline predicted probability and store this using estadd
. Finally, we calculate marginal effects and store them again using estadd
.
As you can see, there are many commands that generate that table. Step-by-step:
b(3)
and p(3)
: 3 decimal places for coefficients and standard errors
alignment (S S)
: alignment of the decimal places. As we have two data columns per specification (one for ß/SE
and one for Mfx
), we need to specify algnment for each column. Here we use the siunitx package (see below) to align the results at the decimal point. The alternative would be alignment (c c)
for centered data entries.
collabels
: labels for the data columns. As we want those centered we need the multicolumn
option
drop
: drop some results from the table
star
: specify how you want to report significance levels
cells
: specify the content for each cell
stats
: adds statistics below the results. We add N
(observations), r2_p
(pseudo R2), chi2
(LR chi2) p
(prob > chi2) and pr
(the baseline predicted probability, created before). Furthermore:
fmt
specifies the number of decimal places (here: N
has 0, all the following 3)
layout
: specify alignment. We want N
to be centered and all the following to be decimal aligned.
labels
: create some nice names
Refinements
If you are grouping variables with the refcat
command, you may want to indent the variables to create a nicer design as in my example. The following Stata command creates a 0.1cm indent for all variable labels. If you want some labels not to have this indent make sure to label (or relabel) this variables after you have run this command.
foreach v of varlist * {
label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
}
If you have some long column labels you might have to insert a manual line break to prevent the column from becoming to wide. The usual command in LaTeX for this is \\
, but that does not work in table columns. We create a special LaTeX command (see below) that takes care of that issue. If you need to insert a linebreak in your table, wrap the text into a \specialcell
field. Then you can use \\
as usual:
mtitle("\specialcell{Co-Holding\\> \pounds100}" "\specialcell{Co-Holding\\> \pounds500}" "\specialcell{Co-Holding\\> \pounds1500}") ///
3. Tables into LaTeX
Now begins the LaTeX hacking part. By default, estout generates a complete table and has the ability to include table titles above and notes below the table. But we are using the fragment option to generate the pure table content only. Why? Because it allows for much more flexibility, plus adding notes below the table with estout almost certainly breaks the width of the first column.
In order for this to work you need to add the following to your LaTeX preamble (i.e. before \begin{document}
):
% *****************************************************************
% Estout related things
% *****************************************************************
\newcommand{\sym}[1]{\rlap{#1}}% Thanks to David Carlisle
\let\estinput=\input% define a new input command so that we can still flatten the document
\newcommand{\estwide}[3]{
\vspace{.75ex}{
\begin{tabular*}
{\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\addlinespace[.75ex]
\end{tabular*}
}
}
\newcommand{\estauto}[3]{
\vspace{.75ex}{
\begin{tabular}{l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\addlinespace[.75ex]
\end{tabular}
}
}
% Allow line breaks with \\ in specialcells
\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
% *****************************************************************
% Custom subcaptions
% *****************************************************************
% Note/Source/Text after Tables
\newcommand{\figtext}[1]{
\vspace{-1.9ex}
\captionsetup{justification=justified,font=footnotesize}
\caption*{\hspace{6pt}\hangindent=1.5em #1}
}
\newcommand{\fignote}[1]{\figtext{\emph{Note:~}~#1}}
\newcommand{\figsource}[1]{\figtext{\emph{Source:~}~#1}}
% Add significance note with \starnote
\newcommand{\starnote}{\figtext{* p < 0.1, ** p < 0.05, *** p < 0.01. Standard errors in parentheses.}}
% *****************************************************************
% siunitx
% *****************************************************************
\usepackage{siunitx} % centering in tables
\sisetup{
detect-mode,
tight-spacing = true,
group-digits = false ,
input-signs = ,
input-symbols = ( ) [ ] - + *,
input-open-uncertainty = ,
input-close-uncertainty = ,
table-align-text-post = false
}
These commands to the following:
- Create two wrappers for estout generated tables.
\estwide
uses tabular*
and fills the table to the width of the text, \estauto
uses tabular
and uses the “standard” table width (i.e. width adjusted to your content).
- You need to specify three options immediately after the command (in the curly brackets).
\estwide{the .tex of the table}{the number of data columns}{alignment}.
Have look at my comment below to clarify the syntax further.
- In the second curly bracket enter the number of data columns, excluding the label column. In the example of Table 2 we have 3 data columns, in Table 4 we have 3 data columns as well, as the subcolumns “ß/SE” and “MFx” are considered to be one column.
- For alignment use the
S
option for decimal alignment using siunitx or C
to centre the data. (Hint: decimal alignment looks much, much better. Just look at any Journal).
- Add the
\specialcell
command for manual line break (see above).
- Add commands for subcaptions to include simple notes below the table using the
caption
package.
\figtext
adds some basic text
\fignote
adds text with “Note: ” before.
\figsource
adds text with “Source: ” before.
\starnote
adds information about significance levels
You can then include the tables into your latex document as follows:
% Table 2
\begin{table}
\caption{Sample Characteristics by the Amount of Co-Holding (\pounds)}
\estwide{table2.tex}{6}{c}
\label{table2}
\end{table}
% Table 4
\begin{table}
\caption{Probit Model for Characteristics of Co-Holders with Income Risk}
\estauto{table4}{3}{S[table-format=4.4]S[table-format=4.4]}
\starnote
\fignote{Omitted groups: \emph{Employment:} Student/Housewife/Disabled. \emph{Housing:} Private renter/Social renter. Further controls for spouse employment status.}
\label{table4}
\end{table}
That’s it. Not so difficult after all! I should add that there is a typographical issue if you are using different text and math fonts, as the brackets and asterisks are set in math font, but the digits in text font. A workaround is provided here, thanks to David Carlise.
Edit: Have a look at my comment below regarding the syntax of the \estauto \estwide
commands. The number of data columns (the second entry) and the alignment (third entry) might be a bit confusing at first, I hope that comment clarifies things a bit.
Links