I would like to obtain Moran's I estimation together with its p-value from a raster data layer --which has na value-- including a figure showing the simulation distribution by mean of spdep::mc.moran() function.
I would like to know whether the code bellow implies the right manner to convert the rater to polygon as required to be input to the function spdep::mc.moran(), as well as whether the code bellow is an appropriate manner to alternatively solve the problem ?
library(raster) library(spdep) r<- raster(file.choose()) l <- rasterToPolygons(r) nb.l <- poly2nb(l, queen=FALSE) lw.l <- nb2listw(nb.l, style="W",zero.policy=TRUE) v<-r[!is.na(r)] M <- moran.mc((v), lw.l, 999, zero.policy=TRUE) M plot(M, main=sprintf("Original, Moran's I (p = %0.3f)", M$p.value), xlab="Monte-Carlo Simulation of Moran I", sub =NA, cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5) abline(v=M$statistic, col="red", lw=2)