Another approach is a simple dofile which should work if the counties codes are consecutive 1 thru N so that each county code equal _n. This assumes, as in the example, that nei1 always has a value and a max of 2 adjacent counties.
gen n1vote=.
gen n2vote=.
local N = _N
forv i=1/`N' {
local v1=nei1[`i']
local v2=nei2[`i']
replace n1v=vote[`v1'] in `i'
replace n2v=vote[`v2'] if `v2'~=. in `i'
}
gen neiVote=n1v if n2vote==.
replace neiVote=(n1v+n2v)/2 if n2v ~=.
Jerry
************************************
Gerald C. Wright
Dept of Political Science
Indiana University
Bloomington, IN
phone (812) 855-6308
fax (812) 855-2027
web: http://mypage.iu.edu/~wright1/
*************************************
-----Original Message-----
From: Political Methodology Society [mailto:[log in to unmask]] On Behalf Of Jonathan Woon
Sent: Friday, September 29, 2006 1:41 PM
To: [log in to unmask]
Subject: Re: [POLMETH] Neighbours' average in stata
Eric,
I think you can accomplish this by reshaping, merging, and collapsing:
BEGIN STATA CODE**********************
save original
/* create a file with just counties and votes, to be merged later */ keep
cty vote ren cty nei_cty ren vote nei_vote sort nei_cty save neivotes
/* now use the original file and reshape so that you have a list of every
county-neighbor pair */ use original drop vote reshape long nei, i(cty)
j(num) drop num drop if nei==.
ren nei nei_cty
sort nei_cty
/* merge pairwise list with votes for neighbor counties */ merge nei_cty
using neivotes
/* use the collapse command to obtain means */ collapse (mean) avgneivote =
nei_vote, by(cty)
/* you can then save the results and merge with your original data */
END STATA CODE*************************
Of course, you'll want to do a couple of hand calculations just to be sure I
haven't led you astray...
Best,
Jon
-----------------------------------
Jonathan Woon
Assistant Professor
Dept. of Social & Decision Sciences
Carnegie Mellon University
Porter Hall 223B
(412) 268-3226 (phone)
(412) 268-6938 (fax)
<http://www.andrew.cmu.edu/user/jwoon>
-----Original Message-----
From: Political Methodology Society [mailto:[log in to unmask]] On
Behalf Of eric magar
Sent: Friday, September 29, 2006 11:45 AM
To: [log in to unmask]
Subject: [POLMETH] Neighbours' average in stata
Dear colleagues:
I will be grateful for help on something that should be easy to do in Stata
but I am unfamiliar with. I have observations at the county level and wish
to compute the average value of certain quantities of interest in
neighbouring counties, such as the average share of the two-party vote.
My dataset has info on which counties are neighbours, ie those sharing a
border with the current observation. This is coded as a series of variables
listing each a neighbouring county, in no special order. Once the listing
for a county is complete, the remainder neighbour variables adopt missing
values. A simple example with four counties arranged in a line would be:
cty vote nei1 nei2
1 0.45 2 .
2 0.59 1 3
3 0.51 4 2
4 0.43 3 .
How can I get the average vote in neighbouring counties? My dataset is quite
large, so doing it case by case would take too long.
Thank you and best wishes,
--e
______________________
Eric Magar
Ciencia Política, ITAM
Mexico City
--
La información contenida en este mensaje de datos es confidencial,
constituye un secreto industrial y/o profesional en términos de la
legislación vigente y se encuentra dirigida exclusivamente al destinatario
indicado en dicho mensaje. Si usted recibe esta información por error o si
usted no es el destinatario del mensaje, favor de notificar al emisor, y
destrúyalo.
The information contained in this electronic message is confidential, it
constitutes a profesional and/or industrial secret in terms of the current
legislation, and is intended for its recipient only. If you receive this
message by mistake and/or if you are not the recipient thereof, please
notify the sender and destroy it.
**********************************************************
Political Methodology E-Mail List
Editor: Karen Long Jusko <[log in to unmask]>
**********************************************************
Send messages to [log in to unmask]
To join the list, cancel your subscription, or modify
your subscription settings visit:
http://polmeth.wustl.edu/polmeth.php
**********************************************************
**********************************************************
Political Methodology E-Mail List
Editor: Karen Long Jusko <[log in to unmask]>
**********************************************************
Send messages to [log in to unmask]
To join the list, cancel your subscription, or modify
your subscription settings visit:
http://polmeth.wustl.edu/polmeth.php
**********************************************************
**********************************************************
Political Methodology E-Mail List
Editor: Karen Long Jusko <[log in to unmask]>
**********************************************************
Send messages to [log in to unmask]
To join the list, cancel your subscription, or modify
your subscription settings visit:
http://polmeth.wustl.edu/polmeth.php
**********************************************************
|