Giorgio Garziano
2016-Feb-02 08:48 UTC
[R] R Sig-Geo group - loop for creating spatial matrix
Dear Francesco, from that point ahead, you have to handle a list of ?nb? objects. I used class() in order to show you what the list is made of. The R code outlined in your reply may follow a pattern like this (showing an option): for (val in z) { dlwknn.B <- nb2listw(neighbors.knn[[val]], style="B", zero.policy=TRUE) globalG.test(CRIME, dlwknn.B, zero.policy=F) } You may want to collect the results of the GlobalG.test in a list as well, the help(globalG.test) provides an example at the bottom of its R help page. To further point out that R language makes possible to implement functions call over a list without specifying ?for? loops. Some examples at: http://www.r-bloggers.com/using-apply-sapply-lapply-in-r/ The R-SIG-Geo mailing list is reachable at: https://stat.ethz.ch/pipermail/r-sig-geo/ if that was your original intention. Best, -- GG From: Francesco Perugini [mailto:francesco.perugini at yahoo.it] Sent: marted? 2 febbraio 2016 09:24 To: Giorgio Garziano Subject: Re: [R] R Sig-Geo group - loop for creating spatial matrix Dear Giorgio, thanks a lot for your reply. From here now, I want to implement the Global G test for spatial autocorrelation for the generated different matrix and plot the Global G statistic (on the y-axes) against different val (on the x-axis). How should the code be? I've tried the following: # Global G dlwknn.B <- nb2listw(class(neighbors.knn[[val]]), style="B", zero.policy=TRUE) globalG.test(CRIME, dlwknn.B, zero.policy=F) but it is not working. Thanks a lot for your help.. franc.per ________________________________ Da: Giorgio Garziano <giorgio.garziano at ericsson.com<mailto:giorgio.garziano at ericsson.com>> A: "r-help at r-project.org<mailto:r-help at r-project.org>" <r-help at r-project.org<mailto:r-help at r-project.org>> Cc: "francesco.perugini at yahoo.it<mailto:francesco.perugini at yahoo.it>" <francesco.perugini at yahoo.it<mailto:francesco.perugini at yahoo.it>> Inviato: Luned? 1 Febbraio 2016 20:39 Oggetto: Re: [R] R Sig-Geo group - loop for creating spatial matrix You may handle that as a list of ?nb? objects. 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))) } class(neighbours.knn) class(neighbors.knn[[1]]) plot(neighbors.knn[[1]], coord) class(neighbors.knn[[2]]) plot(neighbors.knn[[2]], coord) and so on. Best, -- GG [[alternative HTML version deleted]]