Creating side-by-side summary statistics table using esttab - latex

I am trying to create a summary stats table with different categorical variables.
This is what I have managed to achieve with the community-contributed command esttab:
The code I have used is the following:
estpost tabstat diff_32 diff_43 diff_54 diff_42 diff_52 diff_53, ///
by(es) stat(mean sd) nototal columns(stat)
esttab . using "$tables/25_trial", replace cells(mean sd) tex label nogaps
However, what I need is for the columns to be side-by-side such that my mean is the main cell, with standard deviation as auxiliary. In other words, each column would contain one categorical variable, with mean and standard deviation in the same cell.
How can I produce the desired output?

Consider the following toy example:
sysuse auto, clear
estpost tabstat price weight mpg, by(foreign) stat(mean sd) nototal columns(stat)
esttab ., cells(mean sd) label nogaps tex
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)}\\
&\multicolumn{1}{c}{}\\
& mean/sd\\
\hline
Domestic & \\
Price & 6072.423\\
& 3097.104\\
Weight (lbs.) & 3317.115\\
& 695.3637\\
Mileage (mpg) & 19.82692\\
& 4.743297\\
\hline
Foreign & \\
Price & 6384.682\\
& 2621.915\\
Weight (lbs.) & 2315.909\\
& 433.0035\\
Mileage (mpg) & 24.77273\\
& 6.611187\\
\hline
Observations & 74\\
\hline\hline
\end{tabular}
}
One has to merely use the unstack option in esttab to get the desired output:
esttab ., cells(mean sd) label nogaps tex unstack
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{2}{c}}
\hline\hline
&\multicolumn{2}{c}{(1)} \\
&\multicolumn{2}{c}{} \\
& Domestic& Foreign\\
& mean/sd& mean/sd\\
\hline
Price & 6072.423& 6384.682\\
& 3097.104& 2621.915\\
Weight (lbs.) & 3317.115& 2315.909\\
& 695.3637& 433.0035\\
Mileage (mpg) & 19.82692& 24.77273\\
& 4.743297& 6.611187\\
\hline
Observations & 74& \\
\hline\hline
\end{tabular}
}

Related

Stata - Esttab to Latex Decimal Alignment

