Weight Matrices
Constructing, standardizing, inspecting and reading weight matrices.
SpatialEconometrics.SpatialWeights.SpatialConnectivity — Type
SpatialConnectivitySummary of a weight matrix's structure, returned by spatial_connectivity. Worth looking at before fitting anything.
Fields
N,n_nonzero,density: size and sparsity.avg_neighbors,min_neighbors,max_neighbors: the degree distribution.n_islands: units with no neighbours at all.is_symmetric: whetherW == W'. Row-standardizing a symmetric contiguity matrix generally destroys symmetry, sofalsehere is normal and not a problem.spectral_radius: largest eigenvalue magnitude; 1 for a row-standardized matrix.
SpatialEconometrics.SpatialWeights.band_weights — Function
band_weights(coords, d_min, d_max; row_standardize=true)Binary weights: w_ij = 1 if d_min < d_ij ≤ d_max, else 0.
SpatialEconometrics.SpatialWeights.block_diagonal_weights — Function
block_diagonal_weights(W_list)Combine a vector of sub-region weight matrices into one block-diagonal matrix.
SpatialEconometrics.SpatialWeights.distance_weights — Function
distance_weights(coords, cutoff; decay=:inverse, alpha=1.0, row_standardize=true)Build distance-decay spatial weights. Pairs beyond cutoff get zero weight.
Decay functions:
:inverse—1 / d^alpha:negative_exponential—exp(-alpha * d):power—d^(-alpha)(same as inverse, explicit name):bisquare—(1 - (d/cutoff)^2)^2
SpatialEconometrics.SpatialWeights.knn_weights — Function
knn_weights(coords, k; binary=false, row_standardize=true, symmetrize=false)Build a spatial weight matrix from k-nearest neighbors. coords is N×D (rows = observations). Returns a sparse N×N matrix.
SpatialEconometrics.SpatialWeights.minmax_standardize — Function
minmax_standardize(W) -> SparseMatrixCSCCopy of W divided by the smaller of its largest row sum and largest column sum. See minmax_standardize! for the in-place version.
A middle course between row_standardize and spectral_standardize: it keeps relative connectedness while being cheaper than an eigenvalue computation, which matters for large W.
SpatialEconometrics.SpatialWeights.minmax_standardize! — Function
minmax_standardize!(W)MinMax standardization: W / (min(r_max) + max(r_max)) where r_max is the vector of max absolute row sums and max absolute column sums.
SpatialEconometrics.SpatialWeights.read_gal — Function
read_gal(path; ids=nothing) → (W, ids)Read a GeoDa GAL contiguity weights file. Returns the row-standardized sparse weight matrix and the observation ID vector, where row i of W corresponds to ids[i].
The ordering comes from the file's observation records, in record order. Pass ids to impose an external table's ordering instead. A neighbour id with no observation record raises an error.
SpatialEconometrics.SpatialWeights.read_gwt — Function
read_gwt(path; ids=nothing) → (W, ids)Read a GeoDa GWT (general weights) file. Returns the sparse weight matrix and the observation ID vector, where row i of W corresponds to ids[i].
A GWT file is only an edge list and carries no ordering of its own, so the ids found in it are sorted — numerically when they all parse as integers, else lexicographically. Pass ids to impose an external table's ordering instead.
SpatialEconometrics.SpatialWeights.row_standardize — Function
row_standardize(W) -> SparseMatrixCSCRow-standardized copy of W: every row with at least one neighbour sums to 1. See row_standardize! for the in-place version.
This is the conventional normalization, and most of this package assumes it. It makes W*y a neighbourhood average, bounds the spatial parameter inside (-1, 1), and makes Moran's I equal the slope in a Moran scatter plot. Rows with no neighbours stay all-zero.
SpatialEconometrics.SpatialWeights.row_standardize! — Function
row_standardize!(W)Row-standardize a sparse weight matrix in place: each row sums to 1. Rows with no neighbors are left as zeros.
SpatialEconometrics.SpatialWeights.spatial_connectivity — Function
spatial_connectivity(W)Compute diagnostic statistics for a spatial weight matrix.
SpatialEconometrics.SpatialWeights.spectral_standardize — Function
spectral_standardize(W) -> SparseMatrixCSCCopy of W divided by its spectral radius. See spectral_standardize! for the in-place version.
Bounds the spatial parameter in (-1, 1) as row-standardization does, but without rescaling rows relative to one another, so differences in connectedness between units are preserved. Preferable when the number of neighbours is itself meaningful; the cost is that W*y is no longer a simple neighbourhood average.
SpatialEconometrics.SpatialWeights.spectral_standardize! — Function
spectral_standardize!(W)Divide all entries by the spectral radius (largest eigenvalue magnitude).
SpatialEconometrics.SpatialWeights.write_gal — Function
write_gal(path, W, ids)Write a weight matrix in GeoDa GAL format.
SpatialEconometrics.SpatialWeights.write_gwt — Function
write_gwt(path, W, ids; threshold=0.0)Write a weight matrix in GeoDa GWT format.