Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: set minimum neighbors using spmat and idistance


From   Lyndsay Boggess <[email protected]>
To   [email protected]
Subject   Re: st: set minimum neighbors using spmat and idistance
Date   Wed, 25 Sep 2013 17:06:57 -0400

Thank you Rafal, that worked out perfectly!

On Wed, Sep 25, 2013 at 3:51 PM, Rafal Raciborski, StataCorp
<[email protected]> wrote:
> Lyndsay Boggess <[email protected]> has a question about the user-written
> -spmat- command:
>
>> I'm having trouble creating inverse distance weights on a population of
>> 2054 census tracts. I use the following code in order to create the weights
>> for only those tracts within 2 miles (a theoretically derived distance
>> pertaining to travelling to crime):
>
>> spmat idistance laweights longitude latitude, id(tract)
>> dfunction(dhaversine, miles) vtruncate(1/2)
>
>> This leaves me with 38 islands, which I think is the problem because when I
>> try to create spatial lag variables, my variables end up with all values of
>> zero. Is there a way to force spmat to make sure that each tract has a
>> minimum of 1 neighbor even if it requires to extend the distance
>> beyond two miles?
>
> Lyndsay can create the desired spatial-weighting matrix by combining two
> spatial-weighting matrices in the way I illustrate below.  For my example,
> I will use the ancillary pollute.dta dataset that comes with the sppack suite.
>
> First, I create a spatial-weighting matrix similar to Lyndsay's:
>
>   . use pollute, clear
>   . spmat idistance mat1 longitude latitude, id(id) df(dhav, mi) vtr(.01)
>
> We can see that the spatial-weighting matrix contains 26 islands:
>
>   . spmat su mat1, links detail
>
> <snip>
> Tabulation of links
> -------------------------
>  # of links |        Obs
> ------------+------------
>           0 |         26
>           1 |         20
>           2 |         20
> <snip>
>
> Next, I use an undocumented -knn()- option of -spmat idistance- to create
> a k-nearest neighbor (KNN) spatial-weighting matrix:
>
>   . spmat idistance mat2 longitude latitude, id(id) df(dhav, mi) knn(1)
>
> That command creates a spatial-weighting matrix in which the nearest neighbor
> of each spatial unit is weighted by its inverse distance and all others
> receive zero weights.
>
> Now I extract the two spatial-matrices from the spmat objects into Mata
>
>   . spmat get mat1 W1
>   . spmat get mat2 W2
>
> and loop through the rows of matrix W1 replacing rows that have all 0's with
> the corresponding rows from matrix W2 that contain the nearest-neighbor:
>
>   . mata:
>   :         N = rows(W1)
>   :         for (i=1; i<=N; i++) {
>   >                 if (sum(W1[i,.]:==0) == N) W1[i,.] = W2[i,.]
>   >         }
>   : end
>
> Next, I put matrix W1 back into the spmat object mat1.  We can see the new
> matrix does not have any islands:
>
>   . spmat put mat1 W1, replace
>   . spmat su mat1, links detail
>
> <snip>
> Tabulation of links
> -------------------------
>  # of links |        Obs
> ------------+------------
>           1 |         46
>           2 |         20
> <snip>
>
> Finally, I drop the unneeded objects from Mata memory:
>
>   . spmat drop mat2
>   . mata mata drop W1 W2 i N
>
> -- Rafal
> [email protected]
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index