---
title: "Class Notes: Week 2, Monday"
format: html
---

Exploring Available Variables with `load_variables()`

Before querying, explore variable definitions and concept groups across survey years:

# Load standard 5-year detailed ACS variables (e.g., 2022)
```{r}
library(tidycensus)
library(tidyverse)
v_detailed <- load_variables(2022, "acs5", cache = TRUE)
```

# View in interactive data viewer
```{r}
View(v_detailed)
```

## Search for specific demographic or economic concepts
```{r}
v_detailed |> 
  filter(str_detect(concept, regex("POVERTY", ignore_case = TRUE)))
# To view pre-aggregated summary profiles:R# Load Data Profile variables
v_profile <- load_variables(2022, "acs5/profile", cache = TRUE)
```


## Method 1: Fetching an Entire Topic TableIf you need all variables within a specific Census summary table (for example, Table B01001 for Sex by Age), pass the table argument:R# Retrieve Table B01001 (Sex by Age) for all 5 Midwest states
```{r}
midwest_age_table <- get_acs(
  geography = "county",
  state = c("IL", "IN", "MI", "OH", "WI"),
  table = "B01001",
  year = 2022,
  output = "wide"
)
```

## Output contains estimate (E) and margin of error (M) columns for each variable

```{r}
glimpse(midwest_age_table)
```

# Method 2: Fetching Comprehensive Data Profiles (Broad Coverage)

The Census Bureau groups key indicators into four comprehensive Data Profiles:

## format as list

- DP02: Selected Social Characteristics (Education, Marital Status, Veteran Status)
- DP03: Selected Economic Characteristics (Employment, Income, Poverty)
- DP04: Housing Characteristics (Home values, Rents, Mortgage status)
- DP05: Demographic and Housing Estimates (Age, Sex, Race/Ethnicity)

## Retrieve the full Economic Profile (Table DP03)

```{r}
midwest_econ_profile <- get_acs(
  geography = "county",
  state = c("IL", "IN", "MI", "OH", "WI"),
  table = "DP03",
  year = 2022,
  output = "wide"
)
```

# Method 3: Replicating ggplot2::midwest StructureTo build a modern version of ggplot2::midwest, define a named vector of raw counts and calculate rates and percentages using dplyr:R# Define target ACS variable codes
```{r}
midwest_vars <- c(
  pop_total      = "B01003_001", # Total Population
  pop_white      = "B02001_002", # White alone
  pop_black      = "B02001_003", # Black alone
  pop_amerindian = "B02001_004", # American Indian / Alaska Native
  pop_asian      = "B02001_005", # Asian alone
  pop_other      = "B02001_007", # Two or more / other race
  pop_adults     = "B15003_001", # Population 25 years and over
  pop_bachelors  = "B15003_022", # Bachelor's degree (adults)
  pop_poverty    = "B17001_002"  # Population below poverty line
)
```

# Download wide-format county data with spatial boundary geometry
```{r}
midwest_modern_raw <- get_acs(
  geography = "county",
  state = c("IL", "IN", "MI", "OH", "WI"),
  variables = midwest_vars,
  year = 2022,
  output = "wide",
  geometry = TRUE
)
```

# Clean, split county/state names, and calculate derived percentage fields
```{r}
midwest_modern <- midwest_modern_raw |> 
  separate_wider_delim(
    cols = NAME, 
    delim = ", ", 
    names = c("county", "state")
  ) |> 
  mutate(
    pct_white     = (pop_whiteE / pop_totalE) * 100,
    pct_black     = (pop_blackE / pop_totalE) * 100,
    pct_amerindian= (pop_amerindianE / pop_totalE) * 100,
    pct_asian     = (pop_asianE / pop_totalE) * 100,
    pct_college   = (pop_bachelorsE / pop_adultsE) * 100,
    pct_poverty   = (pop_povertyE / pop_totalE) * 100
  ) |> 
  select(GEOID, county, state, pop_total = pop_totalE, starts_with("pct_"), geometry)
```

# View resulting dataset

```{r}
head(midwest_modern)
```
Summary of Query Options

# format as 
::: {.list-table}

- - Approach
  - Function Syntax
  - Best Used When

- - Individual Variables
  - variables = c(name = "CODE")
  - Target metric calculation and custom formulas

- - Data Profile
  - table = "DP03"
  - Broad, multi-variable socioeconomic research

:::

References

Walker, K. (2023). Analyzing US Census Data: Methods, Maps, and Models in R. Chapman and Hall/CRC.US Census Bureau. American Community Survey (ACS) 5-Year Data.