从0开始学习R语言--Day41--Moran‘s I

发布于:2025-07-09 ⋅ 阅读:(20) ⋅ 点赞:(0)

在处理带有空间特征的数据,我们往往都直接一股脑地处理数据点,但很多时候,空间上的信息对于处理后续衍生出来的问题会有很大帮助,例如对于城市里大小县城的发展情况,只知道单一县城的经济发展曲线,很难解释一些拐点和突然的攀升,而如果知道相邻县城存在经济发展飞快的例子,可能就是被带动了经济水平;亦或者是在处理社交网络的好有问题时,只知道谁和谁是朋友(类似于空间矩阵),是无法推断出经济收入相似的推论的,所以说,空间属性与数据本身相结合去分析,能有助于剖析更深层次的结论。

以下是一个例子:

library(spdep)
library(sf)

# 生成模拟数据
set.seed(123)
n_points <- 100
coords <- data.frame(
  x = runif(n_points, 0, 100),
  y = runif(n_points, 0, 100)
)

# 模拟空间自相关变量
dist_matrix <- as.matrix(dist(coords))
cov_matrix <- exp(-dist_matrix / 30)
sim_data <- MASS::mvrnorm(1, mu = rep(0, n_points), Sigma = cov_matrix)
coords$value <- sim_data

# 转换为 sf 对象(使用平面坐标 CRS)
points_sf <- st_as_sf(coords, coords = c("x", "y"), crs = NA)  # 无 CRS

# 计算空间权重(k=5 近邻)
knn <- knn2nb(knearneigh(as.matrix(coords[, c("x", "y")]), k = 5))
sp_weights <- nb2listw(knn, style = "W")

# Moran's I 检验
moran_test <- moran.test(coords$value, sp_weights)
print(moran_test)

# 可视化
plot(st_geometry(points_sf), pch = 16, col = "blue", main = "Simulated Spatial Data")
plot(knn, coords = as.matrix(coords[, c("x", "y")]), add = TRUE, col = "red")

输出:

	Moran I test under randomisation

data:  coords$value  
weights: sp_weights    

Moran I statistic standard deviate = 8.324, p-value < 2.2e-16
alternative hypothesis: greater
sample estimates:
Moran I statistic       Expectation          Variance 
      0.476105070      -0.010101010       0.003411721 

结果显示Moran I值为0.476大于0,数据在空间上呈正相关,而p远小于0.05,说明统计高度显著,而图像则说明这些数据高度聚集,在分析时可以用分组或边际的思路。