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

问题描述 投票:2回答:1

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
} 

我得到三个.tex文件,名为tab_base_price_jtab_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}

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

如何以输出.tex文件没有\begin{document}的方式从Stata输出回归表,并从以下开始:

\begin{tabular}{lccc} \hline
 & (1) & (2) & (3) \\
*** ALL THE TABLE VALUES - DELETED from this illustration ***
\end{tabular}
latex stata stata-macros
1个回答
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}
© www.soinside.com 2019 - 2024. All rights reserved.