Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: distance-based grouping


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: distance-based grouping
Date   Sun, 10 Dec 2006 13:54:10 -0000

At first I thought this is a problem for -cluster-, but it 
doesn't have quite the usual structure. Ken Higbee may be 
able to tell us how to use that. 

You can put in a single observation the ids 
of all nests within 100 units of any given nest like this: 

I assume variables nest_id1, nest_id2, distance. 

gen near_ids = "" 
bysort nest_id1 (distance) : replace near_ids = near_ids[_n-1] + string(nest_id2) + " " if distance <= 100
by nest_id1 : replace near_ids = trim(near_ids[_N]) 

If your ids are already string, you won't need the -string()-. 
If you have many more near nests than can fit into a string variable
in your Stata, this is no use. Otherwise it seems the first step
in your problem. 


Nick 
[email protected] 

> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of 
> Anirudh V. S.
> Ruhil
> Sent: 09 December 2006 23:36
> To: [email protected]
> Subject: st: distance-based grouping
> 
> 
> Given the following data:
> 
> nest_id	nest_id	distance
> 0		408		67.38784
> 0		35		55.74133
> 0		299		72.978
> 0		496		96.11179
> 0		373		62.25668
> 0		466		32.23399
> 0		358		52.49713
> 0		0		0
> 0		42		90.56657
> 0		407		75.57355
> 1		161		21.8282
> 1		128		30.91449
> 1		373		91.10458
> 1		465		54.24871
> 1		496		68.08784
> 1		1		0
> 1		42		61.35756
> 1		34		21.97413
> 
> where "nest_id" flags unique bird nests, and "distance" is 
> measured in 
> feet.
> 
> I need to identify all nests within a specified distance (for 
> e.g., 100) of 
> each other, and thus create unique clusters of nests. These 
> clusters will 
> contain nests that show up once per cluster but may show up 
> in more than 
> one cluster (for e.g., nest 496 shows up in the same cluster 
> as nest 0, 
> shows up again in a different cluster with nest 1, but nest 0 
> and nest 1 
> are not in a common cluster). Hopefully my articulation of 
> the problem is 
> comprehensible, and someone has a clue as to the appropriate 
> syntax. I need 
> to be able to run this code for nests along several miles of 
> a river so 
> anything that cuts down on manual labor would be invaluable.
> 
> best
> 
> Ani
> *
> *   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/
> 

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index