[R-sig-Geo] listw for local Moran in spdep

Roger Bivand Roger.Bivand at nhh.no
Tue Feb 8 14:29:10 CET 2011


On Tue, 8 Feb 2011, Agustin Lobo wrote:

> Thanks. I have problems to understand the order in which I have to
> input the data from the matrix.
> This is what I'm doing:
>
> require(spdep)
> gpclibPermit()
> require(ncf)
> #data
> x <- expand.grid(1:20, 1:5)[,1]
> y <- expand.grid(1:20, 1:5)[,2]
> zori <- rmvn.spa(x=x, y=y, p=2, method="exp")
> z = scale(zori)
> dim(z)=c(20,5)
> z = t(z)[5:1,]
> rz = raster(z)
>
> #weights
> wrz = cell2nb(nrow=nrow(rz), ncol=ncol(rz), type="queen", torus=FALSE)
> #lisa
> lisarz = localmoran(x=rz at data@values, listw=nb2listw(wrz))
>
> (but I do not know if the ordering of the data x and the weights listw
> are matching)

Most of the use examples of cell2nb() are that one generates the nb object 
first to use in studying the properties of tests and estimators, because a 
grid on a torus has no edge effects. The positions are encoded in the 
"region.id" attribute of the nb object - the example in ?cell2nb shows 
their decoding to retreive coordinates corresponding to your case.

As I suggested, use rather dnearneigh:

require(spdep)
require(ncf)
xy <- expand.grid(x=1:20, y=1:5)
zori <- rmvn.spa(x=x, y=y, p=2, method="exp")
xy$z <- scale(zori)
coordinates(xy) <- c("x", "y")
wrz <- dnearneigh(xy, 0, sqrt(2))
wrz
lisarz = localmoran(x=xy$z, listw=nb2listw(wrz))
str(lisarz)

I'm assuming that rmvn.spa() outputs the vector in the correct order.

Roger



>
> l3r = lisarz[,1]
> dim(l3r)=c(20,5)
> l3r = t(l3r)[5:1,]
> l3r = raster(l3r)
> plot(l3r)
>
>
>
> Agus
>
> 2011/2/7 Roger Bivand <Roger.Bivand at nhh.no>:
>> On Mon, 7 Feb 2011, Agustin Lobo wrote:
>>
>>> Hi!
>>>
>>> I'd like to run localmoran() from spdep on a small grid (matrix). Is
>>> there a function to calculate the spatial weights (listw) from
>>> the grid  (defining 3x3 neighborhoods)?
>>
>> For a 3x3 neighbourhood on a grid simply defined by numbers of rows and
>> columnd, see ?cell2nb. Otherwise, use dnearneigh() with the threshold set to
>> the length of the diagonal distance between cell centres giving the cell
>> centre coordinates as input.
>>
>>>
>>> (BTW: the definition of LISA in the spdep manual is unformatted)
>>>
>>
>> The LaTeX version looks fine! I'll try to repair the regular page.
>>
>> Roger
>>
>>> Thanks
>>>
>>> Agus
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>> --
>> Roger Bivand
>> Economic Geography Section, Department of Economics, Norwegian School of
>> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
>> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
>> 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
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no


More information about the R-sig-Geo mailing list