Ritesh Mistry asked
> >Lets say i have a list of 10 individuals and 10 scores.
> For each individual
> >I would like to compute a new variable that is the average
> of the other 9
> >scores. Any ideas on how that can be done?
> >
> >The data for the example look like this:
> >
> >id score newvar
> > 1 10
> > 2 13
> > 3 12
> > 4 19
> > 5 21
> > 6 9
> > .
> > .
> > .
> >10 15
> >
> >The next problem I am having difficulty with is that these
> 10 individuals
> >are nested within a community and there are, lets say, 5
> communities in
> >total with 10 individual is each for total N of 50. I
> would like to compute
> >the new variable, where the value for a particular
> individual is the average
> >of the scores for the other 9 people in the community
> where they reside. So
> >I would like to repeat the procedure separately for each community.
Royce de R. Barondes replied
> 1. Create a new variable "score_total" (for the entire
> population or,
> using "by", the individual community) comprising the sum of
> all the scores
> (within the population or community).
> 2. Generate newvar equal to (score_total-score)/(n-1)
Clearly, Royce is correct. You can use the principle
sum for everybody else = sum for everybody - value for you
after which
average for everybody else = sum for everybody else / (n - 1)
In Stata terms this is, with a group structure:
bysort group : gen newvar = sum(score)
by group : replace newvar = newvar[_N] / (_N - 1)
There could be at least one problem with
this, any missing values in -score-.
The extra wrinkle to cope with that is
something like
bysort group : gen newvar = sum(score)
by group : gen N = sum(score < .)
by group : replace newvar = newvar[_N] / (N[_N] - 1)
or like
bysort group : gen newvar = sum(score)
by group : egen N = count(score)
by group : replace newvar = newvar[_N] / (N - 1)
There is a more detailed discussion of this and
similar problems at
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
Nick
[email protected]
*
* 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/