Skip to contents

Accepts 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"
)

Arguments

polygons

An sf object, or a data.frame with centroid columns.

value_col

Column to test for spatial autocorrelation.

ds_name

Tag for the result title.

k_neighbours

k for the k-NN weights graph.

centroid_lat_col, centroid_lon_col

Names of the centroid columns when polygons is a plain data.frame.

Value

A named list with moran_I, z_score, p_value, n.

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).