r-spatial / spdep

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

Example in `n.comp.nb()` not reproducible #146

Closed JosiahParry closed 4 weeks ago

JosiahParry commented 2 months ago

I am trying to find out the best way to identify the disconnected subgraphs from a nb object. In trying to explore how to convert an nb object to an igraph object I ran into the following issue.

library(sf)
#> Warning: package 'sf' was built under R version 4.3.1
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(spdep)
#> Loading required package: spData
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
columbus <- st_read(system.file("shapes/columbus.shp", package="spData")[1], quiet=TRUE)
col.gal.nb <- read.gal(system.file("weights/columbus.gal", package="spData")[1])
coords <- st_coordinates(st_centroid(st_geometry(columbus)))
col2 <- droplinks(col.gal.nb, 21)
res <- n.comp.nb(col2)

B <- as(nb2listw(col2, style="B", zero.policy=TRUE), "CsparseMatrix")
#> Error in as(nb2listw(col2, style = "B", zero.policy = TRUE), "CsparseMatrix"): no method or default for coercing "listw" to "CsparseMatrix"

Created on 2024-03-31 with reprex v2.0.2

rsbivand commented 2 months ago

Coercion method is in spatialreg.