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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: median without current observation


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: median without current observation
Date   Mon, 15 Apr 2013 10:17:55 +0100

Federico and I made different guesses about the data structure and
what is needed.

In Federico's case, the code can be simplified, I think

levelsof region, local(reglist)
gen me_y = .
foreach r of local reglist {
       su y if region != `r', detail
       replace me_y  = r(p50) if region==`r'
}

Nick
njcoxstata@gmail.com

On 15 April 2013 09:22, Federico Belotti <f.belotti@gmail.com> wrote:

> The following code creates R mediany variables (R is the number of regions), where each variable takes the median of the distribution of "y" excluding the value of region `r'.
>
> levelsof region, local(reglist)
> foreach r of local reglist {
>
>         egen me_y`r' = median(y) if region!=`r'
>         tempvar tmp
>         egen "`tmp'" = max(me_y `r')
>         replace me_y `r' = `tmp' if region==`r'
>
> }
>
> hope this helps.
> Federico
>
> On Apr 15, 2013, at 9:29 AM, Andres Silva wrote:
>
>> Dear statausers,
>>
>> I want to create a variable "mediany", which needs to be regional
>> median (without considering the current value of "y"). I mean I need
>> to calculate the median with the remaining values of "y" in each
>> specific region.
>>
>> by region: egen mediany=median(y)
>> How can I tell stata not use the current "y" value?
>>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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