Announcement

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

  • Unequal number of observations in percentile groups

    Hi Stata users,

    I am trying to come up with percentile groups using the code below

    Code:
    _pctile asset_index, nquantiles(100)
    return list
    forval i=1/99 {
    local p`i' = (r(r`i'))
    }
    
    gen percent = .
    replace percent = 1 if asset_index < `p1'
    replace percent = 100 if asset_index > `p99' & asset_index < .
    forval i=1/98 {
    di "``i''"
    local j = `i' + 1
    di "lower `p`i''"
    di "upper `p`j''"
    replace percent = `i' + 1 if inrange(asset_index, `p`i'', `p`j'')
    }
    ta percent
    I am noticing that there are groups with extremely few number of observations such as 3 or 5. I am not sure whether the syntax is wrong or my understanding of how percentiles are calculated is misinformed.

    Am attaching the dataset since
    Code:
    dataex
    may not be the most effective way of sharing 9097 observations

    Any advice is welcome.

    Thanks in advance!
    Attached Files
    Last edited by Stephen Okiya; 29 Nov 2021, 01:05.

  • #2
    One line of code is sufficient. It seems one group has 5 obs while others are reasonably large.

    Code:
    xtile percent = asset_index, n(100)
    Code:
    . tab percent, sort
    
            100 |
      quantiles |
             of |
    asset_index |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             33 |        281        3.10        3.10
             27 |        258        2.84        5.94
             38 |        255        2.81        8.75
             21 |        236        2.60       11.35
             16 |        221        2.44       13.79
             12 |        207        2.28       16.07
             50 |        200        2.20       18.27
             46 |        199        2.19       20.46
             24 |        185        2.04       22.50
             18 |        180        1.98       24.48
             53 |        168        1.85       26.34
             40 |        162        1.79       28.12
             31 |        158        1.74       29.86
             43 |        148        1.63       31.49
              5 |        145        1.60       33.09
             26 |        139        1.53       34.62
             66 |        138        1.52       36.14
              9 |        136        1.50       37.64
             14 |        130        1.43       39.07
             10 |        126        1.39       40.46
             84 |        120        1.32       41.79
             82 |        113        1.25       43.03
              3 |        112        1.23       44.26
             58 |        112        1.23       45.50
             64 |        112        1.23       46.73
             67 |        110        1.21       47.94
             70 |        109        1.20       49.15
             49 |        107        1.18       50.33
             55 |        106        1.17       51.49
             20 |        103        1.13       52.63
              2 |        102        1.12       53.75
             68 |        102        1.12       54.88
              1 |         98        1.08       55.96
             36 |         98        1.08       57.04
             81 |         98        1.08       58.12
             63 |         97        1.07       59.18
             88 |         97        1.07       60.25
             61 |         96        1.06       61.31
             90 |         96        1.06       62.37
             74 |         95        1.05       63.42
             80 |         95        1.05       64.46
             97 |         94        1.04       65.50
             56 |         93        1.02       66.52
             76 |         93        1.02       67.55
             85 |         93        1.02       68.57
             94 |         93        1.02       69.60
              7 |         92        1.01       70.61
             71 |         91        1.00       71.61
             92 |         91        1.00       72.62
             99 |         91        1.00       73.62
             45 |         90        0.99       74.61
             78 |         90        0.99       75.60
             96 |         90        0.99       76.60
            100 |         90        0.99       77.59
             72 |         89        0.98       78.57
             79 |         89        0.98       79.55
             93 |         89        0.98       80.53
              6 |         88        0.97       81.50
             91 |         88        0.97       82.47
             95 |         88        0.97       83.44
             98 |         88        0.97       84.41
             59 |         86        0.95       85.36
             62 |         84        0.93       86.28
             89 |         84        0.93       87.21
             77 |         83        0.91       88.12
             73 |         80        0.88       89.00
             75 |         79        0.87       89.87
             87 |         78        0.86       90.73
             60 |         76        0.84       91.57
             37 |         72        0.79       92.36
             86 |         72        0.79       93.16
              8 |         70        0.77       93.93
             57 |         65        0.72       94.64
             65 |         63        0.69       95.34
             48 |         61        0.67       96.01
             52 |         58        0.64       96.65
             83 |         57        0.63       97.28
              4 |         53        0.58       97.86
             44 |         46        0.51       98.37
             30 |         37        0.41       98.78
             42 |         37        0.41       99.18
             32 |         26        0.29       99.47
             69 |         22        0.24       99.71
             54 |         21        0.23       99.94
             15 |          5        0.06      100.00
    ------------+-----------------------------------
          Total |      9,075      100.00

    Comment


    • #3
      Thanks so much Fei Wang for your help. This is great!!

      Comment


      • #4
        This overlaps with your earlier thread https://www.statalist.org/forums/for...centile-groups

        Please don't run two or more threads on the same topic.

        Comment


        • #5
          Nick Cox Well noted!

          Comment

          Working...
          X