8

親愛なる群衆

問題

パッケージnfc、pgirmess、SpatialPack、およびspdepを使用して、空間コレログラムを計算しようとしました。しかし、距離の始点と終点を定義するのに苦労しました。私はより短い距離での空間的自己相関にのみ興味がありますが、より小さなビンにはあります。さらに、ラスターが非常に大きい (1.8 メガピクセル) ため、これらのパッケージではなく SpatialPack でメモリの問題が発生します。

そこで、パッケージ ラスターの関数 Moran を使用して、独自のコードを作成しようとしました。ただし、完全なデータセットの結果は他のパッケージの結果とは多少異なるため、エラーが発生する必要があります。私のコードにエラーがなければ、少なくとも同様の問題を抱えている他の人を助けるかもしれません.

質問

私の焦点行列が間違っているかどうかはわかりません。中央のピクセルを組み込む必要があるかどうか教えてください。テストデータを使用すると、メソッド間の違いを示すことはできませんが、完全なデータセットでは、下の画像に示すように違いが見えます。ただし、ビンはまったく同じではない (50m 対 69m) ため、これが違いの一部を説明している可能性があります。ただし、最初のビンでは、この説明はもっともらしいようには思えません。それとも、ラスターの不規則な形状と、NA を処理するさまざまな方法が違いを引き起こしているのでしょうか?

独自の方法と 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)
4

1 に答える 1

2

コレログラムの結果を比較するには、あなたの場合、2 つのことを考慮する必要があります。(i) コードは、ラスターの解像度に比例するビンに対してのみ機能します。その場合、ビンのわずかな違いにより、重要な量のペアが含まれたり除外されたりする可能性があります。(ii) ラスターの不規則な形状は、特定の距離間隔の相関を計算するために考慮されるペアに強い影響を与えます。したがって、コードは両方を処理し、ビンの長さに任意の値を許可し、ラスターの不規則な形状を考慮する必要があります。これらの問題に対処するためのコードの小さな変更を以下に示します。

# 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 パッケージと独自のコードを使用した結果が同じであることを示しています。

ここに画像の説明を入力

于 2016-05-27T14:44:17.190 に答える