Computes Moran's I spatial autocorrelation statistic for a numeric vector given a spatial weights matrix.
Value
A list containing:
observed: The observed Moran's I statisticexpected: Expected value under null hypothesis of no spatial autocorrelation (typically -1/(n-1))sd: Standard deviation under null hypothesis
Details
Moran's I is defined as: $$I = \frac{n}{W} \frac{\sum_i \sum_j w_{ij}(x_i - \bar{x})(x_j - \bar{x})}{\sum_i (x_i - \bar{x})^2}$$
where n is the number of observations, W is the sum of all weights, and w_ij is the weight between locations i and j.
Under the null hypothesis of no spatial autocorrelation:
Expected value: E[I] = -1/(n-1)
Variance is computed using the analytical formula from Cliff and Ord (1981)
Examples
# Create example data
set.seed(42)
x <- rnorm(100)
coords <- cbind(runif(100), runif(100))
W <- buildSpatialNetwork(coords, method = "knn", k = 6)
# Calculate Moran's I
result <- moranI(x, W)
print(result)
#> $observed
#> [1] 0.001644026
#>
#> $expected
#> [1] -0.01010101
#>
#> $sd
#> [1] 0.05200438
#>