Stata 使用esttab按组生成汇总统计数据,其中包含均值差异和显著性列
我想使用Stata 使用esttab按组生成汇总统计数据,其中包含均值差异和显著性列,stata,Stata,我想使用esttab(ssc安装estout)按组生成汇总统计数据,并用列表示平均差异和显著性。使用estpost、summary和ttest将它们作为两个单独的表生成并手动组合是很容易的,但我想自动化整个过程 下面的代码生成所需表的两个组件 sysuse auto, clear * summary statistics by group eststo clear by foreign: eststo: quietly estpost summarize /// price mpg w
esttab
(ssc安装estout
)按组生成汇总统计数据,并用列表示平均差异和显著性。使用estpost
、summary
和ttest
将它们作为两个单独的表生成并手动组合是很容易的,但我想自动化整个过程
下面的代码生成所需表的两个组件
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,单元格(“平均sd”)标签
esttab,宽标签
*手动、剪切和粘贴解决方案
-------------------------------------------------------------------------------------------------------
(1) (2) (3)
平均标准差平均标准差
-------------------------------------------------------------------------------------------------------
价格6072.423 3097.104 6384.682 2621.915-312.3(-0.44)
英里数(mpg)19.82692 4.743297 24.77273 6.611187-4.946**(-3.18)
重量(磅)3317.115 695.3637 2315.909 433.0035 1001.2***(7.50)
净空(英寸)3.153846.9157578 2.613636.4862837 0.540**(3.30)
行李箱空间(…英尺)14.75 4.306288 11.40909 3.216906 3.341***(3.67)
-------------------------------------------------------------------------------------------------------
意见52 22 74
-------------------------------------------------------------------------------------------------------
括号中的t统计量
*p我认为没有办法用esttab
(estout
来自ssc
)的包来实现这一点,但我有一个解决方案,它是listtab
(也叫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}")
这将产生以下结果
如果您仍然想使用esttab,可以使用单元格和模式。可以使用以下代码复制原始post中的表:
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
-----------------------------------------------------------------------------------------------------
选择的答案很好,但有点多余。仅使用estpost测试即可获得相同的结果
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*
谢谢我还没有仔细查看单元格
选项。无论我在哪里使用esttab
,这都会派上用场。到处都是。Ben Jann应该因为esttab获得诺贝尔奖。太好了!所选答案的一个优点是可以包括estpost-ttest
未报告的时刻。