nbmatrix: Construct Neighbourhood Matrix from Polygons

View source: R/nbmat.R

nbmatrixR Documentation

Construct Neighbourhood Matrix from Polygons

Description

Building a neighbourhood matrix based on regions (polygons) with contiguous boundaries

Usage

nbmatrix(
  polygon_sf, 
  ID_col,
  row.names = NULL,
  snap = NULL,
  queen = TRUE,
  weights_style = "W",
  weights_zeropolicy = NULL,
  verbose = FALSE
  )

Arguments

polygon_sf

sf object with polygons

ID_col

Column of polygon_sf with unique ID of each polygon

row.names

row.names for the sf object, passed to spdep::poly2nb

snap

Snapping distance, passed to spdep::poly2nb

queen

If TRUE, a single shared boundary point meets the contiguity condition; passed to spdep::poly2nb

weights_style

weights_style can take values “W”, “B”, “C”, “U”, “minmax” and “S”; passed to the style argument of spdep::nb2listw

weights_zeropolicy

if FALSE stop with error for any empty neighbour sets, if TRUE permit the weights list to be formed with zero-length weights vectors; passed to the zero.policy argument of spdep::nb2listw

verbose

bool argument which indicates whether progress messages are displayed

Details

The function is based on spdep::poly2nb, which creates neighbours lists, and spdep::nb2listw, which computes spatial weights for neighbours lists. Please consult the corresponding documentation for a more detailed description of the parameters. The input is a sf object (spatial data frame) and the results (contained in a {nbmatrix-class} object) include 1) a nb list (poly2nb result), 2) a listw object (spatial weights), 3) a data.frame with a linear neighbor matrix, 4) a data.frame with the number of neighbors, and other elements (statistics etc.)

Value

object of class nbmatrix-class

Author(s)

Thomas Wieland

References

Cliff AD, Ord JK (1981) Spatial Processes: Models & Applications. https://archive.org/details/spatialprocesses0000clif

GeoDa Center (2026) GeoDa. An Introduction to Spatial Data Science. https://geodacenter.github.io/

Wieland T (2020) Flatten the Curve! Modeling SARS-CoV-2/COVID-19 Growth in Germany at the County Level. REGION 7(2), 43–83. \Sexpr[results=rd]{tools:::Rd_expr_doi("https://doi.org/10.18335/region.v7i2.324")}

Wieland T (2022) Spatial patterns of excess mortality in the first year of the COVID-19 pandemic in Germany. European Journal of Geography 13(4), 18-33. \Sexpr[results=rd]{tools:::Rd_expr_doi("https://doi.org/10.48088/ejg.t.wie.13.4.018.033")}

See Also

nbstat, moran, getisord, gstar

Examples

data(RKI_Corona_counties)
# German counties (Source: Robert Koch Institute)

Corona_nbmat <- 
  nbmatrix (
    RKI_Corona_counties, 
    ID_col="AGS",
    verbose = TRUE
  )
# Creating neighborhood matrix

Corona_nbstat <- nbstat(
  Corona_nbmat,
  link_data = RKI_Corona_counties, 
  ID_col = "AGS", 
  data_col = "EWZ", 
  func = "sum",
  verbose = TRUE
  )
# Sum of population (EWZ) of neighboring counties

plot(
  Corona_nbstat,
  main = "No. of inhabitants"
)
# Plot simple map of "EWZ"

swash documentation built on Oct. 18, 2026, 5:06 p.m.