# Re: st: spatial analysis - DISTANCE

 From David Torres To statalist@hsphsun2.harvard.edu 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?

Thanks,
--------------------------------------------

David Diego Torres, MA(Sociology)

Quoting Scott Merryman <scott.merryman@gmail.com>:

On Sun, Aug 30, 2009 at 9:15 AM, David Torres<torresd@umich.edu> 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.

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

mata
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)
dist
end
svmat N
l

Scott

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

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