将面板数据汇总统计导出到 LaTeX

Export panel data summary statistics to LaTeX

我想导出使用 xtsum 命令生成的汇总统计数据:

webuse nlswork, clear
xtsum hours birth_yr


Variable         |      Mean   Std. Dev.       Min        Max |    Observations
-----------------+--------------------------------------------+----------------
hours    overall |  36.55956   9.869623          1        168 |     N =   28467
         between |             7.846585          1       83.5 |     n =    4710
         within  |             7.520712  -2.154726   130.0596 | T-bar = 6.04395
                 |                                            |
birth_yr overall |  48.08509   3.012837         41         54 |     N =   28534
         between |             3.051795         41         54 |     n =    4711
         within  |                    0   48.08509   48.08509 | T-bar = 6.05689

有没有办法在 Stata 中做到这一点?

您可以在下面找到一个实现,它使用 community-contributed 命令 esttab(输入 ssc install estout 进行下载)导出生成的(LaTeX)table.

首先定义程序xtsum2:

program define xtsum2, eclass

syntax varlist

foreach var of local varlist {
    xtsum `var'

    tempname mat_`var'
    matrix mat_`var' = J(3, 5, .)
    matrix mat_`var'[1,1] = (`r(mean)', `r(sd)', `r(min)', `r(max)', `r(N)')
    matrix mat_`var'[2,1] = (., `r(sd_b)', `r(min_b)', `r(max_b)', `r(n)')
    matrix mat_`var'[3,1] = (., `r(sd_w)', `r(min_w)', `r(max_w)', `r(Tbar)')
    matrix colnames mat_`var'= Mean "Std. Dev." Min Max "N/n/T-bar"
    matrix rownames mat_`var'= `var' " " " "

    local matall `matall' mat_`var'
    local obw `obw' overall between within
}

if `= wordcount("`varlist'")' > 1 {
    local matall = subinstr("`matall'", " ", " \ ",.)
    matrix allmat = (`matall')
    ereturn matrix mat_all = allmat
}
else ereturn matrix mat_all = mat_`varlist'
ereturn local obw = "`obw'"

end

然后您可以 运行 xtsum2 并使用 esttab:

获得结果
xtsum2 hours birth_yr
esttab e(mat_all), mlabels(none) labcol2(`e(obw)') varlabels(r2 " " r3 " ")

------------------------------------------------------------------------------------------
                                  Mean    Std. Dev.          Min          Max    N/n/T-bar
------------------------------------------------------------------------------------------
hours          overall        36.55956     9.869623            1          168        28467
               between               .     7.846585            1         83.5         4710
                within               .     7.520712    -2.154726     130.0596     6.043949
birth_yr       overall        48.08509     3.012837           41           54        28534
               between               .     3.051795           41           54         4711
                within               .            0     48.08509     48.08509     6.056888
------------------------------------------------------------------------------------------

对于LaTeX输出,只需添加tex选项:

esttab e(mat_all), mlabels(none) labcol2(`e(obw)') varlabels(r2 " " r3 " ") tex

\begin{tabular}{lc*{5}{c}}
\hline\hline
            &            &        Mean&   Std. Dev.&         Min&         Max&   N/n/T-bar\
\hline
hours       &  overall   &    36.55956&    9.869623&           1&         168&       28467\
            &  between   &           .&    7.846585&           1&        83.5&        4710\
            &   within   &           .&    7.520712&   -2.154726&    130.0596&    6.043949\
birth\_yr    &  overall   &    48.08509&    3.012837&          41&          54&       28534\
            &  between   &           .&    3.051795&          41&          54&        4711\
            &   within   &           .&           0&    48.08509&    48.08509&    6.056888\
\hline\hline
\end{tabular}