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

Roger Bivand Roger.Bivand at nhh.no
Fri Feb 5 13:27:33 CET 2016


On Fri, 5 Feb 2016, Francesco Perugini wrote:

> 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

val is defined in for (val in z), and z in z <- c(620, 650, 700) - 
arguably should be c(0.620, 0.650, 0.700) given all.linkedT as 0.619. 
There is no dnb[[620]], it is length(dnb) == length(z) long.

Roger

>
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no
http://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
http://depsy.org/person/434412



More information about the R-sig-Geo mailing list