Skip to article frontmatterSkip to article content

Learning question: Calculate local Moran components and compare high–high, low–low and dissimilar-neighbour quadrants. Synthetic data keep the calculation inspectable. A quadrant label alone is not proof of local statistical significance.

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 spatial lag of standardised values and their local Moran contributions. Which units have positive and negative contributions?

# TODO: standardise values with population SD, compute W @ standardized values and I_i.

TODO 2: Classify the four sign-based quadrants and plot local contributions on the 6×6 synthetic grid. State why quadrant membership is not the same as significance.

# TODO: compute HH/LL/HL/LH labels from standard and lag; plot local_i on a grid.

Interpretation

Local Moran contributions identify locations that contribute to patterns of local association. Formal inference requires local permutation distributions and an explicit policy for multiple testing; these are not supplied by a quadrant map.

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.

Open the worked solution