Skip to main content

Spatial Autocorrelation

Spatial autocorrelation asks whether values attached to nearby locations are more similar or dissimilar than a declared reference pattern. It is not ordinary correlation with coordinates, and it is not evidence that one place caused another.

Prerequisites​

Complete road networks and map matching and spatial joins, overlay, and dissolve. You should be able to state a feature's spatial support and CRS before defining “nearby.”

Learning goals​

  • Build a spatial-weight matrix from an explicit neighborhood and boundary rule.
  • Compute global Moran's II and its expectation under randomization.
  • Define a permutation null hypothesis and a one- or two-sided alternative before testing.
  • Separate exploratory dependence evidence from prediction and causal claims.

Weights are part of the question​

For observations xix_i, centered values zi=xi−xˉz_i=x_i-\bar{x}, weights wijw_{ij}, and S0=∑i∑jwijS_0=\sum_i\sum_jw_{ij},

I=nS0∑i∑jwijzizj∑izi2.I=\frac{n}{S_0}\frac{\sum_i\sum_j w_{ij}z_i z_j}{\sum_i z_i^2}.

This lesson uses a fixed 3×33\times3 lattice, binary rook contiguity, no self-neighbors, and no wraparound at the outer boundary. Diagonal cells are not neighbors. The formula uses the binary matrix directly; row-standardizing it would answer a different weighted question. Islands need an explicit policy rather than an invented neighbor.

The null fixes both the observed values and WW, then assigns the values exchangeably to the nine cells. Under this randomization null, E[I]=−1/(n−1)=−0.125E[I]=-1/(n-1)=-0.125. The worked test is one-sided for positive dependence, uses 999 seeded permutations, and includes the observed arrangement through the +1+1 correction. A small pseudo-pp value is evidence against that null, conditional on the chosen weights and exchangeability—not a probability that the null is true.

Worked example​

import numpy as np

values = np.array([1, 1, 2, 1, 2, 3, 2, 3, 3], dtype=float)
cells = [(row, column) for row in range(3) for column in range(3)]
weights = np.array([
[int(abs(row - other_row) + abs(column - other_column) == 1)
for other_row, other_column in cells]
for row, column in cells
], dtype=float)

def moran_i(sample):
centered = sample - sample.mean()
cross_products = (weights * np.outer(centered, centered)).sum()
return len(sample) / weights.sum() * cross_products / (centered @ centered)

observed = moran_i(values)
rng = np.random.default_rng(57)
permuted = np.array([moran_i(rng.permutation(values)) for _ in range(999)])
p_positive = (1 + np.count_nonzero(permuted >= observed)) / (len(permuted) + 1)
print(f"n={len(values)} directed_weight_sum={weights.sum():.0f} islands={(weights.sum(axis=1) == 0).sum()}")
print(f"moran_i={observed:.3f} randomization_expectation={-1 / (len(values) - 1):.3f}")
print(f"permutations={len(permuted)} alternative=positive pseudo_p={p_positive:.3f} seed=57")

Expected output:

n=9 directed_weight_sum=24 islands=0
moran_i=0.500 randomization_expectation=-0.125
permutations=999 alternative=positive pseudo_p=0.004 seed=57

The synthetic field has known smooth dependence, so the result is a verifier target. It does not estimate the strength of a real process.

Limitations and common mistakes​

  • Selecting distance bands after inspecting significance changes the inferential procedure.
  • Global II can hide distinct local clusters and outliers; many local tests require multiplicity control.
  • Exchangeability can be implausible under trends, unequal measurement error, or constrained assignments.
  • Dependence in a response differs from dependence in model residuals.
  • Neither positive II nor a small pseudo-pp value establishes a causal spillover.

Practice​

Recompute S0S_0 after adding queen-contiguity diagonals. Before looking at the result, write how the estimand and null distribution change, and decide whether a one-sided alternative is still justified.

Next steps​

Next, distinguish values attached to areas from event locations in hotspots and point patterns.

Primary sources​

Checked 2026-10-18: P. A. P. Moran, Notes on Continuous Stochastic Phenomena; PySAL esda, global Moran's I and permutation inference; PySAL libpysal, spatial weights documentation.