user319487
user319487

Reputation:

How can I 'align' multiple two-way tabulations in esttab?

I am trying to prepare a table that will display two-way frequency tables of several variables. The logic is that each of the variables will be tabulated by the same binary indicator.

I would like to send the output to a tex file using the estout community-contributed family of commands. However, each cross-tabulation appears in new column.

Consider the following reproducible toy example:

sysuse auto
eststo clear 

eststo: estpost tab headroom foreign, notot
eststo: estpost tab trunk foreign, notot

esttab, c(b) unstack wide collabels(N)

----------------------------------------------------------------
                      (1)                       (2)             

                 Domestic      Foreign     Domestic      Foreign
                        N            N            N            N
----------------------------------------------------------------
1_missing_5             3            1                          
2                      10            3                          
2_missing_5             4           10                          
3                       7            6                          
3_missing_5            13            2                          
4                      10            0                          
4_missing_5             4            0                          
5                       1            0            0            1
6                                                 0            1
7                                                 3            0
8                                                 2            3
9                                                 3            1
10                                                3            2
11                                                4            4
12                                                1            2
13                                                4            0
14                                                1            3
15                                                2            3
16                                               10            2
17                                                8            0
18                                                1            0
20                                                6            0
21                                                2            0
22                                                1            0
23                                                1            0
----------------------------------------------------------------
N                      74                        74             
----------------------------------------------------------------

Is there a way to 'align' the output so that there are only two Domestic and Foreign columns?

Upvotes: 0

Views: 2501

Answers (2)

user8682794
user8682794

Reputation:

Producing the desired output requires that you stack the results together.

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

program append_tabs, eclass
    version 8
    syntax namelist
    tempname b tmp
    local i 0
    foreach name of local namelist {
        qui est restore `name'
        foreach x in Domestic Foreign {
            local ++i
            mat `tmp'`i' = e(b)
            mat li `tmp'`i'
            mat `tmp'`i' = `tmp'`i'[1,"`x':"]
            local cons = colnumb(`tmp'`i',"_cons")
            if `cons'<. & `cons'>1 {
                mat `tmp'`i' = `tmp'`i'[1,1..`cons'-1]
            }
            mat li `tmp'`i'
            mat `b'`i' = `tmp'`i''
            mat li `b'`i'    
        }
    }
    mat `b'D = `b'1 \ `b'3
    mat `b'F = `b'2 \ `b'4
    mat A = `b'D , `b'F
    ereturn matrix results = A
    eret local cmd "append_tabs"
end

Next run your tabulations and stack their results using append_tabs:

sysuse auto, clear
estimates store clear

estpost tabulate headroom foreign, notot
estimates store one 

estpost tabulate trunk foreign, notot
estimates store two

append_tabs one two

Finally, see the results:

esttab e(results), nonumber mlabels(none) eqlabels(none) collabels("Domestic" "Foreign")

--------------------------------------
                 Domestic      Foreign
--------------------------------------
1_missing_5             3            1
2                      10            3
2_missing_5             4           10
3                       7            6
3_missing_5            13            2
4                      10            0
4_missing_5             4            0
5                       1            0
5                       0            1
6                       0            1
7                       3            0
8                       2            3
9                       3            1
10                      3            2
11                      4            4
12                      1            2
13                      4            0
14                      1            3
15                      2            3
16                     10            2
17                      8            0
18                      1            0
20                      6            0
21                      2            0
22                      1            0
23                      1            0
--------------------------------------

Use the tex option in the esttab command to see the LaTeX output.

Upvotes: 1

maxliving
maxliving

Reputation: 110

If you're outputting to a tex file, one solution would be to use the append option of esttab. So in your case it would be something like:

sysuse auto
eststo clear 
estpost tab headroom foreign, notot
eststo tab1
estpost tab trunk foreign, notot
eststo tab2
esttab tab1 using outputfile.tex, c(b) unstack wide collabels(N) replace
esttab tab2 using outputfile.tex, c(b) unstack wide collabels(N) append

I believe there may be a more elegant solution as well, but this is generally pretty easy to implement. When appending, you'll likely have to specify a bunch of options to remove the various column headers (I believe estout's default assumes you don't want most of those headers, so it may be worth looking into estout instead of esttab).

Upvotes: 1

Related Questions