I am trying to output a LaTex file using Stata's esttab command. I cannot get the decimals to align, nor can I get the nice formatting from the "booktabs" option to work. I also would like to increase the width of the columns. I have been tinkering with this for hours and have had no luck. Below is my Stata code and the LaTex code that I am running through Overleaf. Any help would be greatly appreciated!
Here is my Stata code:
#delimit ;
esttab r1 r2 r3 r4 using "C:\Users\user\Dropbox\Private
Code\Code\STATA\latex\table1.tex", replace
b(3) nomtitle label star(* 0.10 ** 0.05 *** 0.01)
booktabs alignment(D{.}{.}{-1})
title(Placeholder)
s(N DAY FFE FIRM_QUARTER r2_a,
fmt(%9.0fc 0 0 0 3)
label("Observations" "Day FE" "Firm FE" "Firm-Quarter FE" "Adj. R-Sq{\super 2}"))
compress nocons;
Here is the LaTex code it spits out:
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{Placeholder}
\begin{tabular}{l*{4}{D{.}{.}{-1}}}
\toprule
&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}
{c}{(3)} &\multicolumn{1}{c}{(4)} \\
\midrule
VAR1 & 0.007\sym{***}& 0.007\sym{***}& 0.006\sym{***}& 0.007\sym{***}\\
& (8.55) & (7.82) & (7.67) & (8.54) \\
\addlinespace
VAR2 & 0.132\sym{***}& 0.131\sym{***}& 0.131\sym{***}& 0.132\sym{***}\\
& (20.35) & (20.15) & (20.15) & (20.35) \\
\midrule
Observations &1,315,077 &1,315,077 &1,315,077 &1,315,077
\\
Day FE & No & Yes & Yes & No \\
Firm FE & Yes & No & Yes & No \\
Firm-Quarter FE & No & No & No & Yes \\
Adj. R-Sq{\super 2}& 0.045 & 0.027 & 0.046 & 0.044
\\
\bottomrule
\multicolumn{5}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{5}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(
p<0.01\)}\\
\end{tabular}
\end{table}
Since you haven't provided the full code, I can't see what would your output be. When I add \documentclass{article} with missing packages in a preamble, I get very wide table which does not fit a page. Also, I don't know whether the code is automatically generated or created/altered by you.
Assuming the latter, I would do the following. Since you add footnotes in the table, I would enclose tabular within threeparttable, which splits the area for table into three parts: top for captions, middle for a table definition e.g. tabular, and bottom part for footnotes. It also adds a convenient macro \tnote{}.
Secondly, dcolumn causes big numbers to be pushed way too much to the left. You should let dcolumn suppress their alignment. I personally don't know how, although I assume there is a way.
I prefer siunitx for formatting numbers in tables. However siunitx needs a bit more work. It automatically parses numbers in cells and any non-number content generates errors. Therefore, non-numbers need to be enclosed within {...} which instructs siunitx to leave the content intact. It also forces numbers not to be formatted, aligned etc., which is what I did for the big numbers you have in the table. There's also option to format thousands and millions with any separator of your choice. For instance you could use one of the macros: \,, :, or \; for thin, mid or thick space, respectively instead of ,.
Below is my suggestion for your table based on siunitx and threeparttable, so you can compare it with your approach.
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{threeparttable}
\newcommand\super[1]{\textsuperscript{#1}}
\sisetup{
group-separator={,},
table-format=1.3,
table-align-text-after = false,
}
\begin{document}
\begin{table}[htbp]
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\centering
\begin{threeparttable}
\caption{Placeholder}
\begin{tabular}{l *{4}{S}}
\toprule
& \multicolumn{1}{c}{(1)}
& \multicolumn{1}{c}{(2)}
& \multicolumn{1}{c}{(3)}
& \multicolumn{1}{c}{(4)} \\
\midrule
{VAR1} & 0.007\tnote{***} & 0.007\tnote{***} & 0.006\tnote{***} & 0.007\tnote{***} \\
& {(8.55)} & {(7.82)} & {(7.67)} & {(8.54)} \\
\addlinespace
VAR2 & 0.132\tnote{***} & 0.131\tnote{***} & 0.131\tnote{***} & 0.132\tnote{***} \\
& {(20.35)} & {(20.15)} & {(20.15)} & {(20.35)} \\
\midrule
Observations
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}} \\
Day FE & {No} & {Yes} & {Yes} & {No} \\
Firm FE & {Yes} & {No} & {Yes} & {No} \\
Firm-Quarter FE & {No} & {No} & {No} & {Yes} \\
Adj. R-Sq{\super2} & 0.045 & 0.027 & 0.046 & 0.044 \\
\bottomrule
\end{tabular}
\par\(t\) statistics in parentheses
\par\tnote{*} \(p < 0.10\),\quad\tnote{**} \(p < 0.05\),\quad\tnote{***} \(p < 0.01\)
\end{threeparttable}
\end{table}
\end{document}
EDIT. I also enclosed statistics in{...} because siunitx does not recognised brackets as part of a number.

Latex table moves out of section and into the next page

