Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Assigning value to highest rank


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Assigning value to highest rank
Date   Mon, 24 May 2004 12:24:36 +0100

There may be a neater way than this rather 
awkward grinding through cases: 

First you can generate a row maximum by 

gen max = max(pl,pd,pu) 

Then the number of values equal to the maximum is 

gen nmax = (pd == max) + (pu == max) + (pl == max) 

The case of a unique maximum is 

gen newvar = (pd == max) + 0.5 * (pu == max) if nmax == 1 

Then your ties between two are 

replace newvar = cond(uniform() < 0.5, 1, 0.5) if pd == max & pu == max
replace newvar = cond(uniform() < 0.5, 0.5, 0) if pu == max & pl == max 
replace newvar = cond(uniform() < 0.5, 1, 0) if pd == max & pl == max 

Presumably the case of three ties needs addressing 

gen random = uniform()
replace newvar = cond(random < 1/3, 1, cond(random < 2/3, 0.5, 0)) if nmax == 3 

Any interest in 

gen newvar2 = ((pd == max) + 0.5 * (pl == max))  / nmax 

as a simpler scoring system? That way, 

pd is unique max => 1 
pl is unique max => 0.5 
pu is unique max => 0 
pd and pl tie    => 0.75 
pd and pu tie    => 0.5
pl and pu tie    => 0.25 
pd, pl, pu tie   => 0.5 

Nick 
n.j.cox@durham.ac.uk 

Tonya Cropper
> 
> I  have three variables within each observation that I am 
> trying to assign a
> value for a new variable based on which of my three variables have the
> highest value.  If the highest value is pl,  I would like to 
> assign newvar =
> 0, if the highest value is pd: newvar = 1, or if the highest 
> value is pu:
> newvar = 0.5.  However, if there's a tie for the highest 
> value, I would like
> to randomly choose the winner and then assign the value as above.
> 
> 
> 
> Here's an example of the data and what I would want newvar to 
> indicate.  I
> would greatly appreciate any assistance.  Thank you.
> 
> 
> 
> Id         pd             pl            pu             newvar
> 
> 117     11              11            8             (1 or 0 
> based on whether
> pd or pl win toss)
> 
> 118     12              8              10            1
> 
> 120     18              10             14           1
> 
> 121      6               11              7            0
> 
> 122      4               9                9            (0 or 
> 0.5 based on
> whether pl or pu win toss)
> 
> 123      6               8                10          0.5
> 
> 126      9               9                12          0.5
> 
> 127      10             7                10          (1 or 
> 0.5 based on
> whether pd or pu win toss)
> 
> 129      9               11              10           0
> 
> 131      9               5                 4            1
> 
> 132      4                5                9            0.5

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index