# census_workshop.R
#
# This is my code for learning how to use tidycensus and other stuff.
# For the MI-Support workshop in Ingham County, 24 July 2026
#
# Authors: Jack Jacobs (jckjcbs@umich.edu)


#### Setup ####

my_dir <- this.path::this.dir()

# install.packages(c(
#   "yaml","tidycensus","sf","tigris","ggmap"
# ))

library(yaml)
library(tidyverse)
library(tidycensus)
library(sf)
library(tigris)
# library(ggmap)
library(this.path)

# Read in our API key and register the key with tidycensus
# If you need a key: https://api.census.gov/data/key_signup.html
api_key <- read_yaml(here("auth.yaml"))$census_api_key
census_api_key(api_key)

# Our first task: get populations for every year that we have in our MDSS query
years <- read_csv(here("data/FAKE_pertussis_mdss.csv"), skip = 1) |> 
  mutate(year = Diagnosis_Date |> mdy() |> year()) |> 
  pull(year) |> 
  unique()
# years <- 2015:2024

# Oh wait, we need to know what variables we are looking for.
# We know what survey we're looking in: the ACS-5!
vars <- c("B01001_002","B01001_026")

# Let's construct our first tidycensus query!
pop2015 <- get_acs(
  geography = "county",
  variables = vars,
  year = 2015,
  state = "MI",
  county = "Ingham",
  survey = "acs5"
)

# Let's use purrr::map()!
# To use map(), we'll need to define our own function
get_acs5 <- function(
  year, variables = c("B01001_002","B01001_026"),
  state = "MI", county = "Ingham", survey = "acs5"
) {
  get_acs(
    geography = "county",
    variables = variables,
    year = year,
    state = state,
    county = county,
    survey = survey
  ) |> 
    mutate(year = year, survey = survey)
}

# Let's map it!
pop <- years |> 
  map(get_acs5) |> 
  bind_rows() |> 
  mutate(sex = variable |> recode_values(
    vars[1] ~ "Male",
    vars[2] ~ "Female"
  )) |> 
  select(year, sex, estimate, moe)

# If we want to map more inputs than just years, we can create a table of all
#   combinations of the inputs we'd like using `tidyr::expand_grid()`, then
#   piping this into `purrr::pmap()`.
expanded_inputs <- expand_grid(
  year = 2015:2024,
  county = c("Ingham","Washtenaw"),
  survey = c("acs1","acs5")
)

# Expanded map
expanded_results <- expanded_inputs |> 
  filter_out(survey == "acs1" & year == 2020) |>  # no ACS-1 estimates in 2020
  pmap(get_acs5) |> 
  bind_rows() |> 
  mutate(
    sex = variable |> recode_values(
      vars[1] ~ "Male",
      vars[2] ~ "Female"
    ),
    geo = NAME |> str_split_i(" County", 1)
  ) |> 
  select(survey, geo, year, sex, estimate, moe)


#### tigris ####

# Get Ingham ZCTAs
zips_48_49 <- zctas(starts_with = c("48","49"))
zips_48_49 <- zips_48_49 |> 
  st_transform(4326)

# Get Ingham County polygon
ingham <- st_read(here("data/mi_counties.geojson")) |> 
  filter(Name == "Ingham") |> 
  st_transform()

# Filter the ZIP codes by Ingham County's borders
zips_ingham <- zips_48_49 |> 
  st_intersection(ingham)

# Can we see it?
zips_ingham |> 
  ggplot() + 
  geom_sf() +
  geom_sf_text(aes(label = GEOID20), size = 2)
