Validate a Spatial Weight Matrix Against CAR/SAR Theory
Source:R/spatial-validate.R
check_spatial_weight.RdRuns five theoretical compatibility checks on a spatial weight matrix \(M\) and reports any deviations from the standard requirements of the chosen model class. Returns a structured object summarising the results.
Usage
check_spatial_weight(
M,
spatial_model = c("car", "sar"),
verbose = TRUE,
sre_type = NULL
)Value
Invisibly, an object of class hbsaems_spatial_check with
components:
is_squareLogical.
has_zero_diagLogical.
is_symmetricLogical.
detected_styleCharacter:
"B","W", or"other".n_isolatedInteger: number of areas with no neighbours.
n_componentsInteger: number of connected components.
issuesCharacter vector of fatal errors.
warningsCharacter vector of soft warnings.
compatibleLogical: TRUE if matrix is theoretically compatible with
spatial_model.
Details
Theoretical requirements
For a CAR model (Besag 1974), the joint distribution
$$u \sim \mathcal{N}\bigl(0,\, \sigma^2 (D - \rho W)^{-1}\bigr)$$
is well-defined only when \(W\) is symmetric with
zero diagonal. By convention, \(W\) is taken as the
binary adjacency matrix (style = "B") so that
\(D = \mathrm{diag}(\text{row sums})\) has integer entries.
For a SAR model (Whittle 1954, Anselin 1988),
$$u = \rho W u + \varepsilon, \quad \varepsilon \sim \mathcal{N}(0, \sigma^2 I)$$
and \(W\) is conventionally row-standardised
(style = "W") so that the spatial autoregressive parameter
\(\rho\) can be interpreted as a normalised correlation in
\((-1, 1)\). Symmetry is not required for SAR.
Examples
# Build a small valid CAR matrix
M <- matrix(c(0, 1, 1, 0,
1, 0, 0, 1,
1, 0, 0, 1,
0, 1, 1, 0), 4, 4)
check_spatial_weight(M, spatial_model = "car")
#>
#> Spatial Weight Matrix Diagnostic
#> ---------------------------------
#> Square : TRUE
#> Zero diagonal : TRUE
#> Symmetric : TRUE
#> Detected style : B
#> Isolated areas : 0
#> Components : 1
#>
#> Matrix is theoretically compatible.
#>
# An asymmetric matrix flagged for CAR
M2 <- M; M2[1, 2] <- 2
check_spatial_weight(M2, spatial_model = "car", verbose = FALSE)$issues
#> [1] "CAR requires a symmetric weight matrix (Besag 1974). Detected asymmetry. Consider symmetrising via M <- (M + t(M)) / 2 or rebuild with build_spatial_weight(...)."