14.7 Contiguous Neighbors

For each observation, the poly2nb function checks whether

  • at least one (queen = TRUE)
  • at least two (“rook”, queen = FALSE)

points are within snap distance of each other.

pol_pres15 |> poly2nb(queen = TRUE) -> nb_q
print(nb_q)
## Neighbour list object:
## Number of regions: 2495 
## Number of nonzero links: 14242 
## Percentage nonzero weights: 0.2287862 
## Average number of links: 5.708216
  • s2 spherical coordinates are used by default
  • symmetric relationships assumed
  • row.names may be customized

14.7.1 Connected

Are the data connected? (Some model estimation techniques do not support graphs that are not connected.)

(nb_q |> n.comp.nb())$nc
# result: 1 for TRUE (more than one for FALSE)
verbose code
#library(Matrix, warn.conflicts = FALSE)
#library(spatialreg, warn.conflicts = FALSE)
nb_q |> 
    nb2listw(style = "B") |> 
    as("CsparseMatrix") -> smat
#library(igraph, warn.conflicts = FALSE)
smat |> graph.adjacency() -> g1
## Warning: `graph.adjacency()` was deprecated in igraph 2.0.0.
## ℹ Please use `graph_from_adjacency_matrix()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
g1 |> count_components()
## [1] 1