Skip to content

globalG.test - x vector with different lenght as the neighbours list

2 messages · Francesco Perugini, Roger Bivand

#
Dear all,
I've a routine to create spatial weight matrix for different k and then to
implement, for each of the created spatial weighted matrix, the Global G
test for autocorrelation.
The point is that I'll try to do this test on multiple variables but then
lenghts are different
[1] 49
[1] 1

Any help on how to code this?
Thanks a lot.
franc.per90


______________________________
This is the routine:

library(spdep)
example(columbus)
#data(columbus)
#attach(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)))
}

class(neighbors.knn)

class(neighbors.knn[[1]])
plot(neighbors.knn[[1]], coord)

class(neighbors.knn[[2]])
plot(neighbors.knn[[2]], coord)
....


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(var, dlwknn.B, zero.policy=F)))
  }
  ZG[[val]] <- temp
}

t(sapply(ZG, function(var) c(var$estimate[1], var$statistic,
p.value=unname(var$p.value))))
#
On Tue, 2 Feb 2016, Francesco Perugini wrote:

            
The lists are too complicated at this stage. Now each k in ZG is a list of 
two htest objects.
Fails if columbus not attached - do not attach as this confuses everything 
here. Use:

f <- c("CRIME", "INC")
temp <- c(temp, list(globalG.test(columbus[[var]], dlwknn.B, zero.policy=F)))
Extract the variables one-by-one in the order of the f vector:

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))))

Roger
Plain text, please.