将多重回归输出到 LaTeX 文档中

Outputting multiple regressions into a LaTeX document

Outreg2 是一个社区贡献的命令,它可以帮助我们轻松地将 Stata 上的回归结果 运行 输出为干净的 table,然后可以在文本中查看, Word 文档,或 LaTeX。

使用 auto.dta 数据集,我 运行 以下回归:

sysuse auto.dta, clear
ssc install outreg2
gen  wtsq  = weight^2
foreach s in price headroom trunk{ 
    xi: reg `s' weight wtsq, vce(robust)
    outreg2 weight wtsq using tab_base_`s'_j, keep(weight wtsq) bdec(3) nocons tex(nopretty) replace
    xi: reg `s' weight wtsq foreign, vce(robust)
    outreg2 weight wtsq foreign using tab_base_`s'_j, keep(weight wtsq foreign) bdec(3) nocons tex(nopretty) append
    xi: reg `s' weight wtsq foreign length, vce(robust)
    outreg2 weight wtsq foreign length using tab_base_`s'_j, keep(weight wtsq foreign length) bdec(3) nocons tex(nopretty) append
} 

我输出了三个 .tex 文件,名称为 tab_base_price_jtab_base_trunk_j,依此类推。当我在 LaTeX 中打开 .tex 文件并 运行 它们时,我以完美的格式获得了 PDF 中的回归 tables,正如我想要的那样。 但是,LaTeX 中的每个文件都具有以下格式:

\documentclass[]{article}
\setlength{\pdfpagewidth}{8.5in} \setlength{\pdfpageheight}{11in}
\begin{document}
\begin{tabular}{lccc} \hline
 & (1) & (2) & (3) \
*** ALL THE TABLE VALUES - DELETED from this illustration ***
\end{tabular}
\end{document}

如果我想创建一个新文档(作为期刊文章或论文格式),并且我想输入这些 .tex 文件之一,使用 \input{tab_base_price_j.tex} 在 LaTeX 中, 我收到此错误:! LaTeX Error: Can be used only in preamble.

如何以输出 .tex 文件没有 \begin{document} 的方式从 Stata 输出回归 tables,并且只从:

开始
\begin{tabular}{lccc} \hline
 & (1) & (2) & (3) \
*** ALL THE TABLE VALUES - DELETED from this illustration ***
\end{tabular}

您只需要使用 tex(fragment) 选项:

sysuse auto.dta, clear
generate  wtsq  = weight^2

foreach s in price headroom trunk { 
    regress `s' weight wtsq, vce(robust)
    outreg2 weight wtsq using tab_base_`s'_j.tex, keep(weight wtsq) bdec(3) nocons tex(fragment)
    regress `s' weight wtsq foreign, vce(robust)
    outreg2 weight wtsq foreign using tab_base_`s'_j.tex, keep(weight wtsq foreign) bdec(3) nocons tex(fragment)
    regress `s' weight wtsq foreign length, vce(robust)
    outreg2 weight wtsq foreign length using tab_base_`s'_j.tex, keep(weight wtsq foreign length) bdec(3) nocons tex(fragment)
} 

然后您可以将这些作为更大文档的一部分输入,如下所示:

\documentclass[10pt]{article}
\begin{document}
... text before inclusion of table tab_base_price_j.tex ...
\input{tab_base_price_j.tex}
... text after inclusion of table tab_base_price_j.tex ...
\input{tab_base_headroom_j.tex}
... text after inclusion of table tab_base_headroom_j.tex ...
\input{tab_base_trunk_j.tex}
... text after inclusion of table tab_base_trunk_j.tex ...
\end{document}