r-spatial / spdep

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

mat2listw sends an error when printing the results in the console #136

Closed guiblanchet closed 4 months ago

guiblanchet commented 7 months ago

This is a slightly tricky problem because, if I run the example code of the function :

columbus <- st_read(system.file("shapes/columbus.shp", package="spData")[1], quiet=TRUE)
col005 <- dnearneigh(st_coordinates(st_centroid(st_geometry(columbus),
 of_largest_polygon=TRUE)), 0, 0.5, as.character(columbus$NEIGNO))
summary(col005)
col005.w.mat <- nb2mat(col005, zero.policy=TRUE)
col005.w.b <- mat2listw(col005.w.mat)

There are no error. However, when calling the object col005.w.b, there is the following error message :

Error in print.listw(x) : regions with no neighbours found, use zero.policy=TRUE

Which is odd given that col005.w.mat was built with zero.policy=TRUE.

Note also that two warning messages show up when running col005.w.b <- mat2listw(col005.w.mat) :

Warning messages: 1: In mat2listw(col005.w.mat) : style is M (missing); style should be set to a valid value 2: In mat2listw(col005.w.mat) : no-neighbour observations found, set zero.policy to TRUE; this warning will soon become an error

The first seems to be fine while the second may hint to where the problems may be in the mat2listw function.

rsbivand commented 7 months ago

Please see #135 and https://github.com/r-spatial/discuss/issues/62 for ongoing discussions. Maybe try the development version by installing from github, if you do, please report back.

rsbivand commented 7 months ago

With the github version:

> col005.w.mat <- nb2mat(col005, zero.policy=TRUE)
> col005.w.b <- mat2listw(col005.w.mat)
Warning messages:
1: In mat2listw(col005.w.mat) :
  style is M (missing); style should be set to a valid value
2: In mat2listw(col005.w.mat) :
  no-neighbour observations found, set zero.policy to TRUE;
this warning will soon become an error
> ?nb2mat
> col005.w.mat <- nb2mat(col005, style="W", zero.policy=TRUE)
> col005.w.b <- mat2listw(col005.w.mat, style="W", zero.policy=TRUE)
> col005.w.b
Characteristics of weights list object:
Neighbour list object:
Number of regions: 49 
Number of nonzero links: 170 
Percentage nonzero weights: 7.080383 
Average number of links: 3.469388 
4 regions with no links:
1005 1006 1008 1043
8 disjoint connected subgraphs

Weights style: W 
Weights constants summary:
   n   nn S0       S1       S2
W 45 2025 45 36.82596 187.3873
rsbivand commented 4 months ago

Resolved here https://github.com/adeverse/adespatial/commit/fa214ac4c74fd7ac4e8b00871746f4ba2fa395a6