Announcement

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

  • Help with ties when using egen and the rank option

    Let's say I have two variables "score" and "rank." I would like "rank" to be a rank of each score allowing for ties. That is, if a tie occurs, each "score" should receive the same rank and the forthcoming rank should "skip" ahead by the number of ties that preceded it (see below for an example).

    I have tried using the egen function with the rank option, but I could not get it to produce what I would like.
    Score Rank
    20 1
    21 2
    22 3
    22 3
    23 5
    24 6
    24 6


  • #2
    You need to use the egen command (not function) with the rank function (not option) and the track option. It's documented, as is the reverse field option.

    Perhaps the names aren't good. In my youth, people could tie at 2nd equal with the same time in a track event or the same height or distance in a field event. That seems rarer now with automatic timing, photofinishes and so forth. I spent some time looking for names for these in 1999 when Richard Goldstein and I first implemented them unofficially, but couldn't find any names in the literature and ended up inventing my own. At least that's my story: Richard may have a different one.

    Since then I have only come across schoolmaster's rank as an alternative name for the field option, which I won't try to persuade anyone to use. There was a comment in
    http://www.stata-journal.com/sjpdf.html?articlenum=pr0046

    Comment


    • #3
      egen Rank = rank(Score), track

      does exactly what you've described.

      Comment


      • #4
        Did you read the entry "rank" using help egen? If you don't understand by reading try out all options:
        Code:
        clear
        input score
        20
        21
        22
        22
        23
        24
        24
        end
        egen rank = rank(score)
        egen rank_f = rank(score), field
        egen rank_t = rank(score), track
        egen rank_u = rank(score), unique
        list, sepby(score)
        By the way: The Statalist FAQ asks you (#3) to read the Stata online help before posting and (#6) to use your full real name (if you think that's pedantic - it helps to keep up the quality of this forum). Use "contact us" at the bottom right to ask the administrators to change your name.

        Comment


        • #5
          Note that even if a egen function didn't exist, this is something you could get directly:

          Code:
          sysuse auto 
          bysort foreign (mpg) : gen rank = _n
          bysort foreign mpg (rank) : replace rank = rank[1]

          Comment


          • #6
            I am concerned that you messed up your data with your use of round(). That function can't perform accurate decimal rounding of fractions because no function can do that. This follows from the fact that at bottom all arithmetic in Stata is binary.

            Also, you seem to be confusing display formats with how data are stored.

            On the contrary, the egen function rank() treats equal values equally.

            Last edited by Nick Cox; 22 Apr 2014, 14:53.

            Comment

            Working...
            X