I am making a journal paper with 2 columns. But I need table for the entire column. Using the below code table is pushed out of the comparatively analysis section into a new page.
\section{Comparatively Analysis}
\hfil\break
\begin{table*}
\centerline {TABLE 1}
\begin{center}
\begin{tabular} {|c|c|c|c|c|}
\hline
\multicolumn{5} { | c | }{Comparison Table}\\
\hline
Paper & Dataset & Approach & Model & Evaluation \\
\hline
Mary[7] & Air Pollution & Statistical & ST Correlation & RMSE 3.54 for 15\% missing data\\
\hline
Zhang[10] & Water Quality & Deep Learning & Seq2Seq & RMSE 3.29 \\
\hline
Turabieh[11] & Mammographic Mass, Hepatitis & Soft Computing & ANFIS-GA,ANFIS-PSO& Accuracy 85.2\% \\
\hline
Verma[12] & MIT-BIH ECG & Deep Learning & LSTM & 5-step RMSE 0.1 and 10-step RMSE 0.07 \\
\hline
Al-Milli[13] & Mammographic Mass & Deep Learning & Jordan RNN & Accuracy 84.27\% \\
\hline
\end{tabular}
\end{center}
\end{table*}
\hfil\break
\section{Conclusion}
Due to improper internet communications
TABLE OUT OF SECTION
NEW PAGE TABLE PUSHED
The main issue is that the table is too large. You have to use resizebox method from graphicx package. Here I provide a slightly better-looking table from the tabu package; this format is widely used in academia and in textbooks.
\usepackage{tabu, makecell, graphicx, booktabs}
%%Begin document (...)
\section{Comparatively}
\hfil\break
\begin{center}
\tabulinesep=1.6mm %%height between lines
\resizebox{1.1\columnwidth}{!}{ %% 1.1 is the scale factor of the column width
\begin{tabu}{ccccc}
\toprule
\toprule
\multicolumn{5}{c}{\ \ \ Comparison Table}\\
\multicolumn{1}{c}{\textit{Paper}}&\multicolumn{1}{c}
{\textit{Dataset}}&\multicolumn{1}{c}{\textit{Approach}}&\multicolumn{1}{c}
{Model}&\multicolumn{1}{c}{\textit{ Evaluation}}\\
\cmidrule(lr){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
\cmidrule(lr){5-5}
Mary$[7]$ & Air Pollution & Statistical & ST Correlation & \makecell{RMSE $3.54$ for \\ $15\%$ missing data } \\
Zhang$[10]$ & Water Quality & Deep Learning & Seq$2$Seq & RMSE $3.29$ \\
Turabieh$[11]$ & \makecell{Mammographic mass \\ Hepatitis} & Soft Computing & \makecell{ANFIS-GA,\\ ANGIS-PSO} & Accuracy $85.2\%$ \\
\bottomrule
\bottomrule
\end{tabu}
}
\end{center}
{ \footnotesize\textbf{Table 1:} Table description }
\hfil\break
\section{Conclusion}
Due to improper internet communications
Change the `scale factor back to 1.0 if the table seems to overlap to much on the right side. If not keep it at 1.1 for better visibility.

How to include "&" in variable label for LaTeX output using esttab?

I am trying to export some regression tables from Stata to LaTeX.
The problem is that I want to display variable labels in the table but some of my labels contain the & character. As such, when I use the community-contributed command esttab to export to LaTeX and then try to compile, I get an error because the Texmaker thinks & should indicate an extra column.
Below is my Stata code:
esttab results1 results2 using "$repodir/output/tables/tract_xregs.tex", ///
se noconstant label star(* 0.10 ** 0.05 *** 0.01) replace ///
nonotes compress nomtitles booktabs ///
s(modelsample modelobs, label("Sample" "N"))
esttab sumstats1 using "$repodir/output/tables/tract_sumstats.tex", booktabs label ///
nonumbers cells("mean p50 min max sd") replace
How can I include the & character in my variable label without getting an error when compiling it?
Consider the following example using Stata' toy auto dataset, which reproduces your problem:
sysuse auto, clear
estimates clear
label variable weight "One & Two"
regress price weight
estimates store ols
esttab ols, label tex
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)}\\
&\multicolumn{1}{c}{Price}\\
\hline
One & Two & 2.044\sym{***}\\
& (5.42) \\
[1em]
Constant & -6.707 \\
& (-0.01) \\
\hline
Observations & 74 \\
\hline\hline
\multicolumn{2}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{2}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
}
You need to include a \ before the & in the variable label as follows:
label variable weight "One \& Two"
esttab ols, label tex
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)}\\
&\multicolumn{1}{c}{Price}\\
\hline
One \& Two & 2.044\sym{***}\\
& (5.42) \\
[1em]
Constant & -6.707 \\
& (-0.01) \\
\hline
Observations & 74 \\
\hline\hline
\multicolumn{2}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{2}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
}
The former produces an error, while the latter compiles fine.

Table title with outreg2

I am working with the community-contributed command outreg2 to export regression estimates from a logit model to LaTeX.
Is there a way to show the table reference/label, for example, "Table 1: Some Title"?
I cannot seem to be able to do this using outreg2:
logit v1 v2 v43, or
outreg2 using "table", label cti(odds ratio) eform tex replace ctitle(Logit)
With esttab, using \label in the title option to get the desired result works:
esttab using "Table.tex",tex eform label replace title("Logit"\label{tab1})
Any suggestions?
EDIT:
Note that even when i do the following:
outreg2 using "table", label cti(odds ratio) eform tex replace ///
ctitle(Logit) title("Logit"\label{tab1})
This only produces the following:
Consider the following toy example:
webuse lbw
logit low age lwt i.race smoke ptl ht ui, or
Both esttab and outreg2 produce the same result when their title() option is used:
esttab, tex eform label replace title("Logit"\label{tab1})
outreg2 using "table", tex label cti(odds ratio) eform replace title("Logit"\label{tab1})
However, this will not work when typesetting as outreg2 produces
different markup than esttab.
The way to do what you want with outreg2 is to run the following from Stata:
outreg2 using table, tex replace label cti(odds ratio) eform
Unlike esttab, the outreg2 command does not have an option to insert markup
directly from within Stata. Thus you will have to add the markup below manually:
.
.
.
\begin{table}[htbp]\centering
\caption{Logit \label{tab1}}
.
.
.
\end{table}
.
.
.
The produced tex file should then look as follows:
\documentclass[]{article}
\setlength{\pdfpagewidth}{8.5in} \setlength{\pdfpageheight}{11in}
\begin{document}
\begin{table}[htbp]\centering
\caption{Logit \label{tab1}}
\begin{tabular}{lc} \hline
& (1) \\
VARIABLES & odds ratio \\ \hline
& \\
birthweight<2500g & \\
& \\
age of mother & 0.973 \\
& (0.0355) \\
weight at last menstrual period & 0.985** \\
& (0.00682) \\
race = 2, black & 3.535** \\
& (1.861) \\
race = 3, other & 2.368** \\
& (1.040) \\
smoked during pregnancy & 2.518** \\
& (1.009) \\
premature labor history (count) & 1.719 \\
& (0.595) \\
has history of hypertension & 6.250*** \\
& (4.322) \\
presence, uterine irritability & 2.135* \\
& (0.981) \\
Constant & 1.586 \\
& (1.910) \\
& \\
Observations & 189 \\ \hline
\multicolumn{2}{c}{ seEform in parentheses} \\
\multicolumn{2}{c}{ *** p$<$0.01, ** p$<$0.05, * p$<$0.1} \\
\end{tabular}
\end{table}
\end{document}
Leaving this here for future reference.
Actually, if you add the option tex(fragment) to outreg2, the resulting file will only contain the tabular environment. Then, you can create the table environment, with the caption that you desire, and use \input only to replace the tabular environment.

Fit a table within \textwidth in Latex

I have a table that is wider than the \textwidth of my document. (I don't want to change the \textwidth.)
I want to force the table to be fitted in \textwidth by wrapping the last (rightest column) column's text.
This is my code for my table:
\begin{table*}[!t]
\caption{Extracted Features used in Meta-Learning} \label{sample-features}
\label{tbl:features}
\begin{center}
\begin{tabular*}{0.95\textwidth,center}{|l||l|}
{\textbf{FEATURE}} &{\textbf{DESCRIPTION}} \\
\hline \\
n.examples & Number of examples \\
n.attrs & Number of attributes \\
prop.symbolic.attrs & Proportion of symbolic attributes \\
prop.missing.values & Proportion of missing values \\
class.entropy & Class entropy \\
avg.mutual.information & Average mutual information \\
prop.h.outlier & Proportion of continuous attributes with outliers \\
avg.attr.entropy & Average attribute entropy \\
avg.symb.pair.mutual.infor & Average mutual information between pairs of symbolic attributes \\
avg.abs.attr.correlation & Average absolute correlation between continuous attributes \\
avg.skewness & Mean skewness of attributes \\
avg.abs.skewness & Mean absolute skewness of attributes \\
avg.kurtosis & Mean kurtosis of attributes \\
canonical.correlation.best.linear.combination & Canonical correlation of the best linear combination of attributes to distinguish between classes \\
relative.prop.best.linear.combination & Proportion of the total discrimination power explained by the best linear combination \\
\end{tabular*}
\end{center}
\end{table*}
The result of this code is the following document:
The part in the right side of the red line (added by myself) should be wrapped.
Any help?
Here is an answer. I hope it can help you:
Using package tabularx as follows:
\usepackage{tabularx,booktabs}
.
.
.
\begin{table*}[!t]
\caption{Caption}
\label{tbl:tbl}
\centering
\begin{tabularx}{\textwidth}{X X}
\toprule
Item 1 & Item 2 \\
\midrule
Item 1 description & Item 2 description \\
.
.
.
\bottomrule
\end{tabularx}
\end{table*}
In my case the solution is as follows:
\begin{table*}[!t]
\caption{Extracted Features used in Meta-Learning} \label{sample-features}
\label{tbl:features}
\centering
\begin{tabularx}{0.95\textwidth}{|X||X|}
\toprule
{\textbf{FEATURE}} &{\textbf{DESCRIPTION}} \\
\midrule
n.examples & Number of examples \\
n.attrs & Number of attributes \\
prop.symbolic.attrs & Proportion of symbolic attributes \\
prop.missing.values & Proportion of missing values \\
class.entropy & Class entropy \\
avg.mutual.information & Average mutual information \\
prop.h.outlier & Proportion of continuous attributes with outliers \\
avg.attr.entropy & Average attribute entropy \\
avg.symb.pair.mutual.infor & Average mutual information between pairs of symbolic attributes \\
avg.abs.attr.correlation & Average absolute correlation between continuous attributes \\
avg.skewness & Mean skewness of attributes \\
avg.abs.skewness & Mean absolute skewness of attributes \\
avg.kurtosis & Mean kurtosis of attributes \\
canonical.correlation.best.linear.combination & Canonical correlation of the best linear combination of attributes to distinguish between classes \\
relative.prop.best.linear.combination & Proportion of the total discrimination power explained by the best linear combination \\
\bottomrule
\end{tabularx}
\end{table*}

Resources