14

我想使用esttab( ssc install estout) 按组生成汇总统计信息,其中包含平均差异和显着性的列。很容易将它们生成为两个单独的表,使用estpost,summarizettest, 并手动组合,但我想自动化整个过程。

以下代码生成所需表的两个组件。

sysuse auto, clear

* summary statistics by group
eststo clear
by foreign: eststo: quietly estpost summarize ///
    price mpg weight headroom trunk
esttab, cells("mean sd") label nodepvar   

* difference in means
eststo: estpost ttest price mpg weight headroom trunk, ///
    by(foreign) unequal 
esttab ., wide label   

我可以打印两张表并将其剪切并粘贴到一张表中。

* can generate similar tables and append horizontally
esttab, cells("mean sd") label
esttab, wide label


* manual, cut-and-paste solution
-------------------------------------------------------------------------------------------------------
                              (1)                       (2)                         (3)                

                             mean           sd         mean           sd         
-------------------------------------------------------------------------------------------------------
Price                    6072.423     3097.104     6384.682     2621.915         -312.3         (-0.44)
Mileage (mpg)            19.82692     4.743297     24.77273     6.611187         -4.946**       (-3.18)
Weight (lbs.)            3317.115     695.3637     2315.909     433.0035         1001.2***       (7.50)
Headroom (in.)           3.153846     .9157578     2.613636     .4862837          0.540**        (3.30)
Trunk space (.. ft.)        14.75     4.306288     11.40909     3.216906          3.341***       (3.67)
-------------------------------------------------------------------------------------------------------
Observations                   52                        22                          74                
-------------------------------------------------------------------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

似乎我应该能够通过一个esttab电话获得所需的表格,而无需剪切和粘贴,但我无法弄清楚。有没有办法在不手动剪切和粘贴的情况下生成所需的表格?

我更喜欢输出一个 LaTeX 表格,但是任何消除剪切和粘贴的东西都是一大步,即使是通过一个分隔的文本文件。

4

3 回答 3

13

如果您仍想使用 esttab,您可以使用单元格和模式进行尝试。可以使用以下代码复制原始帖子中的表:

sysuse auto, clear

eststo domestic: quietly estpost summarize ///
    price mpg weight headroom trunk if foreign == 0
eststo foreign: quietly estpost summarize ///
    price mpg weight headroom trunk if foreign == 1
eststo diff: quietly estpost ttest ///
    price mpg weight headroom trunk, by(foreign) unequal

esttab domestic foreign diff, ///
cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(2)) t(pattern(0 0 1) par fmt(2))") ///
label

产生

-----------------------------------------------------------------------------------------------------
                              (1)                       (2)                       (3)                

                             mean           sd         mean           sd            b               t
-----------------------------------------------------------------------------------------------------
Price                     6072.42      3097.10      6384.68      2621.92      -312.26         (-0.44)
Mileage (mpg)               19.83         4.74        24.77         6.61        -4.95**       (-3.18)
Weight (lbs.)             3317.12       695.36      2315.91       433.00      1001.21***       (7.50)
Headroom (in.)               3.15         0.92         2.61         0.49         0.54**        (3.30)
Trunk space (.. ft.)        14.75         4.31        11.41         3.22         3.34***       (3.67)
-----------------------------------------------------------------------------------------------------
Observations                   52                        22                        74                
-----------------------------------------------------------------------------------------------------
于 2016-07-08T12:44:33.353 回答
4

我不认为有办法用esttab( estoutpackage from ssc) 做到这一点,但我有一个listtab(also ssc) 和postfile. 此处的表格与我上面建议的表格略有不同,但该方法足够通用,您可以对其进行修改以满足您的需求。

该解决方案还使用 LaTeX 的booktabs包。

/* data and variables */
sysuse auto, clear
local vars price mpg weight headroom trunk

/* means */
tempname postMeans
tempfile means
postfile `postMeans' ///
    str100 varname domesticMeans foreignMeans pMeans using "`means'", replace
foreach v of local vars {
    local name: variable label `v'
    ttest `v', by(foreign)
    post `postMeans' ("`name'") (r(mu_1)) (r(mu_2)) (r(p))
}
postclose `postMeans'

/* medians */
tempname postMedians
tempfile medians
postfile `postMedians' ///
    domesticMedians foreignMedians pMedians using `medians', replace
foreach v of local vars {
    summarize `v' if !foreign, detail
    local med1 = r(p50)
    summarize `v' if foreign, detail
    local med2 = r(p50)
    ranksum `v', by(foreign)
    local pval = 2 * (1 - normal(abs(r(z))))
    post `postMedians' (`med1') (`med2') (`pval')
}
postclose `postMedians'

/* combine */
use `means'
merge 1:1 _n using `medians', nogenerate
format *Means *Medians %9.3gc
list

/* make latex table */
/* requires LaTeX package `booktabs` */
listtab * using "Table.tex", ///
    rstyle(tabular) replace ///
    head("\begin{tabular}{lcccccc}" ///
    "\toprule" ///
    "& \multicolumn{3}{c}{Means} & \multicolumn{3}{c}{Medians} \\" ///
    "\cmidrule(lr){2-4} \cmidrule(lr){5-7}" ///
    "& Domestic & Foreign & \emph{p} & Domestic & Foreign & \emph{p}\\" ///
    "\midrule") ///
    foot("\bottomrule" "\end{tabular}")

这产生以下结果。

在此处输入图像描述

于 2015-01-10T10:33:34.790 回答
3

选择的答案很好,但有点多余。您可以仅使用 estpost ttest 获得相同的结果。

sysuse auto, clear
estpost ttest price mpg weight headroom trunk, by(foreign)
esttab, cells("mu_1 mu_2 b(star)"

输出如下所示:

             mu_1          mu_2         b   

c_score     43.33858    42.034       1.30458***
nc_a4_17    4.007524    3.924623    .0829008*  
于 2018-02-22T16:43:35.067 回答