 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

st: RE: a question on averaging in Stata

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: a question on averaging in Stata Date Wed, 18 Jan 2012 13:34:11 +0000

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

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?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/