Skip to content

Moran's I : list of neighbour and generating residual maps.

3 messages · milton ruser, Roger Bivand

#
On Thu, 14 May 2009, milton ruser wrote:

            
Yes, the line with the overlay() got corrupted, and still is.

Try:

require(rgdal)
require(adehabitat)
(file1 <-  paste(system.file(package = "adehabitat"),
                "ascfiles/elevation.asc", sep = "/"))
el <- readGDAL(file1)
image(el, axes=T)
el.pix <- as(el, "SpatialPixelsDataFrame")
el.pol <- as.SpatialPolygons.SpatialPixels(el.pix)
rn <- sapply(slot(el.pol, "polygons"), function(x) slot(x, "ID"))
df <- data.frame(rn=rn, row.names=rn)
el.pol.spdf<-SpatialPolygonsDataFrame(el.pol, data=df)
el.pol.spdf$pixvalue <- el.pix$band1
spplot(el.pix, col.regions=heat.colors(20))
x11()
spplot(el.pol.spdf, "pixvalue", col.regions=heat.colors(20))

moves the values without overlaying - since the SpatialPixels and 
SpatialPolygons objects match one-to-one, this is OK.
gsz <- gridparameters(el)[,2]
d <- ceiling(sqrt(sum(gsz^2)))
library(spdep)
nb_queen <- dnearneigh(coordinates(el.pol.spdf), 0, d)
nb_queen

before calculating Moran's I:

moran.test(el.pol.spdf$pixvalue, nb2listw(nb_queen, style="C"))

There will typically be a lot of autocorrelation if the raster cell 
resolution is much finer than the "size" of the entities.

Hope this helps,

Roger