Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Distance matrix


From   "Scott Merryman" <smerryman@kc.rr.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Distance matrix
Date   Thu, 16 Jun 2005 13:58:26 -0500

Dan,

Here is one way.  I am not sure about your distance formulas, so I used the
Pythagorean distance formula.


. l, noobs

  +------------------------------+
  | points   latitude   longit~e |
  |------------------------------|
  |      a          2          5 |
  |      b          4          9 |
  |      c          9          4 |
  |      d          1          0 |
  |      e          2          0 |
  +------------------------------+


qui {
local j = 2
count
local num = r(N)
levels points, local(levels) clean	
foreach l in `levels' {
local k = 1
	gen `l' = .
	forv i = `j'/`num' {
		*replace `l'=acos( (cos(latitude[`i'])*cos(latitude[`i' -
`k'])*cos(longitude[`i'] - longitude[`i' - `k'])) + /// 
		(sin(latitude[`i'])*sin(latitude[`i' - `k'])) )*3963 in `i'
		
		replace `l' = sqrt((abs(latitude[`i'] - latitude[`i'
-`k']))^2 + /// 
		(abs(longitude[`i'] - longitude[`i' - `k']))^2) in `i'

		local k = `k' + 1
		}
	local j = `j' + 1
	}
}
l points a-e, noobs


. l points a-e, noobs


  +-------------------------------------------------+
  | points          a          b          c   d   e |
  |-------------------------------------------------|
  |      a          .          .          .   .   . |
  |      b   4.472136          .          .   .   . |
  |      c   7.071068   7.071068          .   .   . |
  |      d    5.09902   9.486833   8.944272   .   . |
  |      e          5   9.219544   8.062258   1   . |
  +-------------------------------------------------+

Hope this helps,
Scott



> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
> statalist@hsphsun2.harvard.edu] On Behalf Of Daniel Egan
> Sent: Monday, June 13, 2005 1:03 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: Distance matrix
> 
> hello all,
> 
> I am attempting to create a dataset consisting of the distance between
> two points.. Currently, the dataset is in a wide format consisting of
> latitude and longitude in the form xx.xxxxxxxxxx
> 
> id     x     y
> a     2     5
> b     4     9
> c     9     4
> .........
> 
> 
> 
> I would like to end up with a dataset consisting of the Euclid distance.
> 
> pair     dist
> a-b     4.5...
> a-c     7.01...
> 
> I believe the formula is:
> distance=arcos( (cos(rlat1)*cos(rlat2)*cos(rlon1-rlon2)) +(sin(rlat1)*sin
> (rlat2)) )*3963
> 
> I have tried
> mds latitude longitude, id(pid)
>  and returned "too many macros". I do have aroun 43k observations.
> 
> Any guidance or assistance would be greatly appreciated.
> 
> 
> Cheers ,
> Dan Egan
> 
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index