使用esttab创建具有可变均值和差异的表

时间:2019-01-12 14:34:44

标签: stata

我想在Stata中生成一个表,其中包含4个不同组的均值,差异和t值。

特别是,假设您有2x2研究设计,并且想要显示结果变量的均值和标准差,并添加另一列以测试治疗方案之间的差异,并添加另一行以包含差异和t值贯穿治疗二。

我有以下代码:

clear
sysuse auto
gen large_trunk = (trunk > 14)
gen price_large_trunk = price if large_trunk == 1
gen price_small_trunk = price if large_trunk == 0

eststo price_domestic: qui estpost sum price_large_trunk price_small_trunk if foreign == 0
eststo price_foreign: qui estpost sum price_large_trunk price_small_trunk if foreign == 1
eststo diff: qui estpost ttest price_large_trunk price_small_trunk, by(foreign)
eststo diff2: qui estpost ttest price if foreign == 0, by(large_trunk)
eststo diff3: qui estpost ttest price if foreign == 1, by(large_trunk)

esttab price_domestic price_foreign diff diff2 diff3, ///
cells("mean(pattern(1 1 0) fmt(2)) b(star pattern(0 0 1) fmt(2))" "sd(pattern(1 1 0) par)  t(pattern(0 0 1) par)") ///
mtitle("Domestic" "Foreign" "Difference") ///
nonumbers  noobs ///
coeflabels(price "Difference") ///
notes 

输出为:

----------------------------------------------------------------------------------------------------------------
                 Domestic      Foreign   Difference           diff2                        diff3                
                  mean/sd      mean/sd          b/t         mean/sd          b/t         mean/sd          b/t   
----------------------------------------------------------------------------------------------------------------
price_larg~k      6900.13      6186.00       714.13                                                             
                (3164.76)    (2186.93)       (0.48)                                                             
price_smal~k      4850.57      6443.12     -1592.55                                                             
                (2608.98)    (2794.83)      (-1.81)                                                             
Difference                                                              -2049.56*                      257.12   
                                                                         (-2.45)                       (0.19)   
----------------------------------------------------------------------------------------------------------------

如您所见,这是一个7x3表。理想情况下,我希望将元素放在第一和第二列的最后一行中,并丢弃第三列之后的所有列。

在相关说明中,我还想知道是否可以抑制表标题中的摘要统计信息(我是指处理名称下的mean/sdb/t)。

1 个答案:

答案 0 :(得分:2)

您需要按以下步骤修改代码:

sysuse auto, clear
est clear
gen large_trunk = (trunk > 14)
gen price_large_trunk = price if large_trunk == 1
gen price_small_trunk = price if large_trunk == 0

qui estpost sum price_large_trunk price_small_trunk if foreign == 0
qui ttest price if foreign == 0, by(large_trunk)
estadd local diff2  `= round(r(mu_1) - r(mu_2), .01)'
estadd local tdiff2 (`= round(`r(t)', .01)')
eststo price_domestic

qui estpost sum price_large_trunk price_small_trunk if foreign == 1
qui ttest price if foreign == 1, by(large_trunk)
estadd local diff2 `= round(r(mu_1) - r(mu_2), .01)'
estadd local tdiff2 (`= round(`r(t)', .01)')
eststo price_foreign

eststo diff1: qui estpost ttest price_large_trunk price_small_trunk, by(foreign)

esttab price_domestic price_foreign diff1, ///
stats(diff2 tdiff2, label("Difference" " ") ) ///
cells("mean(pattern(1 1 0) fmt(2)) b(star pattern(0 0 1) fmt(2))" "sd(pattern(1 1 0) par)  t(pattern(0 0 1) par)") ///
mtitle("Domestic" "Foreign" "Difference") collabels(none) ///
nonumbers  noobs ///
coeflabels(price "Difference") ///
notes 

结果:

------------------------------------------------------
                 Domestic      Foreign   Difference   
------------------------------------------------------
price_larg~k      6900.13      6186.00       714.13   
                (3164.76)    (2186.93)       (0.48)   
price_smal~k      4850.57      6443.12     -1592.55   
                (2608.98)    (2794.83)      (-1.81)   
------------------------------------------------------
Difference       -2049.56       257.12                
                  (-2.45)        (.19)                
------------------------------------------------------

或者也许:

esttab price_domestic price_foreign diff1, ///
stats(diff2 tdiff2, label("Difference" " ") ) ///
cells("mean(pattern(1 1 0) fmt(2)) b(star pattern(0 0 1) fmt(2))" "sd(pattern(1 1 0) par)  t(pattern(0 0 1) par)") ///
mtitle("Domestic" "Foreign" "Difference") collabels(none) ///
nonumbers  noobs ///
coeflabels(price "Difference") ///
notes gaps prefoot(" ")

------------------------------------------------------
                 Domestic      Foreign   Difference   
------------------------------------------------------
price_larg~k      6900.13      6186.00       714.13   
                (3164.76)    (2186.93)       (0.48)   

price_smal~k      4850.57      6443.12     -1592.55   
                (2608.98)    (2794.83)      (-1.81)   

Difference       -2049.56       257.12                
                  (-2.45)        (.19)                
------------------------------------------------------

甚至:

esttab price_domestic price_foreign diff1, ///
stats(diff2 tdiff2, label("Difference" " ") ) ///
cells("mean(pattern(1 1 0) fmt(2)) b(star pattern(0 0 1) fmt(2))" "sd(pattern(1 1 0) par)  t(pattern(0 0 1) par)") ///
mtitle("Domestic" "Foreign" "Difference") collabels(none) ///
nonumbers  noobs ///
coeflabels(price "Difference") ///
notes gaps plain 

                 Domestic      Foreign   Difference   

price_larg~k      6900.13      6186.00       714.13   
                (3164.76)    (2186.93)       (0.48)   

price_smal~k      4850.57      6443.12     -1592.55   
                (2608.98)    (2794.83)      (-1.81)   

Difference       -2049.56       257.12                
                  (-2.45)        (.19)