Global spatial autocorrelation: weights and Moran’s I
Learning question: Understand what a neighbourhood definition means and compare an observed global statistic with an explicit permutation null. This self-contained pilot uses synthetic areal units; it does not replace the legacy Singapore transit worked example.
Prerequisites: NumPy and Matplotlib. Concept links: Part 1, Chapter 6. Estimated effort: 20–30 minutes. No private data needed.
This is a guided methods lab; the Malaysia income and Singapore transit applications remain separate empirical investigations.
Companion notebook: View the worked solution.
1. Set up observations and spatial weights¶
Rows of the rook-contiguity weight matrix are standardised to sum to one. The units form a fictional regular grid, not real administrative areas.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(5230)
n_side = 6
n = n_side**2
coords = np.array([(x,y) for y in range(n_side) for x in range(n_side)])
# Synthetic neighbouring-area pattern: a smooth geographic gradient plus noise.
values = 15 + 1.8*coords[:,0] + 1.2*coords[:,1] + rng.normal(0, 1, n)
W = np.zeros((n,n))
for i, (x,y) in enumerate(coords):
for j, (xx,yy) in enumerate(coords):
if abs(x-xx)+abs(y-yy)==1: W[i,j]=1
W = W/W.sum(axis=1,keepdims=True)
z = values-values.mean()
S0=W.sum()
observed = (n/S0)*(z@W@z)/(z@z)
print("Global Moran I:",round(observed,3))TODO 1: Calculate the row-standardised spatial lag of centred values and draw a Moran scatterplot.
# TODO: lag = W @ z, plot z vs lag; label axes.TODO 2: Generate 999 permutation statistics by shuffling the centred values while keeping W fixed. Report the upper-tail Monte Carlo p-value.
# TODO: permute z 999 times and recompute Moran I; use (1 + count >= observed)/(999 + 1).Interpretation¶
The permutation distribution is conditional on the chosen values and weights. Global Moran’s I does not explain the mechanism producing spatial similarity; a different neighbour definition may change the result.
Reflect: Which choices would need revisiting for real geographical data?
Check your understanding¶
Try the exercises and write down your interpretation before comparing your work with the complete answers.