你好,我尝试用 Latex 写论文,但我没有乳胶使用经验。我通常在乳胶中处理文本和数学函数,处理表格时却遇到了很大困难。
我正在使用 python 的 stargazer 包从 python 输出回归。
stargazer 包输出的 latex 表不太好。输出的表太宽。而且包中似乎有一个错误,要求的列数不正确。我想使用主表上方调用的函数来更正表的列和格式。
我有五个回归要报告。但我对一系列数据集进行了相同的分析。
我想要一种方法来编写一个函数来调整所有可能的相同格式的表格。即使用完全相同的方程式,只是结果不同。这将使我能够快速报告许多结果表。
下面是我想以可迭代方式重新处理的代码示例:
有什么好办法吗?
提前致谢
\begin{table}[!htbp] \centering
\caption{Merton Selection Algorithm Results}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lcc} %%% THIS IS WRONG
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{5}{c}{\textit{Dependent variable:}} \
\cr \cline{5-6}
\\[-1.8ex] & (1) & (2) & (3) & (4) & (5) \\
\hline \\[-1.8ex]
Constant or Alpha & 0.00368$^{***}$ & 0.00235$^{**}$ & 0.00463$^{***}$ & 0.00331$^{***}$ & 0.00332$^{***}$ \\
& (0.00092) & (0.00092) & (0.00104) & (0.00096) & (0.00088) \\
& & & & & \\
Index & 0.24083$^{***}$ & & & & 0.20465$^{***}$ \\
& (0.03294) & & & & (0.05731) \\
& & & & & \\
Best Random & & 0.30741$^{***}$ & & & 0.18873$^{***}$ \\
& & (0.03621) & & & (0.04733) \\
& & & & & \\
Worst Random & & & 0.08992$^{**}$ & & -0.17774$^{***}$ \\
& & & (0.04332) & & (0.05362) \\
& & & & & \\
Mean Randoms & & & & 0.37352$^{***}$ & 0.07392$^{}$ \\
& & & & (0.05704) & (0.11988) \\
& & & & & \\
\hline \\[-1.8ex]
Observations & 156.0 & 156.0 & 156.0 & 156.0 & 156.0 \\
R${2}$ & 0.25768 & 0.31885 & 0.02721 & 0.21783 & 0.40213 \\
Adjusted R${2}$ & 0.25286 & 0.31442 & 0.02089 & 0.21275 & 0.3863 \\
Residual Std. Error & 0.01132(df = 154.0) & 0.01084(df = 154.0) & 0.01296(df = 154.0) & 0.01162(df = 154.0) & 0.01006(df = 151.0) \\
F Statistic & 53.45841$^{***}$ (df = 1.0; 154.0) & 72.0871$^{***}$ (df = 1.0; 154.0) & 4.30756$^{**}$ (df = 1.0; 154.0) & 42.88879$^{***}$ (df = 1.0; 154.0) & 25.39116$^{***}$ (df = 4.0; 151.0) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{5}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
答案1
我建议进行以下改进:S
最后 5 列的列类型,使小数点的数字对齐,规则是booktabs
在其周围有一些垂直填充,加载caption to control the spacing between caption and table, and of
makecell` 以在标准单元格中启用换行。
我擅自将因变量文本与最后两个单元格相对应,因为它与下面的短行一致,但我不确定它是否明智。无论如何,它很容易修改。
\documentclass{article}
\usepackage{geometry}
\usepackage{array, makecell, booktabs, caption}
\usepackage{siunitx}
\captionsetup{skip=6pt}
\begin{document}
\begin{table}[!htbp]
\sisetup{table-format=-1.5, table-number-alignment=center, table-space-ext-pre=(),%
table-align-text-pre=false, table-space-text-post=***}
\setlength{\tabcolsep}{3pt}
\renewcommand{\cellalign}{tc}
\centering
\caption{Merton Selection Algorithm Results}
\label{}
\small
\begin{tabular}{@{}l*6{S}} %
\toprule\midrule[0.4pt]
& & & & \multicolumn{2}{c}{\textit{Dependent variable:}} \\
\cmidrule{5-6}
& {(1) }& {(2)} & {(3)} & {(4)} & {(5)} \\
\midrule
Constant or Alpha & 0.00368$^{***}$ & 0.00235$^{**}$ & 0.00463$^{***}$ & 0.00331$^{***}$ & 0.00332$^{***}$ \\
& {(}0.00092{)} & {(}0.00092{)} &{(}0.00104{)} & {(}0.00096{)} & {(}0.00088{)} \\
& & & & & \\
Index & 0.24083$^{***}$ & & & & 0.20465$^{***}$ \\
& {(}0.03294{)} & & & & {(}0.05731{)} \\
& & & & & \\
Best Random & & 0.30741$^{***}$ & & & 0.18873$^{***}$ \\
& & {(}0.03621{)} & & & {(}0.04733{)} \\
& & & & & \\
Worst Random & & & 0.08992$^{**}$ & & -0.17774$^{***}$ \\
& & & {(}0.04332{)} & & {(}0.05362{)} \\
& & & & & \\
Mean Randoms & & & & 0.37352$^{***}$ & 0.07392$^{}$ \\
& & & & {(}0.05704{)} & {(}0.11988{)} \\
\addlinespace[2ex]
\midrule
Observations & {156.0} & {156.0} & {156.0} & {156.0} & {156.0} \\
R${2}$ & {0.25768} & {0.31885} & {0.02721} & {0.21783} & {0.40213} \\
Adjusted R${2}$ & {0.25286} & {0.31442} & {0.02089} & {0.21275} & {0.3863\phantom{0}} \\
\addlinespace[1ex]
\makecell[l]{ Residual Std. Error\\ df = } &{\makecell{0.01132\\(154.0)}} & {\makecell{0.01084\\(154.0)}} & {\makecell{0.01296\\(154.0)}} & {\makecell{0.01162\\(154.0)}} & {\makecell{0.01006\\(151.0)}} \\
\addlinespace[1ex]
\makecell[l]{F Statistic\\ df = } &{\makecell{53.45841$^{***}$\\ (1.0; 154.0)}} & {\makecell{72.0871$^{***}$\\ (1.0; 154.0)}} & {\makecell{4.30756$^{**}$\\ (1.0; 154.0)}} & {\makecell{42.88879$^{***}$ \\(1.0; 154.0)}} & {\makecell{25.39116$^{***}$\\ (4.0; 151.0)}} \\
\midrule[0.4pt]\bottomrule\addlinespace
\multicolumn{6}{@{}>{$}l<{$}}{\textit{Note:}\quad^{*} p<0.1;\enspace ^{**}p < 0.05;\enspace ^{***}p < 0.01} \\
\end{tabular}
\end{table}
\end{document}