0
votes

I want to generate a table in Stata that contains means, differences and t-values for 4 different groups.

In particular, say you have 2x2 study design and want to display the mean and standard deviation of the outcome variable and add a further column that tests for the difference across treatment one and a further row that contains the difference and t-value across treatment two.

I have the following code:

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 

The output is:

----------------------------------------------------------------------------------------------------------------
                 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)   
----------------------------------------------------------------------------------------------------------------

As you can see this is a 7x3 table. Ideally I would want to have the elements in the last row in columns one and two and discard all columns after column three.

On a related note, I also wonder if I can suppress the summary statistics in the table header (I mean the mean/sd and b/t under the treatment names).

1

1 Answers

2
votes

You need to modify your code as follows:

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 

Result:

------------------------------------------------------
                 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)                
------------------------------------------------------

Or perhaps:

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)                
------------------------------------------------------

Or even:

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)