The standard deviate of local Moran statistic (z-score) is derived following:
z = (morans.observed - morans.expected) / sqrt(morans.variance)
And the p-value for a two sided test, simply:
p = pnorm(z)
However, to return the expected and variance rasters you will need to modify the raster::MoranLocal
function directly as, there is no way to derive these statistics on the raster resulting from the function.
This looks perfectly correct to me - you can reproduce the value of Moran(r)
if you use queen=TRUE
in the neighbour calculation and use style="B"
in the weighted neighbour calculation, so that all the weights are 1. With style="W"
the sum of weights is 1 for each feature.
By default raster::Moran
uses a weight matrix:
w = matrix(c(1, 1, 1, 1, 0, 1, 1, 1, 1), 3, 3)
which corresponds to queen's adjacency and a binary adjacency. Using my standard r = raster(matrix(1:12, 4,3))
quick test raster, and following your code:
> nb.l <- poly2nb(l, queen=TRUE)
> lw.l <- nb2listw(nb.l, style="B",zero.policy=TRUE)
> v<-r[!is.na(r)]
> M <- moran.mc((v), lw.l, 999, zero.policy=TRUE)
> M
Monte-Carlo simulation of Moran I
data: (v)
weights: lw.l
number of simulations + 1: 1000
statistic = 0.33205, observed rank = 994, p-value = 0.006
alternative hypothesis: greater
> Moran(r)
[1] 0.3320473
Best Answer
You could do a monte-carlo test of
I>0
:First lets create a very correlated raster:
And now do 99 Moran's I of rasters that are random samplings of those values:
And let's see the distribution:
And its clear that the
Moran(r)
is way outside the range of the simulations, so we can reject the null that the data are uncorrelated with respect to random sampling/rearrangement of the data.To get the approximate pseudo p-value, see where the Moran stat for the data ranks amongst the simulations. Suppose the Moran stat for the data was 0.018 (using the 0.969 example from my code is a bit extreme), then compute the rank:
Which shoes that 0.018 ranks 98 out of the 100 values (99 sims + itself). Hence reject H0 (no spatial autocorrelation under random arrangement hypothesis) with approximate p = 0.98.
If you do more simulations, then the general case is:
for any number of simulations in M99.
This is what
moran.mc
from thespdep
package does with data for polygons or other general neighbourhood structures.Alternatively convert your raster to a grid of polygons and use the
spdep
functions with either 4-way or 8-way neighbours (or beyond...)