gpt4 book ai didi

r - 使用 raster 包的空间相关图

转载 作者:行者123 更新时间:2023-12-04 08:48:07 24 4
gpt4 key购买 nike

亲爱的人群
问题
我尝试使用软件包 nfc、pgirmess、SpatialPack 和 spdep 计算空间相关图。但是,我很难定义距离的起点和终点。我只对较小距离的空间自相关感兴趣,但对较小的 bin 感兴趣。此外,由于光栅非常大(1.8 兆像素),我遇到了这些包的内存问题,但 SpatialPack。
因此,我尝试使用包 raster 中的 Moran 函数生成自己的代码。但是我一定有一些错误,因为完整数据集的结果与其他包的结果有些不同。如果我的代码没有错误,它至少可以帮助其他有类似问题的人。
问题
我不确定我的焦点矩阵是否有误。你能告诉我是否需要合并中心像素吗?使用 testdata 我无法显示方法之间的差异,但在我的完整数据集上,存在可见差异,如下图所示。但是,垃圾箱并不完全相同(50m 与 69m),因此这可能解释了部分差异。但是,在第一个垃圾箱中,这种解释对我来说似乎不合理。或者我的栅格的不规则形状以及处理 NA 的不同方法可能会导致差异?
Comparison of Own method with the one from SpatialPack
可运行示例
测试数据
计算测试数据的代码取自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)

最佳答案

为了比较相关图的结果,在您的情况下,应考虑两件事。 (i) 您的代码仅适用于与您的光栅分辨率成正比的 bin。在这种情况下,bin 中的一些差异可能会包含或排除重要数量的对。 (ii) 栅格的不规则形状对被认为计算特定距离间隔相关性的对有很强的影响。所以你的代码应该同时处理两者,允许 bin 长度的任何值,并考虑光栅的不规则形状。为解决这些问题而对您的代码进行的小修改如下。

# SpatialPack correlation
library(SpatialPack)
test <- modified.ttest(z.value,z.value,coords = xy,nclass = 21)

# Own correlation
bins <- test$upper.bounds
library(raster)
sp.Corr <- matrix(nrow = 0,ncol = 2)
for (i in 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 (i > bins[1]) {
midpoint <- ceiling(dim(w)/2) # get the midpoint
half_range <- floor(dim(wOld)/2)
w[(midpoint[1] - half_range[1]):(midpoint[1] + half_range[1]),
(midpoint[2] - half_range[2]):(midpoint[2] + half_range[2])] <-
w[(midpoint[1] - half_range[1]):(midpoint[1] + half_range[1]),
(midpoint[2] - half_range[2]):(midpoint[2] + half_range[2])]*(wOld==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))
}
# Comparing
plot(x=test$upper.bounds, test$imoran[,1], col = 2,type = "b",ylab = "Moran's I",xlab="Upper bound of distance", lwd = 2)
lines(x=sp.Corr[,2],y = sp.Corr[,1], col = 3)
points(x=sp.Corr[,2],y = sp.Corr[,1], col = 3)
legend('topright', legend = c('SpatialPack', 'Own code'), col = 2:3, lty = 1, lwd = 2:1)

如图所示,使用SpatialPack包的结果和自己的代码是一样的。

enter image description here

关于r - 使用 raster 包的空间相关图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33409501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com