4
votes

I am running a ttest command and exporting results to LaTeX using estpost and the community-contributed command esttab.

I am testing for a difference for means (of variable height, by child gender) for several years and would like the years to be displayed vertically (in rows) rather than horizontally.

My code and is given below:

foreach i in 2009 2010 2013 {
    use "`i'.dta", clear
    global year `i'
    eststo _$year : estpost ttest height, by(child_gender)
}

esttab . using "trends.tex", nonumber append

Data for 2009:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 156
1 135
0 189
1 168
0 157
1 189
1 135
1 145
0 124
1 139
end

Data for 2010:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 151
1 162
0 157
1 134
0 157
1 189
1 135
1 145
0 143
1 166
end

Data for 2013:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 177
0 135
0 189
0 168
0 157
1 189
1 135
1 145
1 124
1 127
end

I would like the output arranged as follows (but in LaTeX):

enter image description here

Any suggestions on how to make this work?

1

1 Answers

1
votes

The way to do this can be found below. You need to play with the options to further polish the table.

First define the program append_ttests, which is a quickly modified version of appendmodels, Ben Jann's program for stacking models:

program append_ttests, eclass
    version 8
    syntax namelist
    tempname b V tmp
    foreach name of local namelist {
        qui est restore `name'
        mat `tmp' = e(b)
        local eq1: coleq `tmp'
        gettoken eq1 : eq1
        mat `tmp' = `tmp'[1,"`eq1':"]
        local cons = colnumb(`tmp',"_cons")
        if `cons'<. & `cons'>1 {
            mat `tmp' = `tmp'[1,1..`cons'-1]
        }
        mat `b' = nullmat(`b') , `tmp'

        mat `tmp' = e(t)
        mat `tmp' = `tmp'["`eq1':","`eq1':"]
        if `cons'<. & `cons'>1 {
            mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
        }
        capt confirm matrix `V'
        if _rc {
            mat `V' = `tmp'
        }
        else {
            mat `V' = ///
            ( `V'  \ ///
             `tmp' )
        } 
    }
    mat `b' = `b''
    mat A = `b' , `V'
    mat rown A = `0'
    ereturn matrix results = A
    eret local cmd "append_ttests"
end

Then run your loop and append the t-tests:

foreach i in 2009 2010 2013 {
    use "`i'.dta", clear
    estpost ttest height, by(child_gender)
    estimates store year`i' 
 }

append_ttests year2009 year2010 year2013

See the results as follows:

esttab e(results), nonumber mlabels(none) ///
                   varlabels(year2009 2009 year2010 2010 year2013 2013) ///
                   collabels("Height" "t statistic")

--------------------------------------
                   Height  t statistic
--------------------------------------
2009             4.666667     .3036859
2010            -3.166667    -.2833041
2013                 21.2     1.415095
--------------------------------------

Add the tex option to see the LaTeX output.