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

将多元回归输出到LaTeX文档中,latex,stata,stata-macros,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) out

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
} 
我得到三个名为
tab\u base\u price\u j
tab\u base\u trunk\u 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}
如果我想创建一个新文档(作为期刊文章或纸张格式),并且我想使用
\input{tab_base_price_j.tex}
在LaTeX中, 我收到此错误:
!LaTeX错误:只能在前言中使用。

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

\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}

在Stata的最新版本中,
xi:
前缀被弃用,取而代之的是因子变量表示法。在您的示例中,它没有害处,但毫无意义,因为没有变量被标记为分类变量(在其他术语中是因子变量)。更重要的是,您应该将
outreg2
解释为SSC贡献的社区。