moran_test: Moran's I Test for Spatial Autocorrelation

View source: R/moran_test.R

moran_testR Documentation

Moran's I Test for Spatial Autocorrelation

Description

This function provides a convenient wrapper to perform Moran's I test for spatial autocorrelation on a numeric vector. It seamlessly handles missing values (NA) by subsetting both the numeric vector and the spatial weights list simultaneously.

Usage

moran_test(
  x,
  listw,
  alternative = c("greater", "less", "two.sided"),
  mc = FALSE,
  nsim = 999,
  zero.policy = TRUE,
  na.rm = TRUE
)

Arguments

x

A numeric vector of the variable of interest (e.g., residuals, random effects, or raw data).

listw

A listw object containing spatial weights created by build_w or spdep.

alternative

A character string specifying the alternative hypothesis. Must be one of "greater" (default), "less", or "two.sided".

mc

Logical; if TRUE, performs Moran's I test using Monte Carlo permutations. Default is FALSE (analytical approach).

nsim

An integer specifying the number of permutations if mc = TRUE. Default is 999.

zero.policy

Logical; if TRUE, allows areas with no neighbors (isolates) to be included in the calculation. Default is TRUE.

na.rm

Logical; if TRUE, missing values in x are removed, and the corresponding rows/columns in the spatial weights are automatically subsetted. Default is TRUE.

Details

This function supports two approaches to testing the significance of Moran's I:

1. Analytical Approach (Randomization - Default)
When mc = FALSE, the function uses the analytical approach (specifically, the assumption of randomization). It computes the theoretical expectation and variance of Moran's I under the null hypothesis of no spatial autocorrelation. This method assumes that the observed values could have occurred in any spatial location with equal probability.
When to use: Use this approach when your dataset is relatively large and follows standard statistical assumptions. It is computationally fast and provides reliable asymptotic p-values for large N.

2. Monte Carlo Permutation Approach (mc = TRUE)
When mc = TRUE, the function calculates the p-value empirically. It randomly permutes (shuffles) the observed values x across the spatial units nsim times. For each permutation, it calculates a pseudo-Moran's I. The final p-value is the proportion of simulated Moran's I values that are as extreme as or more extreme than the observed Moran's I.
When to use: Use this approach when your dataset has a relatively small number of areas or when you want to avoid relying on asymptotic theory. Because it computes the p-value empirically without assuming a specific theoretical distribution for the Moran's I statistic, the Monte Carlo approach is highly robust and is widely recommended for evaluating MCMC outputs.

Value

A list with class htest containing the following components:

  • statistic: The value of the standard deviate of Moran's I.

  • p.value: The p-value of the test.

  • estimate: The value of the observed Moran's I, its expectation, and variance.

  • method: A character string indicating the type of test performed.

  • data.name: A character string giving the name(s) of the data.

Examples

# Load datasets
data(databeta)
data(weight_mat)

# Convert the spatial weights matrix to a 'listw' object
W_listw <- spdep::mat2listw(weight_mat, style = "W", zero.policy = TRUE)

# Perform Moran's I test (Analytical approach)
moran_test(x = databeta$y, listw = W_listw)

# Perform Moran's I test (Monte Carlo permutation approach)
moran_test(x = databeta$y, listw = W_listw, mc = TRUE, nsim = 99)

# Handling Missing Values automatically (na.rm = TRUE is default)
y_with_na <- databeta$y
y_with_na[c(2, 5)] <- NA
moran_test(x = y_with_na, listw = W_listw, na.rm = TRUE)


saeHB.Spatial.Beta documentation built on July 1, 2026, 5:07 p.m.