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

时间:2019-04-15 16:02:54

标签: latex stata stata-macros

Outreg2是一个社区贡献的命令,可以帮助我们轻松地将在Stata上运行的回归结果输出到一个干净的表中,然后可以在文本,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
} 

我得到三个名为.textab_base_price_j等的三个tab_base_trunk_j文件。当我在LaTeX中打开.tex文件并运行它们时,就可以根据需要以完美的格式获取PDF回归表。 但是,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.

我如何从Stata输出回归表,以使输出.tex文件没有\begin{document},而是以以下内容开头:

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

1 个答案:

答案 0 :(得分:4)

您只需要使用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}