Announcement

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

  • Form deciles with no 0 observations

    Hello,

    Can you please help me with the following issue: I want to form deciles based on the credit rating for different bonds.
    The credit rating variable (rating_num) can take values between 1 and 22.
    I tried 2 different methods and they yield the same result:

    bys monthly_dates: astile decile_cr_monthly = rating_num, nq(10)

    egen decile_r_num = xtile(rating_num ) , by(monthly_dates ) nq(10)

    The problem is that in deciles 5 and 7 I have 0 observations.
    I understand that this is due to the cutoff point.
    I was wondering if there is a way to define the cutoff points such that I have observations in each of the 10 deciles?

    Thank you very much for your help!







  • #2
    If you have 10 or more distinct values, then you can always group them into 10 classes, but very likely they won't be deciles (meaning, decile bins with each 1/10 of the values).

    Code:
    tab rating_num
    
    quantile rating_num
    will usually make the problem clear.

    More discussion in

    SJ-18-3 dm0095 . . . . . . . . . . . Speaking Stata: From rounding to binning
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q3/18 SJ 18(3):741--754 (no commands)
    basic review of how to bin variables in Stata, meaning how to
    divide their range or support into disjoint intervals

    https://www.stata-journal.com/articl...article=dm0095 [needs subscription access or one-off payment]


    SJ-12-4 pr0054 . . . . . . . . . . Speaking Stata: Matrices as look-up tables
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q4/12 SJ 12(4):748--758 (no commands)
    illustrates the use of matrices as look-up tables

    https://www.stata-journal.com/sjpdf....iclenum=pr0054 (see Section 4) [accessible to all]

    With integer values, ties are often common and may well rule out equal frequencies in quantile bins. Here are some results making the point:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    * -distinct- is from the Stata Journal and must be installed before it can be used
    . distinct
    
    -------------------------------------
                  |     total   distinct
    --------------+----------------------
             make |        74         74
            price |        74         74
              mpg |        74         21
            rep78 |        69          5
         headroom |        74          8
            trunk |        74         18
           weight |        74         64
           length |        74         47
             turn |        74         18
     displacement |        74         31
       gear_ratio |        74         36
          foreign |        74          2
    -------------------------------------
    
    . tab trunk
    
    Trunk space |
      (cu. ft.) |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              5 |          1        1.35        1.35
              6 |          1        1.35        2.70
              7 |          3        4.05        6.76
              8 |          5        6.76       13.51
              9 |          4        5.41       18.92
             10 |          5        6.76       25.68
             11 |          8       10.81       36.49
             12 |          3        4.05       40.54
             13 |          4        5.41       45.95
             14 |          4        5.41       51.35
             15 |          5        6.76       58.11
             16 |         12       16.22       74.32
             17 |          8       10.81       85.14
             18 |          1        1.35       86.49
             20 |          6        8.11       94.59
             21 |          2        2.70       97.30
             22 |          1        1.35       98.65
             23 |          1        1.35      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    
    
    . xtile trunk10=trunk, nq(10)
    
    . tab trunk10
    
             10 |
      quantiles |
       of trunk |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |         10       13.51       13.51
              2 |          9       12.16       25.68
              3 |          8       10.81       36.49
              4 |          3        4.05       40.54
              5 |          8       10.81       51.35
              6 |         17       22.97       74.32
              8 |          8       10.81       85.14
              9 |          7        9.46       94.59
             10 |          4        5.41      100.00
    ------------+-----------------------------------
          Total |         74      100.00

    Comment


    • #3
      Hi Nick,

      Thank you so much for your answer!

      Comment

      Working...
      X