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

Re: st: spatial analysis - DISTANCE

From   David Torres <>
Subject   Re: st: spatial analysis - DISTANCE
Date   Sun, 30 Aug 2009 11:40:07 -0400

I copied and pasted your example, Scott, and got a couple of error messages about N matrix not found. I installed the moremata files, but this didn't solve the problem. I'm not quite as adept at using stata as some, so, please, bear with me. I've never used a mata command, so would like to know whether its use will allow for variable creation wheere the unitids can be stored and reshaped and all that jazz?


David Diego Torres, MA(Sociology)

Quoting Scott Merryman <>:

On Sun, Aug 30, 2009 at 9:15 AM, David Torres<> wrote:
Austin, I've looked over the code you offered and it seems that what I'm
getting is just the xy coordinates rather than what I need.  There's got to
be a way, somehow, to have stata return a list of all the unitids that are
within the specified radius.  When I run the code I have, all I get is a
return of the unitid on the nearest school to my 65000 tracts.  However,
within the rad10 variable, several tracts have multiple schools within my
radius of 10 miles.  I need to get all of these unitids regardless of
whether I end up with wide or long data; I can always reshape later.

How about something like this:

set obs 9
gen id =_n
set seed 1234
gen latitude = uniform()/2
gen longitude = uniform()/2

X= st_data(.,( "latitude" , "longitude"))
X =X*(pi()/180)
dist = J(rows(X),rows(X), 0)
for (i = 1; i <=rows(X); i++) {
    for (j = 1; j <=rows(X); j++) {
	  dist[i,j] = (1/1.609)*6372.795*(2*asin(sqrt( sin((X[i,1] ///
	  - X[j,1])/2)^2 +  cos(X[i,1])*cos(X[j,1])*sin((X[i,2] ///
	  - X[j,2])/2)^2  )))
N = mm_cond(dist :<= J(rows(dist), cols(dist), 10), ///
  J(rows(dist), cols(dist),1), J(rows(dist), cols(dist), 0))
st_matrix("N" ,N)
svmat N


*   For searches and help try:

*   For searches and help try:

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