Skip to contents

Computes Moran's I spatial autocorrelation statistic for a numeric vector given a spatial weights matrix.

Usage

moranI(x, W, standardize = TRUE)

Arguments

x

Numeric vector of values (e.g., gene expression).

W

Square numeric matrix of spatial weights. Must have the same dimension as length(x).

standardize

Logical. If TRUE (default), row-standardize the weights matrix.

Value

A list containing:

  • observed: The observed Moran's I statistic

  • expected: 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)

References

Cliff, A.D. and Ord, J.K. (1981) Spatial Processes: Models & Applications. Pion.

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
#>