huizezhang-sherry / cubble

A tidy structure for spatio-temporal vector data
https://huizezhang-sherry.github.io/cubble/
Other
55 stars 9 forks source link

Why does the cubble lose it's geometry here #19

Closed dicook closed 7 months ago

dicook commented 10 months ago
covid <- read_csv("https://raw.githubusercontent.com/numbats/eda/master/data/melb_lga_covid.csv") %>%
  mutate(Buloke = as.numeric(ifelse(Buloke == "null", "0", Buloke))) %>%
   mutate(Hindmarsh = as.numeric(ifelse(Hindmarsh == "null", "0", Hindmarsh))) %>%
   mutate(Towong = as.numeric(ifelse(Towong == "null", "0", Towong))) %>%
  pivot_longer(cols = Alpine:Yarriambiack, names_to="NAME", values_to="cases") %>%
  mutate(Date = ydm(paste0("2020/",Date))) %>%
  mutate(cases=replace_na(cases, 0))

covid <- covid %>%
  group_by(NAME) %>%
  mutate(new_cases = cases - dplyr::lag(cases)) %>%
  na.omit()

lga <- strayr::read_absmap("lga2018") |>
  rename(lga = lga_name_2018) |>
  filter(state_name_2016 == "Victoria") 

covid <- covid %>%
  select(-cases) %>%
  rename(lga = NAME, date=Date, cases = new_cases) 
covid_ts <- as_tsibble(covid, key=lga, index=date)

covid_matching <- check_key(spatial = lga, temporal = covid_ts)

lga <- lga %>% 
  mutate(lga = ifelse(lga == "Colac-Otway (S)", "Colac Otway (S)", lga)) %>%
  filter(!(lga %in% covid_matching$others$spatial))

covid_matching <- check_key(spatial = lga, temporal = covid_ts)

covid_cb <- make_cubble(
  spatial = lga, temporal = covid_ts,
  potential_match = covid_matching, index = date)

covid_tot <- covid_cb %>%
  face_temporal() %>%
  summarise(totcases = sum(cases, na.rm = TRUE))
> covid_tot
# A tibble: 79 × 2
   lga            totcases
   <chr>             <dbl>
 1 Alpine (S)            0
 2 Ararat (RC)           2
 3 Ballarat (C)         46
 4 Banyule (C)         350
....
huizezhang-sherry commented 10 months ago

I think what you want is covid_cb |> rowwise() |> mutate(a = sum(ts$cases, na.rm = TRUE))

dicook commented 10 months ago

I think that it would be the correct result for

%>%
  face_temporal() %>%
  summarise(totcases = sum(cases, na.rm = TRUE))

to retain the geometry.

You need some simple examples like this in the vignettes, and also for the functions, possibly.