Description
Hi Josiah, thank you for the amazing package! It helped me a lot!
I don't know if this is the right place for this kind of issue, but I think it worth asking.
So, I'm using a lot of function here to work an dataset that might have some time periods of a lot NA
on the value variable that I'm interested to produce an Emerging hot spot analysis like here
I've been able to reproduce my error when running this piece of code with random NAs introduced at the value column of the bos spacetime cube.
gi_stars <- bos_nb |>
group_by(time_period) |>
mutate(gi_star = local_gstar_perm(value, nb, wt)) |>
tidyr::unnest(gi_star)
gi_stars |>
select(.region_id, time_period, gi_star, p_folded_sim)
and the error as follow:
Error in
mutate()
:
ℹ In argument:gi_star = local_gstar_perm(value, nb, wt)
.
ℹ In group 1:time_period = 2010-01-01
.
Caused by error incard()
:
! not a neighbours list
Runrlang::last_trace()
to see where the error occurred.
I think the issue here is that as I have NA at the value
column this might have introduced some neighbours that aren't any more neighbouring other places, but maybe I'm wrong. I tried alternatives and workaorunds like filtering the bos spacetime before giving to the Gi* pipe and using queen = T and/or allow_zero = T, at st_conguity and st_weights, respectively, But none of this haven't worked, the error returned is the same.
Thanks in advance for any hlep or insights Josiah!
Here I give you my whole pipeline adapated from the vignette:
### Create objects to store file paths
df_fp <- system.file("extdata", "bos-ecometric.csv", package = "sfdep")
geo_fp <- system.file("extdata", "bos-ecometric.geojson", package = "sfdep")
### read in data
df <- readr::read_csv(df_fp, col_types = "ccidD")
df <- df |>
mutate(value = ifelse(rbinom(n(), 1, 0.1), NA, value))
geo <- sf::st_read(geo_fp)
bos_expanded <- expand.grid(time_period = unique(df$time_period),
.region_id = unique(geo$.region_id))
bos <- bos_expanded |>
left_join(df)
### Create spacetime object called `bos`
bos <- spacetime(bos, geo,
.loc_col = ".region_id",
.time_col = "time_period")
bos
bos_nb <- bos |>
activate("geometry") |>
mutate(
nb = include_self(st_contiguity(geometry, queen = T)),
wt = st_weights(nb, allow_zero = T, zero.policy = T)
) |>
set_nbs("nb") |>
set_wts("wt")
head(bos_nb)
gi_stars <- bos_nb |>
filter(!is.na(value)) |>
group_by(time_period) |>
mutate(gi_star = local_gstar_perm(value, nb, wt)) |>
tidyr::unnest(gi_star)
gi_stars |>
select(.region_id, time_period, gi_star, p_folded_sim)
### conduct EHSA
ehsa <- emerging_hotspot_analysis(
x = bos,
.var = "value",
k = 1,
nsim = 99
)
### preview some values
head(ehsa)
### evaluate the classifications of our hotspots
count(ehsa, classification)