Learn R Programming

SVG (version 1.0.0)

moranI: Calculate Moran's I Statistic

Description

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

Usage

moranI(x, W, standardize = TRUE)

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

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.

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

Run this code
# Create example data
set.seed(42)
x <- rnorm(100)
coords <- cbind(runif(100), runif(100))

# \donttest{
# Calculate Moran's I (requires RANN package)
if (requireNamespace("RANN", quietly = TRUE)) {
    W <- buildSpatialNetwork(coords, method = "knn", k = 6)
    result <- moranI(x, W)
    print(result)
}
# }

Run the code above in your browser using DataLab