[R-sig-Geo] globalG.test - x vector with different lenght as the neighbours list
Roger Bivand
Roger.Bivand at nhh.no
Tue Feb 2 16:36:23 CET 2016
On Tue, 2 Feb 2016, Francesco Perugini wrote:
> 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
>
>> length(dlwknn.B$neighbours)
> [1] 49
>> length(var)
> [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)
The lists are too complicated at this stage. Now each k in ZG is a list of
two htest objects.
>
> f <- c(CRIME, INC)
Fails if columbus not attached - do not attach as this confuses everything
here. Use:
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)))
temp <- c(temp, list(globalG.test(columbus[[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))))
>
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
> [[alternative HTML version deleted]]
Plain text, please.
>
> _______________________________________________
> 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