Sweep polygon Moran's I across (category x year)
Source:R/tps_spatial_advanced.R
morie_tps_moran_sweep_heatmap.RdLoops morie_tps_polygon_morans_i over a grid of
value-column prefixes and years, returning the resulting matrix
of Moran's I values for downstream visualisation as a heatmap.
Usage
morie_tps_moran_sweep_heatmap(
polygons,
category_prefixes = NULL,
years = NULL,
k_neighbours = 5L,
ds_name = "NeighbourhoodCrimeRates",
centroid_lat_col = "lat",
centroid_lon_col = "lon"
)Arguments
- polygons
An sf object or data.frame with centroid columns and per-year value columns.
- category_prefixes
Character vector of column prefixes. Defaults to the 9 published TPS rate categories.
- years
Integer vector of years. Defaults to 2014:2024.
- k_neighbours
k for the k-NN weights graph passed down.
- ds_name
Tag for the result title.
- centroid_lat_col, centroid_lon_col
Centroid column names forwarded to
morie_tps_polygon_morans_i.
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_2023 = rpois(16, 30),
ASSAULT_RATE_2024 = rpois(16, 32),
HOMICIDE_RATE_2023 = rpois(16, 2),
HOMICIDE_RATE_2024 = rpois(16, 2)
)
morie_tps_moran_sweep_heatmap(polys,
category_prefixes = c("ASSAULT_RATE", "HOMICIDE_RATE"),
years = c(2023L, 2024L),
centroid_lat_col = "lat", centroid_lon_col = "lon")
#> Moran's I sweep -- 2 categories x 2 years
#> =========================================
#> Call: morie_tps_moran_sweep_heatmap(cats=2, years=2)
#>
#> Categories 2
#> Years 2023-2024
#> Min I -0.13028
#> Max I 0.090545
#>
#> Swept polygon Moran's I across 2 category prefix(es) and 2 year(s) (2023-2024). Min I = -0.130, max I = +0.091; positive values indicate spatial autocorrelation in the corresponding crime rate across neighbourhoods.