Announcement

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

  • Adding number of observations to table of margins results using collect

    This is I'm afraid one of many variants of this type of question about the table collect commands on the list but I can't find a solution that works in my case.

    I am trying to add the number of observations to a table which collects margins results.
    I followed the instructions in the first example in the "Remarks and Examples" section of the manual collect addtags entry https://www.stata.com/manuals/tablescollectaddtags.pdf

    but although the table shows all the results I want, it does not add the number of observations as the last row.
    Here is a toy example

    Code:
    clear
    sysuse auto
    gen highqual=rep78>3
    regress mpg i.foreign i.highqual
    
    collect clear
    collect _r_b _r_ci,tags(rowheader["Group means mpg"]): margins foreign
    collect _r_b _r_ci,tags(rowheader["Difference (mpg)"]): margins r.foreign
    collect _r_b _r_ci,tags(rowheader["Group means quality"]): margins highqual
    collect _r_b _r_ci,tags(rowheader["Difference (quality)"]): margins highqual
    
    collect addtag extra[N], fortags(result[N])
    collect recode result N=_r_b
    collect label levels extra N "Observations"
    
    collect layout (rowheader#colname extra)  (result[_r_b _r_ci]) 
    collect style cell, nformat(%5.2f)
    collect style cell result[_r_ci], sformat("[%s]") cidelimiter(", ")
    collect style cell result[_r_b], halign(center)
    collect label levels colname 1.highqual "high quality" 0.highqual "low quality" r1vs0.highqual "High vs Low" 
    collect label levels result _r_b "mpg",modify
    
    collect preview

  • #2
    Perhaps this?
    Code:
    clear
    sysuse auto
    gen highqual=rep78>3
    regress mpg i.foreign i.highqual
    
    collect clear
    collect _r_b _r_ci, tags(rowheader["Group means mpg"]): margins foreign
    collect _r_b _r_ci, tags(rowheader["Difference (mpg)"]): margins r.foreign
    collect _r_b _r_ci, tags(rowheader["Group means quality"]): margins highqual
    collect _r_b _r_ci, tags(rowheader["Difference (quality)"]): margins highqual
    collect addtag colname[N], fortags(result[N]) replace
    collect label levels colname N "Observations"
    collect style cell, nformat(%5.2f)
    collect style cell result[_r_ci], sformat("[%s]") cidelimiter(", ")
    collect style cell result[_r_b], halign(center)
    collect label levels colname 1.highqual "high quality" 0.highqual "low quality" r1vs0.highqual "High vs Low" 
    collect recode result N=_r_b
    collect style cell colname[N]#result[_r_b], nformat(%5.0f)
    collect layout (rowheader#colname)  (result[_r_b _r_ci])
    collect label levels result _r_b "mpg",modify
    which produces:
    Code:
    . collect preview
    
    ----------------------------------------------
                            |  mpg      95% CI    
    ------------------------+---------------------
    Group means mpg         |                     
      Domestic              | 20.21 [18.66, 21.76]
      Foreign               | 23.86 [21.32, 26.40]
      Observations          |   74                
    Difference (mpg)        |                     
      (Foreign vs Domestic) |  3.65  [0.48,  6.82]
      Observations          |   74                
    Group means quality     |                     
      low quality           | 20.26 [18.45, 22.08]
      high quality          | 22.52 [20.52, 24.51]
      Observations          |   74                
    Difference (quality)    |                     
      low quality           | 20.26 [18.45, 22.08]
      high quality          | 22.52 [20.52, 24.51]
      Observations          |   74                
    ----------------------------------------------

    Comment


    • #3
      I had gotten that far as well, although with different code, but of course I wanted to only have one row with the number of observations at the bottom as all the results come from a single regression and and sample with N observations.

      Comment


      • #4
        Code:
        clear
        sysuse auto
        gen highqual=rep78>3
        regress mpg i.foreign i.highqual
        
        collect clear
        collect _r_b _r_ci, tags(rowheader["Group means mpg"]): margins foreign
        collect _r_b _r_ci, tags(rowheader["Difference (mpg)"]): margins r.foreign
        collect _r_b _r_ci, tags(rowheader["Group means quality"]): margins highqual
        collect _r_b _r_ci, tags(rowheader["Difference (quality)"]): margins highqual
        collect addtag rowheader["Observations"] colname[N], fortags(rowheader["Difference (quality)"]#result[N]) replace
        collect style cell, nformat(%5.2f)
        collect style cell result[_r_ci], sformat("[%s]") cidelimiter(", ")
        collect style cell result[_r_b], halign(center)
        collect label levels colname 1.highqual "high quality" 0.highqual "low quality" r1vs0.highqual "High vs Low" 
        collect recode result N=_r_b
        collect style cell rowheader["Observations"]#colname[N]#result[_r_b], nformat(%5.0f)
        collect style header colname[N], level(hide)
        collect layout (rowheader#colname)  (result[_r_b _r_ci])
        collect label levels result _r_b "mpg",modify
        which produces:
        Code:
        . collect preview
        
        ----------------------------------------------
                                |  mpg      95% CI    
        ------------------------+---------------------
        Group means mpg         |                     
          Domestic              | 20.21 [18.66, 21.76]
          Foreign               | 23.86 [21.32, 26.40]
        Difference (mpg)        |                     
          (Foreign vs Domestic) |  3.65  [0.48,  6.82]
        Group means quality     |                     
          low quality           | 20.26 [18.45, 22.08]
          high quality          | 22.52 [20.52, 24.51]
        Difference (quality)    |                     
          low quality           | 20.26 [18.45, 22.08]
          high quality          | 22.52 [20.52, 24.51]
        Observations            |   74                
        ----------------------------------------------

        Comment


        • #5
          Wow! thanks. I have to admit I don't find this table language intuitive yet. (and I'm a user who started with Stata 2.1)

          Comment

          Working...
          X