Skip to content
Prev 12669 / 29559 Next

Simulating spatially autocorrelated data

On Thu, 1 Sep 2011, Downey, Patrick wrote:

            
I think that you are using the distances as weights, not inverse 
distances, which seems more sensible.
As Terry Griffin says, you can use spdep for this:

library(spdep)
rho <- 0.95
N <- 200
x.coord <- runif(N,0,100)
y.coord <- runif(N,0,100)
points <- cbind(x.coord,y.coord)
e <- rnorm(N,0,1)
dnb <- dnearneigh(points, 0, 150)
dsts <- nbdists(dnb, points)
idw <- lapply(dsts, function(x) 1/x)
lw <- nb2listw(dnb, glist=idw, style="W")
inv <- invIrW(lw, rho)
y <- inv %*% e
moran.test(y, lw)

to reproduce your analysis with IDW, here without:

lw <- nb2listw(dnb, glist=dsts, style="W")
inv <- invIrW(lw, rho)
y <- inv %*% e
moran.test(y, lw)
# no autocorrelation

and here with a less inclusive distance threshold:

dnb <- dnearneigh(points, 0, 15)
dsts <- nbdists(dnb, points)
idw <- lapply(dsts, function(x) 1/x)
lw <- nb2listw(dnb, glist=idw, style="W")
inv <- invIrW(lw, rho)
y <- inv %*% e
moran.test(y, lw)


the larger the distance threshold, the less well the spatial process is 
captured, alternatively use idw <- lapply(dsts, function(x) 1/(x^2)), for 
example, to attenuate the weights more sharply.

Hope this clarifies,

Roger