Announcement

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

  • Row labels for an esttab/estout table with many descriptive statistics per column

    I want to produce and export (to Latex) a table of summary statistics for one variable, where the summary statistics are reported in different rows, while different populations of interest are considered in each column. I am using `esttab` or `estout` together with `estpost` from SSC. This seems very simple but I have been working at this for hours, and cannot figure out how to get `esttab` or `estout` to label the rows with the name of the statistic.

    Minimum working example:

    Code:
    sysuse auto, clear
    eststo testme1: estpost tabstat price if foreign==0, s(mean cv min p1 p5 p50 p95 p99 max count) columns(statistics)
    eststo testme2: estpost tabstat price if foreign==1, s(mean cv min p1 p5 p50 p95 p99 max count) columns(statistics)
    
    esttab testme1 testme2, cells(mean cv min p1 p5 p50 p95 p99 max count) noobs mtitles(column1 column2) nonumbers
    Code:
    --------------------------------------
                      column1      column2
                 mean/cv/mi~a mean/cv/mi~a
    --------------------------------------
    price            6072.423     6384.682
                     .5100278     .4106571
                         3291         3748
                         3291         3748
                         3667         3798
                       4782.5         5759
                        13594        11995
                        15906        12990
                        15906        12990
                           52           22
    --------------------------------------
    I want the first column, which currently just has "price", to have "mean cv min p1 p5 p50 p95 p99 max count" each in separate rows, instead of reporting these labels in the column header with slashes dividing them.

    I have tried playing with varlabels(), main(), aux(), coeflabels(), labcol2(), using summarize rather than tabstat... can anyone assist? It seems this should be very simple and I must be missing something. If it's too difficult with `esttab` or `estout`, is there another command for exporting descriptive stats of this kind to Latex which people could recommend?

  • #2
    Just to add: I should have mentioned that I'm using Stata 18. So if it's easy to do this using `table` and `collect`, I'd be very happy to try that. Currently looking at the 350 page manual...

    Comment


    • #3
      #5 of https://www.statalist.org/forums/for...=1747257935520 illustrates some technique with estout. In your example, it's a matter of iterating the code.

      Code:
      sysuse auto, clear
      clear matrix
      
      eststo m1: estpost tabstat price if foreign==0, s(mean cv min p1 p5 p50 p95 p99 max count) columns(statistics)
      foreach stat in mean cv min p1 p5 p50 p95 p99 max count{
          mat m1= nullmat(m1) \ e(`stat')
      }
      estadd mat res=m1': m1
      
      eststo m2: estpost tabstat price if foreign==1, s(mean cv min p1 p5 p50 p95 p99 max count) columns(statistics)
      foreach stat in mean cv min p1 p5 p50 p95 p99 max count{
          mat m2= nullmat(m2) \ e(`stat')
      }
      estadd mat res=m2': m2
      
      esttab m1 m2, cells(res) mlab(Domestic Foreign) noobs collab(none)
      Res.:

      Code:
      . esttab m1 m2, cells(res) mlab(Domestic Foreign) noobs collab(none)
      
      --------------------------------------
                            (1)          (2)
                       Domestic      Foreign
      --------------------------------------
      mean             6072.423     6384.682
      cv               .5100278     .4106571
      min                  3291         3748
      p1                   3291         3748
      p5                   3667         3798
      p50                4782.5         5759
      p95                 13594        11995
      p99                 15906        12990
      max                 15906        12990
      count                  52           22
      --------------------------------------

      Comment


      • #4
        With the official table and collect:

        Code:
        sysuse auto, clear
        local opts
        foreach stat in mean cv min p1 p5 p50 p95 p99 max count{
            local opts "`opts' stat(`stat' price)"
        }
        qui table foreign, `opts' nformat(%5.0f) nototal
        collect style cell result[cv], nformat(%3.2f)
        collect layout (result) (foreign)
        Res.:

        Code:
        . collect layout (result) (foreign)
        
        Collection: Table
              Rows: result
           Columns: foreign
           Table 1: 10 x 2
        
        -------------------------------------------------
                                    |      Car origin    
                                    |  Domestic   Foreign
        ----------------------------+--------------------
        Mean                        |      6072      6385
        Coefficient of variation    |      0.51      0.41
        Minimum value               |      3291      3748
        1st percentile              |      3291      3748
        5th percentile              |      3667      3798
        50th percentile             |      4782      5759
        95th percentile             |     13594     11995
        99th percentile             |     15906     12990
        Maximum value               |     15906     12990
        Number of nonmissing values |        52        22
        -------------------------------------------------
        Last edited by Andrew Musau; 14 May 2025, 16:05.

        Comment


        • #5
          Andrew you are once again a lifesaver -- thank you!

          I have been playing around with table and collect and did just get to the solution in your second post, which I just saw just now. I'm glad I had to read some of the manual -- it seems like game-changer to me, much simpler and more flexible than I imagined (though I have not yet tried anything complex), and worth the time investment to learn.

          Comment

          Working...
          X