Skip to content

listw for local Moran in spdep

4 messages · Agustin Lobo, Roger Bivand

#
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)?

(BTW: the definition of LISA in the spdep manual is unformatted)

Thanks

Agus
#
On Mon, 7 Feb 2011, Agustin Lobo wrote:

            
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.
The LaTeX version looks fine! I'll try to repair the regular page.

Roger

  
    
#
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)

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 Tue, 8 Feb 2011, Agustin Lobo wrote:

            
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