Statalist


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

st: RE: RE: AW: Compute mean for groups leaving one member out


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: AW: Compute mean for groups leaving one member out
Date   Fri, 23 Oct 2009 19:41:02 +0200

<>
" But also keep track of missing values too."


Very true, and an obvious omission in the rush this morning. As an
economist, I often fall prey to the notion of a perfect world, market, model
whatever...
So here is my modified code that takes account of the missing thing. Note
that I voluntarily introduce the missingness with the -drop- command in the
middle of the code section...



*******
clear*
set obs 40

gen country=_n
gen quarter=quarterly("1990:1", "YQ")

expand 68
bys country: /*
*/ replace quarter=quarter+_n-1
format quarter %tq

gen icrg=rnormal(10,100)

drop if runiform()<.1

//calculate mean
bys quarter: egen totalicrg= /*
*/ total(icrg)
bys quarter: egen numobs= /* 
 */ count(icrg)
//divide by # of countries-1
gen mymean=(totalicrg-icrg)/ /* 
 */ (numobs-1)

//10 random checks...

forv i=1/10{
	local a= 1+int(`c(N)'*runiform())
	l country quarter mymean in `a'
	su icrg if quarter==quarter[`a'] /* 
	*/ & country!=country[`a']
}
*******


HTH
Martin

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nick Cox
Sent: Freitag, 23. Oktober 2009 19:31
To: [email protected]
Subject: st: RE: AW: Compute mean for groups leaving one member out

In words, you get 

1. the total of every value but one value 

2. the number of values - 1 

and divide. But also keep track of missing values too. 

Nick 
[email protected] 

Martin Weiss

"Could you suggest a simple way of doing this?"



Taking a cue from Nick`s FAQ cited earlier:


*************
clear*
set obs 40

gen country=_n  
gen quarter=quarterly("1990:1", "YQ")

expand 68
bys country: /* 
*/ replace quarter=quarter+_n-1
format quarter %tq

gen icrg=rnormal(10,100)

//calculate mean
bys quarter: egen totalicrg= /* 
*/ total(icrg)
//divide by # of countries-1
gen mymean=(totalicrg-icrg)/39

//10 random checks...

forv i=1/10{
	local a= 1+int(`c(N)'*runiform())
	l country quarter mymean in `a'
	su icrg if quarter==quarter[`a'] /* 
	*/ & country!=country[`a']
}
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von kokootchke
Gesendet: Freitag, 23. Oktober 2009 07:39
An: statalist
Betreff: st: Compute mean for groups leaving one member out

Dear all:

I have a panel dataset of 40 countries at quarterly frequency from 1990:1 to
2006:4. I would like to compute the average of a variable (called icrg) for
all countries in a given time period. My problem is that in doing this
computation, I would like to leave one country out of the calculation. 

For example, in period 1990:1 for Mexico, I would like to compute the
average of icrg for ALL other countries in this period EXCLUDING Mexico. 

Could you suggest a simple way of doing this?


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index