Announcement

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

  • Percentages in table (Stata 18) and the across option

    I'm confused about how table handles the "across" option in percent across rows. The simplest example I can show is using the auto.dta data where the "across" percentages differ from the direct calculation from the frequencies. At first I thought this might be due to some case/listwise deletion, but with no missing data in the auto data, I'm perplexed as to why I get differing numbers from the two approaches.
    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . table mpg foreign, stat(percent weight, across(foreign)) zerocounts

    --------------------------------------------
    | Car origin
    | Domestic Foreign Total
    --------------+-----------------------------
    Mileage (mpg) |
    12 | 100.00 100.00
    14 | 85.55 14.45 100.00
    15 | 100.00 100.00
    16 | 100.00 100.00
    17 | 54.16 45.84 100.00
    18 | 83.02 16.98 100.00
    19 | 100.00 100.00
    20 | 100.00 100.00
    21 | 69.27 30.73 100.00
    22 | 100.00 100.00
    23 | 100.00 100.00
    24 | 78.18 21.82 100.00
    25 | 19.98 80.02 100.00
    26 | 72.19 27.81 100.00
    28 | 74.19 25.81 100.00
    29 | 100.00 100.00
    30 | 51.71 48.29 100.00
    31 | 100.00 100.00
    34 | 100.00 100.00
    35 | 100.00 100.00
    41 | 100.00 100.00
    Total | 77.20 22.80 100.00
    --------------------------------------------


    Code:
    . table mpg foreign, stat(count weight) zerocounts

    -------------------------------------------
    | Car origin
    | Domestic Foreign Total
    --------------+----------------------------
    Mileage (mpg) |
    12 | 2 0 2
    14 | 5 1 6
    15 | 2 0 2
    16 | 4 0 4
    17 | 2 2 4
    18 | 7 2 9
    19 | 8 0 8
    20 | 3 0 3
    21 | 3 2 5
    22 | 5 0 5
    23 | 0 3 3
    24 | 3 1 4
    25 | 1 4 5
    26 | 2 1 3
    28 | 2 1 3
    29 | 1 0 1
    30 | 1 1 2
    31 | 0 1 1
    34 | 1 0 1
    35 | 0 2 2
    41 | 0 1 1
    Total | 52 22 74
    -------------------------------------------


    For example, for mileage level 17, there are 2 foreign and 2 domestic, so I would expect the "across" table to generate 50% for each, but using percent I get 54.16 and 45.84. Is there an option that I'm missing? I feel like it must be something simple, but the difference between the percent option and doing the raw division is flummoxing me.
    Last edited by John Hund; 04 Oct 2024, 15:24.

  • #2
    From the documentation.
    statistic(ratiostat [varlist] [, ratio_options]) specifies that ratio statistic ratiostat be
    computed. If varlist is specified, ratios are computed based on the totals of the specified
    variables
    . If varlist is not specified, ratios are computed based on frequencies.
    For your hand calculations, you should be looking at totals instead of cell counts.
    Code:
    . quietly table mpg foreign, stat(total weight)
    
    . collect style cell result[total], empty(0)
    
    . collect preview
    
    ---------------------------------------------
                  |           Car origin
                  |  Domestic   Foreign     Total
    --------------+------------------------------
    Mileage (mpg) |
      12          |     9,560         0     9,560
      14          |    20,250     3,420    23,670
      15          |     7,800         0     7,800
      16          |    15,200         0    15,200
      17          |     7,090     6,000    13,090
      18          |    24,830     5,080    29,910
      19          |    26,640         0    26,640
      20          |     9,360         0     9,360
      21          |    11,000     4,880    15,880
      22          |    14,550         0    14,550
      23          |         0     6,600     6,600
      24          |     8,170     2,280    10,450
      25          |     2,200     8,810    11,010
      26          |     4,750     1,830     6,580
      28          |     5,060     1,760     6,820
      29          |     2,110         0     2,110
      30          |     2,120     1,980     4,100
      31          |         0     2,200     2,200
      34          |     1,800         0     1,800
      35          |         0     4,070     4,070
      41          |         0     2,040     2,040
      Total       |   172,490    50,950   223,440
    ---------------------------------------------
    
    . di 100*7090/13090
    54.163484
    
    . di 100*6000/13090
    45.836516

    Comment


    • #3
      Thanks a lot, that's super helpful!

      Comment

      Working...
      X