rgeoda icon indicating copy to clipboard operation
rgeoda copied to clipboard

Error Bivariate Local Moran function (and Local MOran function too?)

Open denis-or opened this issue 2 years ago • 1 comments

Hi folks!

I've been thinking about an error in the bivariate moran function and I'd like some help. Below, I replicate it with the guerry shp:

Ok:

  library(rgeoda)
  library(ggplot2)
  library(dplyr)
  library(sf)

guerry_path <- system.file("extdata", "Guerry.shp", package = "rgeoda")
guerry <- sf::st_read(guerry_path)

lisa_rgeoda <- rgeoda::queen_weights(guerry) |> 
  rgeoda::local_bimoran(guerry[c("Crm_prp","Wealth")])

mutate(
  guerry,
  bv_moran = factor(
    x = rgeoda::lisa_clusters(lisa_rgeoda),
    levels = 0:6,
    labels = rgeoda::lisa_labels(lisa_rgeoda)
  )
) |>
  ggplot() +
  geom_sf(aes(fill = bv_moran)) +
  scale_fill_manual(name = "With rgeoda",
                    values = rgeoda::lisa_colors(lisa_rgeoda),
                    drop = F)

image

Not ok:

set.seed(123)
guerry$Crm_prp[sample(1:nrow(guerry), size = 3, replace = T)] <- NA

lisa_rgeoda_NA <- rgeoda::queen_weights(guerry) |> 
  rgeoda::local_bimoran(guerry[c("Crm_prp","Wealth")])

mutate(
  guerry,
  bv_moran = factor(
    x = rgeoda::lisa_clusters(lisa_rgeoda_NA),
    levels = 0:6,
    labels = rgeoda::lisa_labels(lisa_rgeoda_NA)
  )
) |>
  ggplot() +
  geom_sf(aes(fill = bv_moran)) +
  scale_fill_manual(name = "With rgeoda",
                    values = rgeoda::lisa_colors(lisa_rgeoda_NA),
                    drop = F)

image

Created on 2023-02-19 with reprex v2.0.2

It seems that part of the code is wrong or incomplete, as it does not cover the NAs or the values equal zero (I guess) https://github.com/GeoDaCenter/libgeoda/blob/a9a3d0a3212dfc7cdc63d41f5e8033ba54063693/sa/BiLocalMoran.cpp#L81

Is it wrong if I use na.omit()? Like as: rgeoda::local_bimoran(na.omit(guerry[c("Crm_prp","Wealth")])) like so the map is different.

What I do? thanks

denis-or avatar Feb 19 '23 21:02 denis-or

#31 I think it's the same problem.

denis-or avatar Feb 20 '23 20:02 denis-or