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: RE: a question on averaging in Stata


From   "rado645-bg@yahoo.de" <rado645-bg@yahoo.de>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: RE: a question on averaging in Stata
Date   Wed, 8 Feb 2012 14:30:50 +0000 (GMT)

Dear Nick,

thanks a lot for your feedback. The information was very useful. I have one additional question that relates to estimating a group median excluding observation i. I have looked at the article that you have referred to, but I got stuck with writing the code for the case of medians. 


Again I have a panel data with items i observed over several years t for variable x. I need to estimate the median of this variable for each year. However I have to estimate a specific median: for each item i I have to estimate 
the median value of x but excluding the observation for item i itself: i.e. the median over the other objects (if I could label them 
-i).
I found this technically more challenging compared to the estimation of means.  I have started with the following code - I used as example one of the codes that you have shared with us in your article. But I cannot find a way to isolate item i from the median calculation. 

Could you please help me with that? I would like to thank you in advance.

Kind regards,
Rado



 generate maxvar = .  
  by year, sort: gen pid = _n 
 summarize pid
 . quietly forvalues i = 1/`r(max)' { 
 .       generate include = 1 if pid != `i'  
 .       egen work = median(idio * include), by(year) 
 .       replace maxvar = work if pid == `i' 
 .       drop include work 
 . }







----- Ursprüngliche Message -----
Von: Nick Cox <n.j.cox@durham.ac.uk>
An: "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Cc: 
Gesendet: 14:34 Mittwoch, 18.Januar 2012
Betreff: st: RE: a question on averaging in Stata

This is a FAQ. 

FAQ     . . Creating variables recording prop. of the other members of a group
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        4/05    How do I create variables summarizing for each
                individual properties of the other members of a
                group?
                http://www.stata.com/support/faqs/data/members.html

but the question also yields easily to Stata logic. The starting point is the idea that the total for everybody else is just the total minus my value. 

The average of every other item is 

(sum of others) / (count of others) 

which is in the simplest case

(sum of all - this value) / (count of all - 1) 

-- although careful code would need to take account of the possibility that each value is missing. 

That is then 

egen sum = total(x), by(group)
egen count = count(x), by(group) 

and then the average is 

gen mean = (sum - cond(missing(x), 0, x) / (count - !missing(x)) 

If any value is missing, then we need to subtract 0 (not missing!) from the total to get the total of others. 

If any value is missing, then we need to subtract 0 (not 1!) from the count to get the count of others. 

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

rado645-bg@yahoo.de

I have a panel data with items i observed over several years t for variable x. 

I have to estimate a specific average: for each item i I have to take the mean value of x  excluding the observations for the item i itself;i.e. the average over the other objects (if I could label them -i).

Is this possible in Stata?



*
*   For searches and help try:
*  http://www.stata.com/help.cgi?searchhttp://www.stata.com/support/statalist/faqhttp://www.ats.ucla.edu/stat/stata/


*
*   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