[R-sig-Geo] A simple correlogram using sp.correlogram with data(meuse)

Roger Bivand Roger.Bivand at nhh.no
Tue Apr 28 09:05:59 CEST 2015


On Tue, 28 Apr 2015, Andy Bunn wrote:

> Hi all, This seems trivial easy but being a bear of very little brain...
>
> I'm looking to build a correlogram using Moran's I and the function
> sp.correlogram() with data(meuse) as a SpatialPointsDataFrame. I've done
> so with the ncf library using correlog() but getting the equivalent out of
> library sp is eluding me. Can somebody help me out?

require(ncf)
ncf.cor <- ncf::correlog(x=coordinates(meuse)[,1],
  y=coordinates(meuse)[,2], z=meuse$zinc, increment=200, resamp=100)

ncf::correlog() chooses bins based on increment. Alternatively, 
pgirmess::correlog() uses by default an optimal number of bins, but can 
use a bin count:

library(pgirmess)
pgir.cor <- pgirmess::correlog(coordinates(meuse), z=meuse$zinc, "Moran",
   nbclass=23)
plot(pgir.cor[,1], pgir.cor[,2], type="b")
cbind(pgir.cor[,1], ncf.cor$mean.of.class)
cbind(pgir.cor[,4], ncf.cor$n)

The differences come from ncf::correlog() constructing the spatial weights 
itself, while pgirmess::correlog() uses spdep::dnearneigh() to construct 
them for the distance bins and then spdep::nb2listw() and 
spdep::moran.test() internally in a loop, once for each bin.

spdep::sp.correlogram() follows the original Cliff & Ord understanding of 
a spatial correlogram - like time series lags - of order 1, order 2, etc. 
The input spatial weights define the weighted graph, so order 2 neighbours 
are neighbours of order 1 neighbours by stepping out on the graph. 
pgirmess::correlog() was written to use distance rather than graph edge 
count distance:

library(spdep)
nb200 <- dnearneigh(meuse, 0, 200)
spdep.cor <- spdep::sp.correlogram(nb200, meuse$zinc, order=23,
  method="I", zero.policy=TRUE)
plot(spdep.cor)

To get the implied mean distances for lags of nb200:

nb200_23 <- nblag(nb200, 23)
mdbins <- sapply(nb200_23, function(x) mean(unlist(nbdists(x, meuse))))
mdbins

There are two different understandings of this spatial correlogram, the 
C&O one based on time series and counting distance in graph edges, and 
another using distance bands, also found in globalG.test, etc.

Hope this clarifies,

Roger


>
> -Andy
>
> require(sp)
> require(ncf)
> data(meuse)
> coordinates(meuse) <- ~x+y
> proj4string(meuse) <- CRS("+init=epsg:28992")
> ncf.cor <- correlog(x=coordinates(meuse)[,1], y=coordinates(meuse)[,2],
> z=meuse at data$zinc, increment=200, resamp=100)
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list