亲爱的人群
问题
我尝试使用 nfc、pgirmess、SpatialPack 和 spdep 包计算空间相关图。但是,我很难定义距离的起点和终点。我只对较小距离的空间自相关感兴趣,但在较小的箱中。此外,由于光栅非常大(1.8 兆像素),除了 SpatialPack 之外,我遇到了这些包的内存问题。
所以我尝试使用包光栅中的函数 Moran 生成我自己的代码。但我一定有一些错误,因为完整数据集的结果与其他包的结果有些不同。如果我的代码没有错误,它至少可以帮助其他有类似问题的人。
问题
我不确定我的焦点矩阵是否错误。您能否告诉我是否需要合并中心像素?使用测试数据我无法显示方法之间的差异,但在我的完整数据集上,存在可见的差异,如下图所示。但是,这些垃圾箱并不完全相同(50m 与 69m),因此这可能解释了部分差异。但是,在第一个垃圾箱中,这种解释对我来说似乎并不合理。或者我的光栅的不规则形状以及处理 NA 的不同方法可能会导致差异?
可运行示例
测试数据
计算测试数据的代码取自http://www.petrkeil.com/?p=1050#comment-416317
# packages used for the data generation
library(raster)
library(vegan) # will be used for PCNM
# empty matrix and spatial coordinates of its cells
side=30
my.mat <- matrix(NA, nrow=side, ncol=side)
x.coord <- rep(1:side, each=side)*5
y.coord <- rep(1:side, times=side)*5
xy <- data.frame(x.coord, y.coord)
# all paiwise euclidean distances between the cells
xy.dist <- dist(xy)
# PCNM axes of the dist. matrix (from 'vegan' package)
pcnm.axes <- pcnm(xy.dist)$vectors
# using 8th PCNM axis as my atificial z variable
z.value <- pcnm.axes[,8]*200 + rnorm(side*side, 0, 1)
# plotting the artificial spatial data
r <- rasterFromXYZ(xyz = cbind(xy,z.value))
plot(r, axes=F)
自己的代码
library(raster)
sp.Corr <- matrix(nrow = 0,ncol = 2)
formerBreak <- 0 #for the first run important
for (i in c(seq(10,200,10))) #Calculate the Morans I for these bins
{
cat(paste0("..",i)) #print the bin, which is currently calculated
w = focalWeight(r,d = i,type = 'circle')
wTemp <- w #temporarily saves the weigtht matrix
if (formerBreak>0) #if it is the second run
{
midpoint <- ceiling(ncol(w)/2) # get the midpoint
w[(midpoint-formerBreak):(midpoint+formerBreak),(midpoint-formerBreak):(midpoint+formerBreak)] <- w[(midpoint-formerBreak):(midpoint+formerBreak),(midpoint-formerBreak):(midpoint+formerBreak)]*(wOld==0)#set the previous focal weights to 0
w <- w*(1/sum(w)) #normalizes the vector to sum the weights to 1
}
wOld <- wTemp #save this weight matrix for the next run
mor <- Moran(r,w = w)
sp.Corr <- rbind(sp.Corr,c(Moran =mor,Distance = i))
formerBreak <- i/res(r)[1]#divides the breaks by the resolution of the raster to be able to translate them to the focal window
}
plot(x=sp.Corr[,2],y = sp.Corr[,1],type = "l",ylab = "Moran's I",xlab="Upper bound of distance")
计算空间相关图的其他方法
library(SpatialPack)
sp.Corr <- summary(modified.ttest(z.value,z.value,coords = xy,nclass = 21))
plot(x=sp.Corr$coef[,1],y = data$coef[,4],type = "l",ylab = "Moran's I",xlab="Upper bound of distance")
library(ncf)
ncf.cor <- correlog(x.coord, y.coord, z.value,increment=10, resamp=1)
plot(ncf.cor)