我想在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/sd和b/t )。
发布于 2019-01-12 23:36:30
您需要修改代码,如下所示:
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) https://stackoverflow.com/questions/54160564
复制相似问题