Announcement

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

  • Using -table- and -dtable-

    I would like to make a table along multiple column variables (e.g. gender and wave / foreign and somecategory). I would also like to have a categorical variable "header" for cohort / Repair Record 1978, which is the variable label.

    HTML Code:
    sysuse auto, clear
    generate u1 = runiform()
    sort u1
    gen somecategory = (_n<30)
    dtable mpg i.rep78, by(foreign)
    Click image for larger version

Name:	Screenshot 2023-11-14 103551.png
Views:	1
Size:	10.7 KB
ID:	1733818


    does this. However, it seems that dtable only allows for one column variable (I cannot add 'somecategory'). Is this true?

    An option using -table- is
    HTML Code:
    table (var) (foreign somecategory)   , stat(mean  mpg i.rep78) stat(sd  mpg) nototals
    But this keeps mean and sd on separate rows. I cannot get sd next to the means in a compact manner, as in -dtable-. Additionally, this option does not add the header "Repair Record 1978" that groups all values of the categorical variable rep78, as in -dtable- above.

  • #2
    Hallo Cator,

    you could use "collect layout" instead.

    Code:
    sysuse auto, clear
    generate u1 = runiform()
    sort u1
    gen somecategory = (_n<30)
    dtable mpg i.rep78, by(foreign)
    
    
    table (var) (foreign), ///
    statistic(fvfrequency rep78) statistic(fvpercent rep78) ///
    statistic(mean mpg) statistic(sd mpg) ///
    nformat(%6.2f mean sd)
    
    collect style header result, level(hide)
    collect style row stack, nobinder spacer
    
    collect recode result fvfrequency=mean fvpercent=sd
    
    collect style cell result[sd]#var[rep78], sformat("%s%%")
    collect style cell result[sd]#var[mpg], sformat("(%s)")
    
    collect layout (var) (foreign#result)
    Code:
    ----------------------------------------------------------------------
                       |                     Car origin                   
                       |     Domestic          Foreign           Total    
    -------------------+--------------------------------------------------
    Repair record 1978 |                                                  
      1                |   2.00    4.17%    0.00    0.00%    2.00    2.90%
      2                |   8.00   16.67%    0.00    0.00%    8.00   11.59%
      3                |  27.00   56.25%    3.00   14.29%   30.00   43.48%
      4                |   9.00   18.75%    9.00   42.86%   18.00   26.09%
      5                |   2.00    4.17%    9.00   42.86%   11.00   15.94%
                       |                                                  
    Mileage (mpg)      |  19.83   (4.74)   24.77   (6.61)   21.30   (5.79)
    ----------------------------------------------------------------------

    Comment


    • #3
      Here is a solution that borrows ideas from dtable.

      Code:
      set seed 18
      sysuse auto, clear
      generate u1 = runiform()
      sort u1
      gen somecategory = (_n<30)
      dtable mpg i.rep78, by(foreign)
      
      * use -table- to compute statistics and arrange the basic layout;
      * use -style(dtable)- to get its styles: numeric formats,
      * parentheses, and percent signs
      table (var) (foreign somecategory result) , ///
          style(dtable) ///
          stat(mean mpg) ///
          stat(sd mpg) ///
          stat(fvfrequency rep78) ///
          stat(fvpercent rep78) ///
          nototals
      
      * use composite results to assign statistics to columns
      collect composite define col1 = mean fvfrequency
      collect composite define col2 = sd fvpercent
      collect style autolevels result col1 col2, clear
      
      * hide the result labels
      collect style header result, title(hide) level(hide)
      * show labels for the column variables
      collect style header foreign somecategory, title(label)
      
      * replay the current layout so you can see the specification with the
      * final table
      collect layout
      Here is the resulting table.
      Code:
      ------------------------------------------------------------------------------
                                                  Car origin
                                    Domestic                      Foreign
                                  somecategory                  somecategory
                                0              1              0              1
      ------------------------------------------------------------------------------
      Mileage (mpg)      19.312 (4.768) 20.650 (4.705) 23.385 (6.384) 26.778 (6.778)
      Repair record 1978
        1                     2  (6.7%)      0  (0.0%)      0  (0.0%)      0  (0.0%)
        2                     5 (16.7%)      3 (16.7%)      0  (0.0%)      0  (0.0%)
        3                    16 (53.3%)     11 (61.1%)      1  (8.3%)      2 (22.2%)
        4                     5 (16.7%)      4 (22.2%)      5 (41.7%)      4 (44.4%)
        5                     2  (6.7%)      0  (0.0%)      6 (50.0%)      3 (33.3%)
      ------------------------------------------------------------------------------

      Comment


      • #4
        #3 works. It makes the necessary code long. Adding this feature to -dtable- in Stata 19 could be considered.

        Comment

        Working...
        X