GeoDaCenter / rgeoda

R library for spatial data analysis based on libgeoda and GeoDa
70 stars 13 forks source link

Odd p-values from `local_geary()` #28

Open rsbivand opened 2 years ago

rsbivand commented 2 years ago

In an example just added to spdep on github, issue https://github.com/r-spatial/spdep/issues/68, I find that the lisa_pvalues() are not bounded by [0, 0.5] as in for example local_moran() for the same data. When I "fold back" the values > 0.5, the vector correlates well with the equivalent conditional permutation output for the local Geary measure just added to spdep. Installing spdep from github (main), the following reproduces the objects and oddness:

library(spdep)
orig <- spData::africa.rook.nb
listw <- nb2listw(orig)
x <- spData::afcon$totcon

(A <- localC(x, listw))
listw1 <- nb2listw(droplinks(sym.attr.nb(orig), 3, sym=TRUE), zero.policy=TRUE)
(A1 <- localC(x, listw1, zero.policy=FALSE))
(A2 <- localC(x, listw1, zero.policy=TRUE))
if (require(rgeoda, quietly=TRUE)) {
  W <- create_weights(as.numeric(length(x)))
  for (i in 1:length(listw$neighbours)) {
    set_neighbors_with_weights(W, i, listw$neighbours[[i]], listw$weights[[i]])
    update_weights(W)
  }
  set.seed(1)
  B <- local_geary(W, data.frame(x))
  all.equal(A, lisa_values(B))
}
set.seed(1)
C <- localC_perm(x, listw, nsim = 499, conditional=TRUE, alternative="two.sided")
plot(lisa_pvalues(B), attr(C, "pseudo-p")[,6])
cor(lisa_pvalues(B), attr(C, "pseudo-p")[,6])
cor(ifelse(lisa_pvalues(B) < 0.5, lisa_pvalues(B), 1-lisa_pvalues(B)), attr(C, "pseudo-p")[,6])