# [R-sig-Geo] globalG.test - plotting from routine

Roger Bivand Roger.Bivand at nhh.no
Wed Feb 3 15:34:28 CET 2016

```On Wed, 3 Feb 2016, Francesco Perugini wrote:

> Dear all,
> I've a routine (see below) that computes the Global G statistics for
> different k-neighbor spatial matrix and for different variables.
> Now I want to plot (for each var) the difference between Global G statistic
> computed at k neighbour with the one computed at k-1 on the y-axes, with
> the number of k on the x-axis.
> I guess I need to extract the Global G statistics from the ZG lists and
> then plot them.This is my attempt:
>
> library(rlist)
> list(ZG)
> list.extract(ZG,'var\$estimate[1]')
>
> plot(c(var\$estimate[1], border="grey60",xlab="k",ylab="Global G statistic",
>     xlim=NULL, axes=TRUE)
> title(main="Global G at different k")

This works for me, using the tabular results I showed you how to get in my

> 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="k",ylab="Global G statistic", xlim=NULL,
axes=TRUE)

The increase looks almost linear.

Roger

>
> Any suggestion on this?
> Thanks a lot.
> franc.per
>
> _____________
> this is the routine:
>
> library(spdep)
> example(columbus)
> coord <- coordinates(columbus)
>
> z <- c(1,2,3,4,5,6,7,8,9)
> neighbors.knn <- list()
>
> for (val in z)
>  {
>  neighbors.knn <- c(neighbors.knn, list(knn2nb(knearneigh(coord, val,
> longlat=F), sym=F)))
> }
>
> 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)
>   temp <- list()
>
> for (var in f)
>              {
>   temp <- c(temp, list(globalG.test(columbus[[var]], dlwknn.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))))
>
> t(sapply(lapply(ZG, "[[", 2), function(var) c(var\$estimate[1],
>                                              var\$statistic,
> p.value=unname(var\$p.value))))
>
> 	[[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