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: calculating mean without own observation


From   Phil Clayton <philclayton@internode.on.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: calculating mean without own observation
Date   Tue, 24 May 2011 00:25:50 +0930

I'm sure there's a more efficient way but one option would just be to loop over the observations manually and calculate the mean each time:

clear

input i      value      category
1     5            1
2     5            1
3     10          1
4      2           2
5      2           2
end

gen value_mean=.
forval i=1/`=_N' {
	summarize value if category==category[`i'] & _n!=`i', meanonly
	quietly replace value_mean=r(mean) in `i'
}

list

Phil

On 24/05/2011, at 12:02 AM, Guo Xu wrote:

> Dear Stata list,
> 
> How do I calculate a mean (or any other summary statistic) excluding
> the *current* observation?
> 
> For example, I have following data:
> 
> i      value      category
> 1     5            1
> 2     5            1
> 3     10          1
> 4      2           2
> 5      2           2
> 
> I would like to calculate the mean for each category (egen value_mean
> = mean(value), by(category)), but exclude the i-th observation:
> For i=1, for example, the mean value by category 1 would be (5+10)/2.
> For i=3, it would be (5+5)/2. For i=4, it would be 2/1.
> 
> I guess there must be some simple *if* condition for this
> manipulation, but I failed to find it - would be most grateful for
> help.
> 
> Thanks,
> 
> Guo
> *
> *   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/


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