汇总系数/ t统计量的组合矩阵并在LaTeX中导出

时间:2018-12-06 01:22:27

标签: matrix latex stata

我对四个结果变量进行了许多回归分析(每年一次,超过十五年)。

我创建了两个15x4矩阵:一个具有系数,另一个具有t统计量。现在我很困,我想用经典的方式将它们制成表格,每一行都有每个模型的系数,并且在它们下面的括号之间是各自的t统计量。

我可以将单个矩阵制成表格,但是即使使用estadd也无法获得上述结果。

这是我的代码:

*creates matrix of all results row(year) column(outcome)
matrix OLS_years = r_Mall, r_hotMall, r_totMall, r_irpMall
matrix list OLS_years
matrix TSTATS = ex_Tall, hot_Tall, tot_Tall, irp_Tall
matrix list TSTATS 

使用社区贡献的命令estout仅生成具有以下系数的表:

    estout matrix(OLS_years), ///
     cells(OLS_years TSTATS(par)) ///
     stats(N, fmt(0) labels ("No. of Obs." )) ///
     starlevels(* 0.1 ** 0.05 *** 0.001) ///
     varwidth(20) ///
     modelwidth(12) ///
     delimiter(&) ///
     end(\\) ///
     prehead(`"\begin{tabular}{l*{4}{c}}"' `"\toprule"') ///
     posthead("\midrule") ///
     prefoot("\midrule") ///
     postfoot(`"\bottomrule"' `"\multicolumn{@span}{l}{\footnotesize t-statistics in parentheses}\\"' `"\multicolumn{@span}{l}{\footnotesize *** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.}\\"' `"\end{tabular}"') ///
     varlabels(_cons Constant, end("" \addlinespace) nolast) ///
     eqlabels(, begin("\midrule" "") nofirst) ///
     substitute(_ \_ "\_cons " \_cons) ///
     interaction(" $\times$ ") ///
     notype ///
     level(95) ///
     style(esttab)

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

这是 不是 的方法,estout是如何用于导出回归的。

在这种情况下创建矩阵不仅是不必要的,而且是完全不好的做法。相反,您必须运行回归并使用estimates store 后估计命令保存结果。

以下是使用Stata的auto玩具数据集来正确执行此操作的方法:

sysuse auto.dta, clear
estimates clear

regress price gear_ratio length
estimates store A

regress mpg i.foreign trunk turn
estimates store B

regress displacement headroom length weight
estimates store C

esttab A B C

------------------------------------------------------------
                      (1)             (2)             (3)   
                    price             mpg    displacement   
------------------------------------------------------------
gear_ratio         -162.4                                   
                  (-0.17)                                   

length              54.88**                        -0.601   
                   (2.78)                         (-0.88)   

0.foreign                               0                   
                                      (.)                   

1.foreign                          -1.168                   
                                  (-0.91)                   

trunk                              -0.312*                  
                                  (-2.32)                   

turn                               -0.840***                
                                  (-5.34)                   

headroom                                            7.146   
                                                   (1.07)   

weight                                              0.118***
                                                   (6.15)   

_cons             -3659.7           59.26***       -68.23   
                  (-0.60)         (10.25)         (-0.92)   
------------------------------------------------------------
N                      74              74              74   
------------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

或者在LaTeX中:

esttab A B C using table.tex

type table.tex

{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{3}{c}}
\hline\hline
            &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}\\
            &\multicolumn{1}{c}{price}&\multicolumn{1}{c}{mpg}&\multicolumn{1}{c}{displacement}\\
\hline
gear\_ratio  &      -162.4         &                     &                     \\
            &     (-0.17)         &                     &                     \\
[1em]
length      &       54.88\sym{**} &                     &      -0.601         \\
            &      (2.78)         &                     &     (-0.88)         \\
[1em]
0.foreign   &                     &           0         &                     \\
            &                     &         (.)         &                     \\
[1em]
1.foreign   &                     &      -1.168         &                     \\
            &                     &     (-0.91)         &                     \\
[1em]
trunk       &                     &      -0.312\sym{*}  &                     \\
            &                     &     (-2.32)         &                     \\
[1em]
turn        &                     &      -0.840\sym{***}&                     \\
            &                     &     (-5.34)         &                     \\
[1em]
headroom    &                     &                     &       7.146         \\
            &                     &                     &      (1.07)         \\
[1em]
weight      &                     &                     &       0.118\sym{***}\\
            &                     &                     &      (6.15)         \\
[1em]
\_cons      &     -3659.7         &       59.26\sym{***}&      -68.23         \\
            &     (-0.60)         &     (10.25)         &     (-0.92)         \\
\hline
\(N\)       &          74         &          74         &          74         \\
\hline\hline
\multicolumn{4}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{4}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
}

答案 1 :(得分:1)

我的挑战是将@Pearly Spencer解释的方法应用于更复杂的设置,在该设置中,我必须将来自4种不同规格和15年的回归估算值堆叠在一个易于输入的Latex表中。我通过将-esttab-与-noisily-选项结合使用来解决此问题,这非常方便,因为它向您显示了其背后的estout命令(esttab是estout的包装器)。这样,我设法个性化了桌子的三个部分(顶部,中间,底部),以便可以很好地堆叠它们。我以一般方式发布代码,其中y1,y2,y3,y4是不同的结果变量,而T是治疗变量,您希望了解其对y的影响。

set more off

*-----------------FIRST YEAR I.E. TOP PART OF THE TABLE
eststo clear
    label var T "T 2002"

                qui reg Y1 T `x_prop' ///
                if year==2002 
                eststo y1_2002

                qui reg Y2 T `x_prop' ///
                if year==2002 
                eststo y2_2002

                qui reg Y3 T `x_prop' ///
                if year==2002 
                eststo y3_2002

                qui reg Y4 T `x_prop' ///
                if year==2002 
                eststo y4_2002

qui cap erase "C:\Users\...\tabz.tex"   // the replace option did not seem to work, so I used this way instead

estout using `"C:\Users\...\tabz.tex"' , ///
 cells(b(fmt(a3) star) t(fmt(2) par)) ///
 starlevels(* 0.1 ** 0.05 *** 0.001) ///
 varwidth(20) ///
 modelwidth(12) ///
 delimiter(&) ///
 end(\\) ///
 prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{4}{c}} \toprule  &\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}&\multicolumn{1}{c}{(8)}\\") ///
 posthead("\midrule") ///
 label ///
 varlabels(_cons Constant, end("" \addlinespace) nolast) ///
 mlabels(, depvar span prefix(\multicolumn{@span}{c}{) suffix(})) ///
 collabels(none) ///
 eqlabels(, begin("\midrule" "") nofirst) ///
 substitute(_ \_ "\_cons " \_cons) ///
 interaction(" $\times$ ") ///
 notype ///
 level(95) ///
 style(esttab) ///
 keep(T) 

 *-----------------OTHER YEARS I.E. MID PART OF THE TABLE
