Polygon-aware Moran's I on a value column
Source:R/tps_spatial_advanced.R
morie_tps_polygon_morans_i.RdAccepts an sf object (recommended) carrying neighbourhood
polygons and a numeric value column, computes polygon centroids
via sf::st_centroid, then runs Moran's I with a k-NN row-
standardised weights matrix on those centroids. Falls back to a
data.frame carrying precomputed centroid columns when
sf is unavailable.
Usage
morie_tps_polygon_morans_i(
polygons,
value_col,
ds_name = "NeighbourhoodCrimeRates",
k_neighbours = 5L,
centroid_lat_col = "lat",
centroid_lon_col = "lon"
)Examples
set.seed(2026)
polys <- data.frame(
HOOD_ID = letters[1:16],
lat = rep(43.6 + (0:3) * 0.02, 4),
lon = rep(-79.4 + (0:3) * 0.02, each = 4),
ASSAULT_RATE_2024 = rpois(16, 30)
)
morie_tps_polygon_morans_i(polys, value_col = "ASSAULT_RATE_2024",
centroid_lat_col = "lat", centroid_lon_col = "lon")
#> Polygon Moran's I -- NeighbourhoodCrimeRates (ASSAULT_RATE_2024)
#> ================================================================
#> Call: morie_tps_polygon_morans_i(value_col=ASSAULT_RATE_2024, k=5)
#>
#> Variable ASSAULT_RATE_2024
#> Polygons 16
#> k-NN 5
#> Moran's I -0.13028
#> Expected I -0.066667
#> Var(I) 0.00077311
#> z-score -2.2879
#> p-value (two-sided) 0.022141
#>
#> Polygon Moran's I on 'ASSAULT_RATE_2024' across 16 polygon(s) with a k=5-NN weights matrix: I = -0.130 (z = -2.29, two-sided p = 0.02214). Positive I means neighbouring polygons share similar values (spatial autocorrelation in the underlying rate).