[R-sig-Geo] Simulating spatially autocorrelated data
Downey, Patrick
PDowney at urban.org
Thu Sep 1 19:58:31 CEST 2011
Hello all,
I'm trying to simulate a spatially autocorrelated random variable, and I
cannot figure out what the problem is. All I want is a simple spatial lag
model where
Y = rho*W*Y + e
Where e is a vector of iid normal random variables, rho is the
autocorrelation, W is a row-normalized distance matrix (a spatial weights
matrix), and Y is the random variable.
I thought the following program should do it, but it's not working. At the
end of the program, I calculate Moran's I, and it is not even close to
rejecting the null hypothesis of no spatial autocorrelation, even when rho
is very high (for example, below, rho is 0.95). Can someone please identify
what the problem is and offer some guidance on how to fix it?
PS - I apologize in advance, but I am not familiar with R's spatial
packages. I've done very little spatial analysis in R, so if there's a
package that can already do this, please recommend.
BEGIN PROGRAM:
install.packages("fields");library(fields)
install.packages("ape");library(ape)
N <- 200
rho <- 0.95
x.coord <- runif(N,0,100)
y.coord <- runif(N,0,100)
points <- cbind(x.coord,y.coord)
e <- rnorm(N,0,1)
dist.nonnorm <- rdist(points,points) # Matrix of Euclidean distances
dist <- dist.nonnorm/rowSums(dist.nonnorm) # Row normalizing the distance
matrix
diag(dist) <- 0 # Ensuring that the main diagonal is exactly 0
I <- diag(N) # Identity matrix (not Moran's I)
inv <- solve(I-rho.lag*dist) # Inverting (I - rho*W)
y <- as.vector(inv %*% e) # Generating data that is supposed to be
spatially autocorrelated
Moran.I(y,dist) # Does not reject null hypothesis of no spatial
autocorrelation
More information about the R-sig-Geo
mailing list