forval i=2003/2015 { 
eststo clear

    label var T "T `i'"
                qui reg Y1 T `x_prop' ///
                if year==`i'
                eststo y1_`i'

                qui reg Y2 T `x_prop' ///
                if year==`i'
                eststo y2_`i'

                qui reg Y3 T `x_prop' ///
                if year==`i'
                eststo y3_`i'

                qui reg Y4 T `x_prop' ///
                if year==`i'
                eststo y4_`i'

esttab using "C:\Users\...\tabz.tex\tabz.tex", keep(T) append f label nomtitle nonumber collabels(none) ///
starlevels(* 0.1 ** 0.05 *** 0.001) booktabs gaps noline ///
    notes addnotes("t-statistics in parentheses" "*** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.") ///
    noobs
}

 *-----------------OTHER YEARS I.E. BOTTOM PART OF THE TABLE

eststo clear

    label var T "T 2016"
                qui reg Y1 T `x_prop' ///
                if year==2016 & violat_2016==0
                eststo y1_2016

                qui reg Y2 T `x_prop' ///
                if year==2016 & violat_2016==0
                eststo y2_2016

                qui reg Y3 T `x_prop' ///
                if year==2016 & violat_2016==0
                eststo y3_2016

                qui reg Y4 T `x_prop' ///
                if year==2016 & violat_2016==0
                eststo y4_2016

esttab using "C:\Users\...\tabz.tex", keep(T) append f label nomtitle nonumber collabels(none) ///
starlevels(* 0.1 ** 0.05 *** 0.001) booktabs gaps noline ///
    notes addnotes("t-statistics in parentheses" "*** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.") ///
     prefoot("\midrule") ///
     postfoot("\bottomrule \multicolumn{5}{l}{\footnotesize t-statistics in parentheses}\\ \multicolumn{5}{l}{\footnotesize *** p{$<$}0.01; ** p{$<$}0.05; * p{$<$}0.10.}\\ \end{tabular}")

我希望这对其他人有用,因为我花了很长时间。

This was the result (bold text modified manually