JosiahParry / sfdep

A tidy interface for spatial dependence
https://sfdep.josiahparry.com/
GNU General Public License v3.0
121 stars 5 forks source link

Estimation of local spatial autocorrelation statistics (local Moran's Ii and Getis-Ord's Gi/Gi*) using sfdep and tidyverse #37

Closed valyo29 closed 9 months ago

valyo29 commented 1 year ago

Hi everyone,

I am currently trying to estimate the local Moran's Ii and the Getis-Ord Gi/Gi* statistic to visualize clusters of voters during the 2019 Swiss national election and compare two political parties at the municipality level.

Unfortunately, those two parties did not participate in all 2212 Swiss municipalities during the 2019 election meaning there are around 120 NAs (different amount for each party), which cause problems when estimating the aforementioned statistics.

Using sfdep and tidyverse, my code, without addressing the missing data problem, looks like this:

df_municip %>% mutate(nb = st_contiguity(geometry), wt = st_weights(wt), local_moran = sfdep::local_moran(party1, nb, wt, nsim=499), local_Gi = sfdep::local_g_perm(party1, nb, wt, nsim=499)) %>% unnest(c(local_moran, local_Gi))

To circumvent the missing data problem, I tried setting na.action = na.exclude or na.action = na.pass. The former did not work, as dpylr's mutate() cannot add columns with fewer rows than the original dataframe (in my case: df_municip) to the original dataframe, while the latter did not work (optimally), as it only estimated two out of the eight values (in my case: ii and p_ii_sim).

Importantly, both of these na.action options only work for the local Moran statistic, as sfdep::local_g_perm(), which relies on spdep's localG_perm(), does not accept the argument na.action.

Does anyone have any suggestions?

P.S. I am sorry if this post is not formatted optimally, it is my first post on Github.

JosiahParry commented 11 months ago

Can you please provide a reproducible example?