## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
	message = FALSE,
	warning = FALSE,
	collapse = TRUE,
  eval = nzchar(Sys.getenv("COMPILE_VIG")),
	comment = "#>"
)

## -----------------------------------------------------------------------------
library(dplyr)
library(tidyr)
library(ggplot2)
library(cancensus)
library(sf)
library(tongfen)
# cancensus::set_api_key("<your cancensus API key>")

## -----------------------------------------------------------------------------
years <- c(1971,seq(1981,2011,5))
vectors <- c(setNames(paste0("v_CA",years,"x16_1"),years),"2016"="v_CA16_1")
timeline <- names(vectors)

toronto <- get_census("CA16CT",regions=list(CSD="3520005"),vectors=vectors,
                      level="DA",geo_format="sf",quiet=TRUE) %>%
  select(GeoUID,all_of(timeline)) %>%
  mutate(across(all_of(timeline),\(x) coalesce(x,0)))

## ----fig.width=7, fig.height=3.5----------------------------------------------
crescent_town <- c("35204370","35204765")

plot_timelines <- function(data) {
  data %>%
    st_drop_geometry() %>%
    pivot_longer(all_of(timeline),names_to="Year",values_to="Population") %>%
    ggplot(aes(x=Year,y=Population,colour=GeoUID,group=GeoUID)) +
    geom_line() +
    geom_point() +
    scale_y_continuous(labels=scales::comma,limits=c(0,NA))
}

toronto %>%
  filter(GeoUID %in% crescent_town) %>%
  plot_timelines() +
  labs(title="Population in two neighbouring dissemination areas")

## -----------------------------------------------------------------------------
anomalies <- tongfen_detect_anomalies(toronto,timeline,id="GeoUID",total_surprise_cutoff=0.4)

anomalies %>% filter(GeoUID %in% crescent_town)

## -----------------------------------------------------------------------------
anomalies %>% count(join)

## -----------------------------------------------------------------------------
joins <- tongfen_anomaly_joins(toronto,timeline,id="GeoUID",total_surprise_cutoff=0.4)

joins %>% filter(GeoUID %in% crescent_town)

## -----------------------------------------------------------------------------
toronto_joined <- tongfen_join_regions(toronto,joins,id="GeoUID")

c(original=nrow(toronto),joined=nrow(toronto_joined))

## ----fig.width=7, fig.height=3.5----------------------------------------------
toronto_joined %>%
  filter(GeoUID %in% crescent_town) %>%
  plot_timelines() +
  labs(title="Population in the joined region")

## ----fig.width=7, fig.height=5------------------------------------------------
bbox <- toronto %>% filter(GeoUID %in% crescent_town) %>% st_buffer(1500) %>% st_bbox()

ggplot(toronto_joined %>% mutate(joined=GeoUID %in% joins$GeoUID_joined)) +
  geom_sf(aes(fill=joined),linewidth=0.1) +
  geom_sf(data=toronto,fill=NA,linewidth=0.1,linetype="dotted") +
  scale_fill_manual(values=c("TRUE"="steelblue","FALSE"="whitesmoke"),guide="none") +
  coord_sf(datum=NA,xlim=bbox[c("xmin","xmax")],ylim=bbox[c("ymin","ymax")]) +
  labs(title="Joined regions around Crescent Town",
       caption="Joined regions in blue, original dissemination areas dotted")

## -----------------------------------------------------------------------------
c(0.4,0.6,0.75) %>%
  lapply(\(cutoff) tibble(total_surprise_cutoff=cutoff,
                          regions_joined=tongfen_anomaly_joins(toronto,timeline,id="GeoUID",
                                                               total_surprise_cutoff=cutoff) %>%
                            nrow())) %>%
  bind_rows()

## -----------------------------------------------------------------------------
regions <- list(CSD="5915022")
datasets <- c("CA01","CA06","CA11","CA16","CA21")
meta <- meta_for_additive_variables(datasets,"Population")

vancouver <- get_tongfen_ca_census(regions=regions,meta=meta,level="DA",base_geo="CA21",quiet=TRUE)

joins <- tongfen_anomaly_joins(vancouver,paste0("Population_",datasets),total_surprise_cutoff=0.4)
joins

## -----------------------------------------------------------------------------
vancouver_joined <- tongfen_join_regions(vancouver,joins,meta)

## -----------------------------------------------------------------------------
vancouver_joined %>%
  st_drop_geometry() %>%
  filter(TongfenID %in% joins$TongfenID_joined) %>%
  select(TongfenID,TongfenUID,starts_with("Population"))

## -----------------------------------------------------------------------------
correspondence <- get_tongfen_correspondence_ca_census(geo_datasets=datasets,regions=regions,
                                                       level="DA",quiet=TRUE) %>%
  tongfen_join_correspondence(joins)

rent_meta <- meta_for_ca_census_vectors(c(rent_2006="v_CA06_2050",rent_2016="v_CA16_4901"))

rent_data <- c("CA06","CA16") %>%
  lapply(\(ds) get_census(ds,regions=regions,level="DA",labels="short",quiet=TRUE,
                          vectors=rent_meta %>% filter(geo_dataset==ds) %>% pull(variable),
                          geo_format=if (ds=="CA16") "sf" else NA) %>%
           rename(!!paste0("GeoUID",ds):="GeoUID")) %>%
  setNames(c("CA06","CA16"))

rents <- tongfen_aggregate(rent_data,correspondence,rent_meta,base_geo="CA16")

rents %>%
  st_drop_geometry() %>%
  filter(TongfenID %in% joins$TongfenID_joined) %>%
  select(TongfenID,rent_2006,rent_2016)

