14.9 Distance-Based Neighbors

  • Distance-based neighbours can be constructed using dnearneigh, with a distance band with lower d1= and upper d2= bounds controlled by the bounds= argument
  • The knearneigh function for -nearest neighbours returns a knn object, converted to an nb object using knn2nb
  • Computation speed boost through dbscan package
  • The nbdists function returns the length of neighbour relationship edges
coords |> 
    knearneigh(k = 1) |> 
    knn2nb() |> 
    nbdists(coords) |> 
    unlist() |> 
    summary()
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   246.5  6663.4  8538.0  8275.1 10123.9 17978.8

Here the largest first nearest neighbour distance is just under 18 km, so using this as the upper threshold gives certainty that all units will have at least one neighbour.

# maybe no neighbors
coords |> dnearneigh(0, 16000) -> nb_d16

# at least one neighbor
coords |> dnearneigh(0, 18000) -> nb_d18
  • Adding 300 m to the threshold gives us a neighbour object with no no-neighbour units, and all units can be reached from all others across the graph.
# connected graph
coords |> dnearneigh(0, 18300) -> nb_d183

It is possible to control the numbers of neighbours directly using -nearest neighbours, either accepting asymmetric neighbours

coords |> knearneigh(k = 6) -> knn_k6

# asymmetrical
knn_k6 |> knn2nb() -> nb_k6

# symmetrical
knn_k6 |> knn2nb(sym = TRUE) -> nb_k6s