Construct a spatial neighbours list for Pakistan administrative units
Source:R/neighbors.R
pk_neighbors.RdBuilds a contiguity or distance-based spatial neighbours structure
for direct use with spdep and spatialreg.
Arguments
- x
An
sfobject with polygon geometries, typically the output ofget_districts()orget_provinces().- style
Character. Neighbour definition:
"queen"(shared boundary point, default),"rook"(shared edge),"knn"(k nearest centroids), or"idw"(inverse distance weights).- k
Integer. Number of nearest neighbours. Required when
style = "knn".- disputed
Character. Controls whether Gilgit-Baltistan and/or Azad Jammu & Kashmir are included in the spatial weights structure:
"include"Default. Both units are treated as normal administrative units and participate in the neighbour graph.
"exclude_both"Both GB and AJK are dropped from
xbefore building the weights. The returneddataelement contains the subsettedsfobject."exclude_gb"Only Gilgit-Baltistan is dropped.
"exclude_ajk"Only Azad Jammu & Kashmir is dropped.
Value
A named list with the following elements:
nbAn
spdepnbobject containing the neighbour relationships. Each element is an integer vector of neighbour indices.listwA row-standardised
spdeplistwobject (style ="W",zero.policy = TRUE), ready forspdep::moran.test(),spdep::localmoran(),spatialreg::lagsarlm(), and related functions.dataThe
sfobject used to build the weights. Identical to the inputxwhendisputed = "include". When any units are excluded this is the subsettedsf, always in row-for-row alignment withnbandlistw. Always usew$datarather than the original input when mapping or attaching analysis results.
Disputed units
GB and AJK are present in the OCHA/HDX boundary data and share polygon
edges with KP and Punjab, so under disputed = "include" (the default)
they participate in the neighbour graph as normal administrative units.
Use "exclude_gb", "exclude_ajk", or "exclude_both" to drop one or
both units before building the weights. The returned data element is
always the sf object actually used to build the weights — identical to
the input when disputed = "include", subsetted otherwise. Always use
w$data rather than the original input for all subsequent analysis and
mapping, regardless of which disputed option is chosen.
Examples
# \donttest{
districts <- get_districts()
# Default: all units included
w <- pk_neighbors(districts)
moran_result <- spdep::moran.test(w$data$area_km2, w$listw)
print(moran_result)
#>
#> Moran I test under randomisation
#>
#> data: w$data$area_km2
#> weights: w$listw
#>
#> Moran I statistic standard deviate = 9.7714, p-value < 2.2e-16
#> alternative hypothesis: greater
#> sample estimates:
#> Moran I statistic Expectation Variance
#> 0.455579101 -0.006289308 0.002234218
#>
# Rook contiguity
w_rook <- pk_neighbors(districts, style = "rook")
# K-nearest neighbours (k = 5)
w_knn <- pk_neighbors(districts, style = "knn", k = 5)
#> Warning: st_centroid assumes attributes are constant over geometries
# Inverse distance weights
w_idw <- pk_neighbors(districts, style = "idw")
#> Warning: st_centroid assumes attributes are constant over geometries
# Exclude both GB and AJK for sensitivity analysis
w_excl <- pk_neighbors(districts, disputed = "exclude_both")
# Use w_excl$data — not the original districts — for subsequent analysis
moran_excl <- spdep::moran.test(w_excl$data$area_km2, w_excl$listw)
# Exclude only Gilgit-Baltistan
w_no_gb <- pk_neighbors(districts, disputed = "exclude_gb")
# Exclude only Azad Jammu & Kashmir
w_no_ajk <- pk_neighbors(districts, disputed = "exclude_ajk")
# }