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:
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.
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
orna.action = na.pass
. The former did not work, as dpylr'smutate()
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, assfdep::local_g_perm()
, which relies on spdep'slocalG_perm()
, does not accept the argumentna.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.