**UPDATE: This post has been updated to fix the decimal alignment in Latex (May 2017)**

In this post, we want to generate a latex file using the stacked standard errors to keep results compact. Using the example in the previous post (Stata to Latex – Part 1), we add new regressions to the table to generate a more complicated output of the type shown below:

The table produces two regressions using price and foreign as dependent variables and regresses them on weight, and mpg. A second regression adds Length as an explanatory variable which is indicated under the table. Each regression has two specifications, where one normal standard errors are shown in round parentheses, while robust standard errors are shown in square brackets. This makes a total of eight regressions, which in a normal form, would make a very lengthy table. Given that the coefficients do not change, it is simpler to present by stacking the standard errors.

The stata code for generating the table is given in two parts. The first part generates the headers, the columns, and the first set of estimates. The second part adds the next set of rows. The code is given as follows:

sysuse auto, clear

replace weight = weight * 0.000453592 // we convert weight from pounds into tons.

lab var weight “Weight(tons)”

eststo clear // clear any existing stored estimates

foreach x in price foreign { // each loop generates a column for the tex file

eststo: regress `x’ weight mpg

estadd local Length “No” // here we add information on the controls at the end of the table

eststo: regress `x’ weight mpg length

estadd local Length “Yes”

}

esttab using “regressions_stacking.tex”, replace f ///

cells(b(fmt(2)) se(fmt(2) star par)) keep(weight) ///

label booktabs noobs nonotes nomtitle collabels(none) ///

mgroups(“Price” “Foreign”, pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

Several new commands are added here:

f: is for fragment which prevents stata from adding additional latex table commands. These will be used directly in the Latex file.

booktabs: Latex code for formatting tables using the booktabs packages. Gives a neater output.

mgroups: Creates the group header together with the line. Note the latex code inserted directly into Stata syntax.

eststo clear

foreach x in price foreign {

lab var weight ” “

eststo: regress `x’ weight mpg, vce(cluster make)

estadd local Length “No”

eststo: regress `x’ weight mpg length, vce(cluster make)

estadd local Length “Yes”

}

esttab using “regressions_stacking.tex”, append f ///

cells(se(fmt(2) star par([ ]))) keep(weight) ///

varlabels(, elist(weight \bottomrule)) ///

label booktabs nomtitle noline nonumber collabels(none) ///

scalars( “Length”)

Additional commands used to generate this table:

varlabels: This command adds a line below the last estimate. Since we are using the noline option here (the first part adds the necessary line), we still need to insert a divider between the last line of the estimates and the observations.

scalars: This adds the indicator for the controls we defined. Several scalars can be added to this file.

The above commands will create a tex file. We now have to insert this in a latex file to generate the output. We use the following preamble in the latex file:

\documentclass[12pt]{article}

\usepackage{booktabs} % For better looking tables

\usepackage{dcolumn} % Align on the decimal point of numbers in tabular columns

\newcolumntype{d}[1]{D{.}{.}{#1}}

\usepackage{threeparttable} % For better formatting of table notes

\usepackage[colorlinks,%

citecolor=black,

filecolor=black,%

linkcolor=black,%

urlcolor=black]{hyperref} % for linking between references, figures, TOC, etc in the pdf document

\setlength{\parindent}{0pt}

Joerg Weber’s brilliant website: http://www.jwe.cc/2012/03/stata-latex-tables-estout/ shows how esttab outputs can be customized. We make use of two of his user written commands: estwide, and Fignotes.

Estwide generates a table that is evenly spaced spanning the whole length of the page:

\let\estinput=\input % define a new input command so that we can still flatten the document

\newcommand{\estwide}[3]{

\vspace{.75ex}{

\textsymbols

\begin{tabular*}

{\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{#2}{#3}}

\toprule

\estinput{#1}

\bottomrule

\addlinespace[.75ex]

\end{tabular*}

}

}

\newcommand{\estauto}[3]{

\vspace{.75ex}{

\textsymbols

\begin{tabular}{l*{#2}{#3}}

\toprule

\estinput{#1}

\bottomrule

\addlinespace[.75ex]

\end{tabular}

}

}

\newcommand{\specialcell}[2][c]{%

\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}

}

\newcommand{\sym}[1]{\rlap{#1}}

We add the document tex preamble, now the main body of text:

\begin{document}

\title{Stacking variables: From Stata to Latex}

\date{\today}

\author{Asjad Naqvi\thanks{WU, Wien. \href{mailto:snaqvi@wu.ac.at}{snaqvi@wu.ac.at}}}

\maketitle

Adding the text on the section:

\section{Regressions}

Specification 1:

\begin{equation}

x_i = \beta_0 + \beta_1 Weight_i + \beta_2 Mpg_i

\end{equation}

Specification 2:

\begin{equation}

x_i = \beta_0 + \beta_1 Weight_i + \beta_2 Mpg_i + \beta_3 Length_i

\end{equation}

Results are given in Table \ref{table}.

The stata generated tex table can now be inserted as:

\begin{table}[h]

\caption{Customized table} \label{table}

\begin{threeparttable}

\estwide{regressions_stacking.tex}{6}{c}

\Fignote{Normal standard errors are in parentheses, robust standard errors in square brackets. Significance levels: * 10\%, ** 5\%, *** 1\%}

\end{threeparttable}

\end{table}

\end{document}

This will create the final file which should look something like this:

Stata_to_Latex_PDF_output.pdf

Previous post:

Stata to Latex – Part 1 – http://asjadnaqvi.com/stata-to-latex-part-1/

Hello. Thanks for the example, is really useful. I have one question though, in which part you calculate and display the robust standard errors? Many thanks in advance! Regards,

Pablo

Hi Pablo,

Robust standard errors are calculated using this line “regress `x’ weight mpg length, vce(cluster make)”.

A.

How do you align on decimal points? I see that you have a command for this but your PDF output is not really aligned. Thanks much!

Hi Giri,

You can use the dcolumn package to align the decimals in the PDF file:

\usepackage{dcolumn}

\newcolumntype{d}[1]{D{.}{.}{#1}}

Make sure all the packages are updated! I am updating the PDF to reflect this as well.

HTH!

A.

Hi,

thanks so much for this tutorial, I am learning how to use TeX with Stata and this was precious.

However, I used your code with different output from STATA that included 6 columns, and the table lines (all of them) do not span the whole table, ending short on the right side. Why could this be?

A.

Hi Andrea,

You might to play a bit with estwide options. For example try changing the number in the line below from 7 to the number of columns your table has:

\estwide{regressions_stacking.tex}{7}{c}

HTH!

Asjad

Hi Andrea,

I’m having the same problem. Did you manage to solve it? How you did it?

Hello,

your explanations were really clear, thank you very much. But this seems to only work when we output 1 explanatory variable (using append, for instance with 5 x_i, gives the 5 coefficient and s.e. of model with homoskedasticity and, after, the 5 s.e. of heteroskedasticity model). Could you give me a hint of how to reproduce the same output layout, but with more than one x_i?

Thank you very much!