| moran_test | R Documentation |
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.
moran_test(
x,
listw,
alternative = c("greater", "less", "two.sided"),
mc = FALSE,
nsim = 999,
zero.policy = TRUE,
na.rm = TRUE
)
x |
A numeric vector of the variable of interest (e.g., residuals, random effects, or raw data). |
listw |
A |
alternative |
A character string specifying the alternative hypothesis. Must be one of |
mc |
Logical; if |
nsim |
An integer specifying the number of permutations if |
zero.policy |
Logical; if |
na.rm |
Logical; if |
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.
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.
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.