r-spatial / spdep

Spatial Dependence: Weighting Schemes and Statistics
https://r-spatial.github.io/spdep/
116 stars 26 forks source link

include.self option for mat2listw or way to specify self.included attribute on listw object #112

Closed rosieluain closed 1 year ago

rosieluain commented 1 year ago

I am using the function localG_perm. I have a matrix of binary weights, based on least-cost distances between locations, so I have been using mat2listw(matrix,style="B") to generate the listw object for localG_perm. The issue I am having is that I would like to compute G*, but I have not been able to find a way to specify self.included when generating the listw from my matrix. I see that there is a way to do this when beginning with an nb object - am I missing the way to do this with a matrix?

I attempted to get around the issue by specifying the self weights in the matrix (1s on the diagonal), but the returned vector has the attribute gstari as FALSE, so I believe the denominator in the local Getis equation would not be calculated correctly for G*.

I also thought it might be possible to specify self.included as an attribute after the listw object was generated. Is there a way to do this?

Thank you - any help would be much appreciated!

rsbivand commented 1 year ago

Not really missing a way to do this, as self least cost distance is by definition zero, and inverse distance then +Inf. You would have first to consider what G* means for your choice of weights.

rosieluain commented 1 year ago

Sorry, I should have clarified: I study fish, and my locations are in water. So I used least-cost distances to determine within-water distances between each pair of locations. I then selected all locations within a certain within-water distance of each location, and assigned a weight of 1 to each of these. So my weights were determined using distance, but are not themselves distances, and G* should still be valid.

Sorry that was confusing - I just wanted to point it out to say that it's not an option for me to determine the neighbours using a different method like dnearneigh (which would give me an object of class nb, which I could then apply include.self to).

Hope that makes sense! Thanks!

rsbivand commented 1 year ago

Yes, makes sense. Could you then create a toy workflow reproducing your setting? I think that after mat2listw(), you could extract the "nb"object and use include.self() on that, then converting back to "listw".

rosieluain commented 1 year ago

Yes, that worked perfectly - thank you!