# Re: st: spatial analysis - DISTANCE

 From David Torres To statalist@hsphsun2.harvard.edu Subject Re: st: spatial analysis - DISTANCE Date Sun, 30 Aug 2009 12:23:37 -0400

```This is what I get:

. clear

. set obs 9
obs was 0, now 9

. gen id =_n

. set seed 1234

. gen latitude = uniform()/2

. gen longitude = uniform()/2

.
. mata
```
------------------------------------------------- mata (type end to exit) --------------------------------------------------
```: 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))
```
```                 <istmt>:  3499  mm_cond() not found
r(3499);

: st_matrix("N", N)
r(3499);

: dist
[symmetric]
```
1 2 3 4 5 6 7 8 +-----------------------------------------------------------------------------------------------------------------
```  1 |            0
2 |  5.157033248             0
3 |  21.21062478   21.71534226             0
4 |  26.95060264   27.64838102   5.939930861             0
5 |   16.1570544    21.1565295   30.29833499    34.5960745             0
```
6 | 14.73674249 19.76346139 22.78331973 26.58113711 8.439103366 0 7 | 6.614080612 9.633040063 15.1934957 20.70923619 17.14313644 12.30209368 0 8 | 22.069818 20.34155009 9.309128607 12.92307095 35.0470512 28.79024435 18.03555849 0 9 | 20.67616548 20.42184227 3.226163273 8.078752624 31.37740006 24.32651125 15.24958295 6.116268222 +-----------------------------------------------------------------------------------------------------------------
```                 9
---------------+
9              0  |
---------------+

: end
----------------------------------------------------------------------------------------------------------------------------

. svmat N
r(111);

end of do-file

r(111);

.

There are two 3499 errors and two 111s.

```
So I need to store my unitids (actually school ipeds codes) so that I can eventually collapse that data on census 2000 info to investigate what postsecondary schools and types of schools serve what populations. If I take distances from school to all tracts within, say, 10 miles, I want to collapse population demographics and create some descriptives using stata. I can do this the other way around, too, using tracts as the center of my radii. I can get a return on the one nearest school, but can't seem to get the next nearest on out to the edge of the specified radius.
```
Diego

--------------------------------------------

David Diego Torres

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

```
```Sorry - I forgot to mention the code uses Ben Jann's -moremata-.

What is the error?

The example returns the distance matrix and neighbor matrix and then
creates the N variables in the original data set:

: dist
[symmetric]
1             2             3             4
5             6             7             8             9
```
+--------------------------------------------------------------------------------------------------------------------------------+
```  1 |            0
|
2 |  5.157033248             0
|
3 |  21.21062478   21.71534226             0
|
4 |  26.95060264   27.64838102   5.939930861             0
|
5 |   16.1570544    21.1565295   30.29833499    34.5960745
0                                                          |
6 |  14.73674249   19.76346139   22.78331973   26.58113711
8.439103366             0                                            |
7 |  6.614080612   9.633040063    15.1934957   20.70923619
17.14313644   12.30209368             0                              |
8 |    22.069818   20.34155009   9.309128607   12.92307095
35.0470512   28.79024435   18.03555849             0                |
9 |  20.67616548   20.42184227   3.226163273   8.078752624
31.37740006   24.32651125   15.24958295   6.116268222             0  |
```
+--------------------------------------------------------------------------------------------------------------------------------+
```

```
+-----------------------------------------------------------------------+ | id latitude longit~e N1 N2 N3 N4 N5 N6 N7 N8 N9 | |-----------------------------------------------------------------------| 1. | 1 .23851 .0647102 1 1 0 0 0 0 1 0 0 | 2. | 2 .1653217 .0502572 1 1 0 0 0 0 1 0 0 | 3. | 3 .1722953 .3643155 0 0 1 1 0 0 0 1 1 | 4. | 4 .1788673 .4499913 0 0 1 1 0 0 0 0 1 | 5. | 5 .4713051 .043847 0 0 0 0 1 1 0 0 0 | |-----------------------------------------------------------------------| 6. | 6 .4298635 .1586816 0 0 0 0 1 1 0 0 0 | 7. | 7 .251903 .1594484 1 1 0 0 0 0 1 0 0 | 8. | 8 .0454795 .3190092 0 0 1 0 0 0 0 1 1 | 9. | 9 .1304665 .3436173 0 0 1 1 0 0 0 1 1 | +-----------------------------------------------------------------------+
```

Why do you want to store the neighboring unit ids?

Scott

On Sun, Aug 30, 2009 at 10:40 AM, David Torres<torresd@umich.edu> wrote:
```
```I copied and pasted your example, Scott, and got a couple of error messages
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)
```
```
*
*   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/
```