Learn R Programming

SVG (version 1.0.0)

moranI_test: Moran's I Test for Spatial Autocorrelation

Description

Performs a statistical test for spatial autocorrelation using Moran's I. Returns the test statistic, expected value, standard deviation, and p-value.

Usage

moranI_test(
  x,
  W,
  alternative = c("greater", "less", "two.sided"),
  standardize = TRUE
)

Value

A named numeric vector with components:

  • observed: Observed Moran's I

  • expected: Expected Moran's I under null

  • sd: Standard deviation under null

  • p.value: P-value from normal approximation

Arguments

x

Numeric vector of values.

W

Square numeric matrix of spatial weights.

alternative

Character string specifying the alternative hypothesis. One of "greater" (default), "less", or "two.sided".

  • "greater": Test for positive spatial autocorrelation (similar values cluster together)

  • "less": Test for negative spatial autocorrelation (dissimilar values are neighbors)

  • "two.sided": Test for any spatial autocorrelation

standardize

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

Examples

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

# \donttest{
# Test for spatial autocorrelation (requires RANN package)
if (requireNamespace("RANN", quietly = TRUE)) {
    W <- buildSpatialNetwork(coords, method = "knn", k = 6)
    result <- moranI_test(x, W)
    print(result)
}
# }

Run the code above in your browser using DataLab