Announcement

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

  • Quantiles: inverse distribution

    I would like to generate a variable that categorizes another variable by its quantile, but in a reverse order, i.e. the first quantile is 5, the second is 4, etc. I have not been able to figure out a simple way to do this. Can anyone help me? Thanks in advance.

  • #2
    There are cunning tricks you might use, but I'd go for grade/primary school arithmetic:

    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . xtile myvar=mpg, nq(5)
    
    . gen myvar2 = 6 - myvar
    
    . tab myvar*
    
             5 |
     quantiles |                         myvar2
        of mpg |         1          2          3          4          5 |     Total
    -----------+-------------------------------------------------------+----------
             1 |         0          0          0          0         18 |        18
             2 |         0          0          0         17          0 |        17
             3 |         0          0         13          0          0 |        13
             4 |         0         12          0          0          0 |        12
             5 |        14          0          0          0          0 |        14
    -----------+-------------------------------------------------------+----------
         Total |        14         12         13         17         18 |        74
    In short, if you have 1..5 and want 5..1 then just subtract from 6. Generalise "for any value of 5", an expression I think echoes a classroom saying of the great probabilist William Feller.

    By the way, you've been a member of the forum for a while and should know of our preference for full real names. Please re-register with your full real name using the CONTACT US button at bottom right.


    .
    Last edited by Nick Cox; 02 Mar 2015, 06:11.

    Comment

    Working...
    X