Statalist


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

st: Building a matrix of distances with sphdist


From   "Davide Cantoni" <[email protected]>
To   [email protected]
Subject   st: Building a matrix of distances with sphdist
Date   Sat, 18 Oct 2008 11:37:08 -0400

Hello,

I have a panel dataset with cities observed in different years, and I
would like to construct a matrix of (great circle) distances of all
cities to each other. I was using, up to now, Bill Rising's -sphdist-.
So this is the way I was thinking to proceed.

*** begin code***

	levelsof cityid, local(allcities)
	foreach C of local allcities {
	
		sum latitude if cityid==`C', meanonly
		gen lat1 = r(mean)
		sum longitude if cityid==`C', meanonly	
		gen lon1 = r(mean)
		
		foreach K of local allcities {
			
				sum latitude if cityid==`K', meanonly
				gen lat2 = r(mean)
				sum longitude if cityid==`K', meanonly	
				gen lon2 = r(mean)
				
				sphdist if cityid==`C', lat1(lat1) lon1(lon1) lat2(lat2)
lon2(lon2) gen(dist_`K')
				
				drop lat2 lon2
				
		}
		
		drop lat1 lon1

	}

*** end code ***

The problem is that -sphdist- does not accept something like "replace"
but would only create new variables with the required "gen" option. So
when the second city in  `C' is processed, Stata complains that the
variable "dist_1" exists already. What I want, though, is that for any
city in my panel the variable "dist_1" indicates the distance to city
#1, so that each variable "dist_1", "dist_2" etc fills up as Stata
mills through the values of `C'.

Alternatively, one could create NxN variables by replacing the
-sphdist- command with

sphdist if cityid==`C', lat1(lat1) lon1(lon1) lat2(lat2) lon2(lon2)
gen(dist_`C'_to_`K')

but then again Stata does not accept the large number of variables
that corresponds to NxN.

Do you have any suggestions on how to solve this? Many thanks for your interest.

Best regards,

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