Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Program to unstack matrix produced by -estpost tabulate twoway-

    Hi all,

    I wanted to share a small program I wrote to solve a problem I was having with -estpost tabulate- (part of the excellent and very useful -estout- package written by Ben Jann and available through SSC). I use -putexcel- to export most of my tables, and I like using -estpost tabulate-, as opposed to -tabulate-, for two way tables -- mainly because it stores row and column percentages in a matrix which I then can combine with other stored tabulations in a loop (this is also possible with the -matcell- option of tab, but it only stores counts).

    The problem I was having is that the way -estpost tabulate- stores two-way tables is in one long matrix, which is not typically what I want. See, for example:

    Code:
    use "http://www.stata-press.com/data/r14/nhanes2d.dta"
    estpost tab sex race
    mat list e(pct)
    which produces one long 1x12 matrix. -estout- has an unstack option, but there wasn't a straightforward way that I found to "unstack" the matrix itself so that it matched a typical two-way table output.

    To that end, I've written a little helper program to do so. Assuming that -estpost tabulate- was just run with two variables, it unstacks the matrices stored by that command, and prints their names for the user. Counts (or weighted counts) are stored in a matrix called count, row percentages in row, column percentages in col, and cell percentages in cell. These can then be combined with other matrices or output with -putexcel- as normal.

    It may be that this program is only useful to me, but I thought I would share it in case anyone else ever had a similar problem. It can be installed from my github page using the following command:

    Code:
    net install unstack, from("https://raw.githubusercontent.com/imaddowzimet/StataPrograms/master/")
    To use it, just run it after either -estpost tabulate- or -estpost svy: tabulate-. Some small examples follow.

    Code:
    use "http://www.stata-press.com/data/r14/nhanes2d.dta"
    estpost tab sex race
    unstack
    mat list row
    Code:
    estpost svy: tab sex race
    unstack
    mat list row
Working...
X