[R-sig-Geo] Results from spdep::moran.mc() different from raster::Moran() on randomized raster layers

Agustin Lobo alobolistas at gmail.com
Sat Nov 21 12:28:15 CET 2015


I compare results of moran.test() and moran.mc()
to results of randomizing a raster layer and
calculating the mean and range of the raster::Moran() values
using w=matrix(c(1,1,1,1,0,1,1,1,1), 3, 3) (following advise
https://stat.ethz.ch/pipermail/r-sig-geo/2015-November/023761.html)

While the spdep functions let reject the null hypothesis
of CSR because of regularity (which makes sense
because the test is a check board pattern),
the range of raster::Moran() for the randomized raster
layers include the actual value of the observed test layer.
Could this be caused by the fact that the test raster layer
is too small (8x8)? Because sample() is actually not
randomizing as boot()? An error on my side?

This is what I do:

Using the testGrid example provided by
https://stat.ethz.ch/pipermail/r-sig-geo/2014-December/022106.html

lng <- rep(seq(0, 7, by=1), 8)
counter = 1
subCounter = 0
startNum = 0
lat = NULL
while (counter < 65) {
 if (subCounter == 8) {
   startNum = startNum + 1
   subCounter = 0
 }
 lat = c(lat, startNum)
 subCounter = subCounter + 1
 counter = counter + 1
}
# now add the black/ white chessboard pattern
chess <- rep(c(0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0), 4)
gridNum <- seq(1:64)
testGrid <- data.frame(gridNum, lat, lng, chess)


nb8q <- cell2nb(8, 8, type="queen", torus=FALSE)
moran.test(testGrid$chess, nb2listw(nb8q, style="B"), alternative="less")
moran.mc(testGrid$chess,nb2listw(nb8q, style="B"),nsim=200,alternative="less")
wn=matrix(c(1,1,1,1,0,1,1,1,1), 3, 3)
Moran(rtestGrid, w=wn)
nsim=200
rtest <- rtestGrid
vmoran <- (1:nsim)*0
for (i in 1:nsim){
  rtest[] <- sample(rtestGrid,ncell(rtestGrid),replace=TRUE)
  vmoran[i] <- Moran(rtest, w=wn)
  }
mean(vmoran)
var(vmoran)
range(vmoran)
hist(vmoran)
Moran(rtestGrid, w=wn)



More information about the R-sig-Geo mailing list