親愛なる群衆
問題
パッケージ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)