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

Roger Bivand Roger.Bivand at nhh.no
Tue Apr 28 19:49:11 CEST 2015


On Tue, 28 Apr 2015, Michael Treglia wrote:

> Hi all,
>
> This is a bit a tangential to the question, but related to
> pgirmess::correlog used in this thread, so I thought it appropriate to
> follow-up here [if not, apologies - I can start a new thread if that's
> preferred].
>
> In recent exploration with the pgirmess::correlog, I was unable to change
> the alternative hypothesis being tested. Since pgirmess::correlog uses
> spdep::moran.test, the alternative hypothesis being tested is by default
> "greater" (confirmed in the documentation). The help for the function
> indicates pgirmess::correlog should also be able to take arguments from
> 'moran.test' within it. Thus, users should be able to use the 'alternative'
> argument to change the alternative hypothesis being tested.  When trying to
> use "alternative='less'" (or 'two.sided'), an error is returned:
>
> Error in try(moran.test(lst.z1[[i]], nb2listw(lst.nb1[[i]], style = "W")),
> :
>  unused argument (alternative = "less")

The problem is that the ... is passed to try(), not moran.test() or 
geary.test() in:

xt <- switch(pmatch(method, c("Moran", "Geary"), nomatch = 3),
     try(moran.test(lst.z1[[i]], nb2listw(lst.nb1[[i]],
         style = "W")), silent = TRUE, ...), try(geary.test(lst.z1[[i]],
         nb2listw(lst.nb1[[i]], style = "W")), silent = TRUE,
         ...), stop("Method must be 'Moran' or 'Geary'"))

With:

xt <- switch(pmatch(method, c("Moran", "Geary"), nomatch = 3),
     try(moran.test(lst.z1[[i]], nb2listw(lst.nb1[[i]],
         style = "W"), ...), silent = TRUE), try(geary.test(lst.z1[[i]],
         nb2listw(lst.nb1[[i]], style = "W"), ...), silent = TRUE),
         stop("Method must be 'Moran' or 'Geary'"))

via for example correlog1 <- edit(correlog), it works. You can save the 
function for later use too.

Hope this helps,

Roger


>
> Reproducible Code (based on the example from help for pgirmess::correlog):
>
> library(spdep)
> library(pgirmess)
> data(oldcol)
> attach(COL.OLD)
> coords<-cbind(X,Y)
> res<-correlog(coords,CRIME, method="Moran", nbclass=NULL,alternative="less")
> ## Error in try(moran.test(lst.z1[[i]], nb2listw(lst.nb1[[i]], style =
> "W")),  :
> ##  unused argument (alternative = "less")
>
> I corresponded with the author of pgirmess and he did not have a solution
> nor much time to work on it. I was hoping to give a go at trouble-shooting
> myself, but haven't had a chance, so this thread seemed like a relevant
> place to bring up the issue, so others are at least aware of it.
>
> Best,
> Mike Treglia
>
>
>
>
>
>
> On Tue, Apr 28, 2015 at 2:05 AM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>
>> 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
>>
>>
>> _______________________________________________
>> 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