[R-sig-Geo] Question about dnearneigh
Takatsugu Kobayashi
tkobayas at indiana.edu
Thu Mar 1 21:37:17 CET 2007
Hi,
I have been doing exploratory analysis of the link between global
Moran's I coefficients and directional biases of frequencies.
I created a 10*10 matrix that contains random numbers using
randomNumbers(). This went well till I had to define a list of nearest
neighbor of each cell. I defined the nearest neighbors as cells that
fall within 3 unit distance of each cell. But the images and Moran's I
appear to be strange, meaning that the images look like random patterns
while the corresponding Moran's I indicate high autocorrelation. I
appreciate if someone could point out what I did wrong. Here is the
commands I typed:
rm(list=ls(all=T))
# Directional bias calculation
library(random)
library(spdep)
# Random process
# 10*10 case
# Show n cases of lattice data that contains random numbers using
randomNumbers()
cases<-2
pts<-seq(1,10,by=1)
x<-rep(pts,each=length(pts))
y<-rep(pts,times=length(pts))
z<-randomNumbers(length(pts)^2*cases,0,100,cases)
D<-cbind(x,y,z)
topo<-rep(list(matrix(,length(pts),length(pts))),cases)
# Reshaping a vector into n matrices
for (m in 1:cases){topo[[m]]<-matrix(D[,m+2],length(pts),length(pts))}
dis<-as.matrix(dist(cbind(x,y),diag=T,upper=T,p=2))
coords<-cbind(x,y)
# Distance-based nearest neighbors list
dnb <- dnearneigh(coords, 0, 3)
par(mfcol=c(1,2))
for (p in 1:cases){
test<-moran.test(D[,p], nb2listw(dnb,style="W"))
image(pts,pts,topo[[p]],col=grey(50:1/50),xlab="Easting",ylab="Northing")
title(main=paste("Moran's I =",round(test$estimate[1],4)))
}
More information about the R-sig-Geo
mailing list