Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Re: calculating proportions


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: calculating proportions
Date   Tue, 21 Feb 2012 19:12:58 +0000

If I understand you correctly, the calculations below will give you
the ingredients you want, or hints on how to get them. As before
-relrank- (Ben Jann) is from SSC.

sysuse auto, clear
contract mpg foreign
relrank mpg if foreign [fw=_freq] , ref(mpg if !foreign) gen(F)
gen negmpg = -mpg
relrank negmpg if foreign [fw=_freq] , ref(negmpg if !foreign) gen(G)
replace G = 1 - G
gen H  = F - G
l mpg F G H


On Tue, Feb 21, 2012 at 6:31 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> No, that's not what I said. It's the opposite, if anything.
>
> In more general terms: we can't comment on code that you don't show
> us. Your previous mail could be read as implying that there was a
> problem with the code using local macros posted by others. My point is
> that there wasn't a problem of that kind. If there was a problem with
> local macros, it lay in your translation of the code.
>
> However, various other questions remain up in the air. The code you
> are using loops over observations. It is evident from looking at the
> code for -relrank- (SSC) that no such approach is necessary, but if
> your problem is solved you may not mind about that, but I wouldn't
> want this thread to fade away without making that point.
>
> Nick
>
> On Tue, Feb 21, 2012 at 6:20 PM, Ekaterina Hertog
> <ekaterina.hertog@sociology.ox.ac.uk> wrote:
>> Dear Nick,
>> thanks a lot for the pointer.
>> I must have had N_female defined earlier. Once I dropped all macros, it
>> worked,
>> Ekaterina
>>
>> On 21/02/2012 17:33, Nick Cox wrote:
>>>
>>> There are various possible reasons for a problem here with what
>>> Ekaterina did, including
>>>
>>> 1. No local macro N_female was defined by her earlier.
>>>
>>> 2. One such was defined by her but is not visible to Stata at that point.
>>>
>>> The use of local macros looks fine in the code posted by Pathmes and
>>> Oliver.
>>>
>>> Nicl
>>>
>>> On Tue, Feb 21, 2012 at 5:22 PM, Ekaterina Hertog
>>> <ekaterina.hertog@sociology.ox.ac.uk>  wrote:
>>>>
>>>> Dear Pathmes and Oliver,
>>>> thanks a lot, great code and it solves my problems! And I learned more
>>>> than
>>>> 3 things from it
>>>> Just a small hitch:
>>>> converting numbers to proportions syntax:
>>>>
>>>> gen fap=fa/`N_female'
>>>>
>>>> gives an error message: 'invalid syntax'.
>>>> I went around it by creating a variable which is a constant and equals a
>>>> total number of women in the population and using it instead of the local
>>>> macro. I clumsy way to do it.
>>>> katya
>>>>
>>>> On 21/02/2012 16:45, Pathmeswaran wrote:
>>>>>
>>>>> Thanks Oliver.
>>>>>
>>>>> With your help I was able to learn 3 things on my first day in
>>>>> statalist!
>>>>>
>>>>>   forvalues
>>>>>   `r(N)'
>>>>>   indentation in foreach/ forvalue loops
>>>>>
>>>>> *************** example code begins **************************
>>>>>
>>>>> version 8
>>>>> clear all
>>>>> set obs 7
>>>>>
>>>>> gen byte id = _n
>>>>>
>>>>> gen byte sex = 1
>>>>> replace sex = 0 in 4/7
>>>>> label def lbl_sex 0 "male" 1 "female"
>>>>> label values sex lbl_sex
>>>>>
>>>>> gen int income = 7
>>>>> replace income = 10 in 3
>>>>> replace income = 0 in 4
>>>>> replace income = 7 in 5
>>>>> replace income = 9 in 6
>>>>> replace income = 11 in 7
>>>>>
>>>>> list
>>>>>
>>>>> * I have just copied Oliver's code
>>>>>
>>>>> sort sex
>>>>> gen idm=_n if sex==0
>>>>> * idm is a unique number for each male
>>>>>
>>>>> gen fa=.
>>>>> label variable fa "Number of females with higher income"
>>>>>
>>>>> gen fb=.
>>>>> label variable fb "Number of females with lower income"
>>>>>
>>>>> gen fe=.
>>>>> label variable fe "Number of females with equal income"
>>>>>
>>>>> * The variables fa, fb&    fe are generated individually for each male
>>>>> in the following loop
>>>>> quietly{
>>>>>        count if sex == 0
>>>>>        local N_male = `r(N)'
>>>>>
>>>>>        count if sex == 1
>>>>>        local N_female = `r(N)'
>>>>>
>>>>>        forvalues i = 1/`N_male' {
>>>>>                egen int feabove = rank(income) if sex==1 | (sex==0&
>>>>>  idm==`i'), field
>>>>>                replace fa= feabove - 1 if idm==`i'
>>>>>                drop feabove
>>>>>
>>>>>                egen int febelow = rank(income) if sex==1 | (sex==0&
>>>>>  idm==`i'), track
>>>>>                replace fb=  febelow - 1 if idm==`i'
>>>>>                drop febelow
>>>>>
>>>>>                replace fe=`N_female' - ( fa+ fb) if idm==`i'
>>>>>        }
>>>>> }
>>>>>
>>>>> * Converting the numbers to proportions
>>>>>
>>>>> gen fap=fa/`N_female'
>>>>> label variable fap "Proportion of females with higher income"
>>>>>
>>>>> gen fbp=fb/`N_female'
>>>>> label variable fbp "Proportion of females with lower income"
>>>>>
>>>>> gen fep=fe/`N_female'
>>>>> label variable fep "Proportion of females with equal income"
>>>>>
>>>>> **************** example code ends ***************************
>>>>>
>>>>>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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   |   Site index