brycewang-stanford/Auto-Empirical-Research-Skills / skills/22-christopherkenny-skills/skills/redistricting-analysis
redistricting-analysis
VerifiedRedistricting analysis in R using the redistverse ecosystem. Use whenever the user is working with redist, redistmetrics, ggredist, geomander, adj, alarmdata, PL94171, censable, easycensus, tinytiger, baf, rict, or redistio. Covers the complete pipeline: Census and spatial data loading, adjacency graph construction, SMC/MCMC simulation, constraints (population balance, county splits, VRA compliance), convergence diagnostics, plan metrics (compactness, partisan fairness, splits), visualization, summary tables, and interactive plan drawing. Invoke whenever the user mentions redistricting, gerrymandering, district plans, simulation ensembles, or any redistverse package by name.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 5.8 KB |
| references/01-redist-simulation.md | markdown | 5.7 KB |
| references/02-redist-constraints.md | markdown | 5.0 KB |
| references/03-redist-advanced.md | markdown | 3.2 KB |
| references/04-redistmetrics-compactness.md | markdown | 4.1 KB |
| references/05-redistmetrics-partisan.md | markdown | 4.6 KB |
| references/06-redistmetrics-other.md | markdown | 4.5 KB |
| references/07-ggredist.md | markdown | 4.6 KB |
| references/08-adj.md | markdown | 4.2 KB |
| references/09-geomander-adjacency.md | markdown | 6.1 KB |
| references/10-geomander-spatial.md | markdown | 6.7 KB |
| references/11-geomander-data.md | markdown | 4.3 KB |
| references/12-alarmdata.md | markdown | 4.2 KB |
| references/13-pl94171.md | markdown | 3.6 KB |
| references/14-census-utilities.md | markdown | 5.2 KB |
| references/15-baf.md | markdown | 3.4 KB |
| references/16-rict.md | markdown | 3.4 KB |
| references/17-redistio.md | markdown | 4.3 KB |
references/14-census-utilities.md
View on GitHub# Census Utilities — censable, easycensus, tinytiger
## censable
`censable` provides Census data retrieval and standardization tools. It wraps `censusapi` for common redistricting-oriented requests, with consistent column naming across years and survey types. Requires a Census API key (set with `add_r_environ()`).
### Building Census Data
```r
library(censable)
# Decennial Census (redistricting data)
dec_data <- build_dec(
geography = 'block', # 'block', 'tract', 'county', 'state'
state = 'MD',
county = NULL, # optional FIPS code for a single county
year = 2020 # 2000, 2010, or 2020
)
# American Community Survey (5-year estimates)
acs_data <- build_acs(
geography = 'tract',
state = 'MD',
year = 2020
)
# Memoized versions (cached in memory — fast for repeated calls in a session)
dec_data <- mem_build_dec(geography = 'tract', state = 'MD', year = 2020)
acs_data <- mem_build_acs(geography = 'tract', state = 'MD', year = 2020)
```
Output columns follow the pattern: `pop` (total), `pop_white`, `pop_black`, `pop_hisp`, `pop_asian`, `pop_aian`, `pop_nhpi`, `pop_other` and equivalent `vap_*` columns for voting-age population.
### Collapsing Race Categories
For analyses that use fewer race categories, collapse the detailed columns:
```r
# Collapse to 4 groups: white, black, hispanic, other
df4 <- collapse4(df) # adds/replaces standard columns
pop4 <- collapse4_pop(df) # population columns only
vap4 <- collapse4_vap(df) # VAP columns only
# Collapse to 5 groups: white, black, hispanic, asian, other
df5 <- collapse5(df)
pop5 <- collapse5_pop(df)
vap5 <- collapse5_vap(df)
```
### State Identifier Conversion
`censable` provides a complete set of functions for converting between state abbreviations, FIPS codes, ANSI codes, and names:
```r
# Convert abbreviation → FIPS
recode_abb_fips('MD') # '24'
recode_abb_name('MD') # 'Maryland'
recode_fips_abb('24') # 'MD'
recode_name_abb('Maryland') # 'MD'
# Add identifier columns to a data frame
df <- join_fips_abb(df, fips_col = 'STATEFP') # adds 'abb' column
df <- join_abb_name(df, abb_col = 'state') # adds 'name' column
# Flexible matching (handles abbreviations, FIPS, ANSI, and names as input)
match_abb('Maryland') # 'MD'
match_fips('MD') # '24'
```
The 12 `join_*()` functions and 4 `match_*()` functions cover all combinations of {abb, ansi, fips, name}.
### GEOID Tools
```r
# Decompose a Census GEOID into its component parts
breakdown_geoid('240010001001000')
# Returns: state='24', county='001', tract='000100', block_group='1', block='000'
# Build a GEOID from standard columns
construct_geoid(df, level = 'block') # constructs GEOID from state/county/tract/block cols
# Custom GEOID construction
custom_geoid(df, cols = c('state', 'county', 'tract'))
```
### API Key Management
```r
has_census_key() # TRUE/FALSE — is a key set?
get_census_key() # retrieve the key
add_r_environ() # opens .Renviron for adding CENSUS_API_KEY=your_key
```
---
## easycensus
`easycensus` provides a simpler interface for ACS data with built-in variable search. Useful for finding and downloading specific demographic or socioeconomic tables.
```r
library(easycensus)
# Search for tables by keyword
find_vars('median household income')
find_vars('educational attainment')
# Download a specific ACS variable
income <- get_acs_wide(
geography = 'tract',
state = 'MD',
variables = c(medinc = 'B19013_001'),
year = 2020
)
# Download a full ACS table
educ <- get_acs_table(
geography = 'tract',
state = 'MD',
table = 'B15003',
year = 2020
)
```
---
## tinytiger
`tinytiger` downloads TIGER/Line shapefiles from the Census Bureau. Returns `sf` objects in NAD83 (EPSG:4269).
```r
library(tinytiger)
# Geographic boundaries
tt_states(year = 2020)
tt_counties(state = 'MD', year = 2020)
tt_tracts(state = 'MD', county = '003', year = 2020) # county FIPS
tt_block_groups(state = 'MD', year = 2020)
tt_blocks(state = 'MD', county = '003', year = 2020) # large files
# Election-related geographies
tt_voting_districts(state = 'MD', year = 2020) # VTDs / precincts
tt_congressional_districts(state = 'MD', year = 2020)
tt_state_legislative_districts(state = 'MD', year = 2020, chamber = 'upper')
tt_state_legislative_districts(state = 'MD', year = 2020, chamber = 'lower')
# Places (cities, towns)
tt_places(state = 'MD', year = 2020)
```
Caching:
```r
options(tigris_use_cache = TRUE) # tinytiger follows tigris cache conventions
```
---
## Typical Combined Workflow
```r
library(redistverse)
library(censable)
library(tinytiger)
# 1. Get VTDs as precinct approximations
vtds <- tt_voting_districts('MD', year = 2020)
# 2. Download 2020 Census redistricting data at VTD level
pop <- build_dec(geography = 'voting district', state = 'MD', year = 2020)
# 3. Join
map_sf <- vtds |> left_join(pop, by = 'GEOID')
# 4. Build redist_map
map <- redist_map(map_sf, pop_col = 'pop', ndists = 8, pop_tol = 0.005)
```
## See Also
- Decennial Census P.L. 94-171 data: [13-pl94171.md](13-pl94171.md)
- Pre-built maps (skip manual assembly): [12-alarmdata.md](12-alarmdata.md)
- Spatial aggregation (block→precinct): [10-geomander-spatial.md](10-geomander-spatial.md)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |