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   statalist <[email protected]>
Subject   Re: st: set minimum neighbors using spmat and idistance
Date   Thu, 26 Sep 2013 14:09:28 -0400

Thank you Rafal, that was very helpful in creating the correct matrix;
it eliminated all the islands!

But now I am having a problem creating spatially lagged variables from
that matrix. My syntax is spmat lag wvar matrix1 var
which seems to work, but many of my created spatial lags are all
missing. From what I can tell the spmat lag command
does not work if there are missing values in the variable. I tried
changing the missing values into a very low number
(0.0001) but when I tested that on a variable that had worked
previously, it changed the generated values of spatially weighted
variables.

Is there a work around for this? Or any way to generate spatially
lagged variables that just skips the missing values?

Any help would be greatly appreciated!!

Thank you!

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