[R-sig-Geo] globalG.test - routine with distance based neighbours

Francesco Perugini francescoperugini07 at gmail.com
Fri Feb 5 10:03:36 CET 2016


Dear all,
I've routine that plot Glogal G statistics at different k-neighbours. I've
tried to run it using distance based neighbours method but I got this
message:

Error in dnb[[val]] : subscript out of bounds

Any suggestion on this?

Thanks a lot.

franc.per

_______________

this is the routine

library(spdep)
example(columbus)
coord <- coordinates(columbus)

#k-nearest neighbours
#z <- c(1:20)
#neighbors.knn <- list()


#Distance-based neighbours
k1 <- knn2nb(knearneigh(coord,k=1,longlat=F))
all.linkedT <- max(unlist(nbdists(k1, coord,longlat=F)))
all.linkedT

z <- c(620, 650, 700)
dnb <- list()
for (val in z)
  {
  dnb <- c(dnb, list(dnearneigh(coord, 0, val, longlat=F)))
# neighbors.knn <- c(neighbors.knn, list(knn2nb(knearneigh(coord, val,
longlat=F), sym=F)))
  }
list(dnb)


ndists <- length(z)
ZG <- vector(mode="list", length=ndists)
names(ZG) <- as.character(z)

f <- c("CRIME", "INC")
for (val in z)
  {
# dlwknn.B <- nb2listw(neighbors.knn[[val]], style="B", zero.policy=TRUE)
  dlwdnb.B <- nb2listw(dnb[[val]], style="B", zero.policy=TRUE)
  temp <- list()

  for (var in f)
  {
#   temp <- c(temp, list(globalG.test(columbus[[var]], dlwknn.B,
zero.policy=F)))
    temp <- c(temp, list(globalG.test(columbus[[var]], dlwdnb.B,
zero.policy=F)))
  }
  ZG[[val]] <- temp
}

t(sapply(lapply(ZG, "[[", 1), function(var) c(var$estimate[1],
                                      var$statistic,
p.value=unname(var$p.value))))
CRIME <- t(sapply(lapply(ZG, "[[", 1), function(var) c(var$estimate[1],
                                      var$statistic,
p.value=unname(var$p.value))))
plot(z, CRIME[,1], xlab="distance", ylab="Global G", xlim=NULL,
axes=TRUE, type="o", col="blue")

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list