Skip to content

brycewang-stanford/Auto-Empirical-Research-Skills / skills/22-christopherkenny-skills/skills/redistricting-analysis

redistricting-analysis

Verified

Redistricting 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.

references/15-baf.md

View on GitHub
# baf — Census Block Assignment File Downloader

## Overview

`baf` downloads official Block Assignment Files (BAFs) directly from the U.S. Census Bureau. BAFs are the authoritative source for enacted district assignments — they map every Census block to a district for a given geography and year. This is not a general-purpose BAF reader/writer; it is specifically for retrieving Census Bureau-published BAFs.

For converting between BAF formats and redistricting plan objects, see `geomander::baf_to_vtd()` ([10-geomander-spatial.md](10-geomander-spatial.md)) and `alarm_add_plan()` ([12-alarmdata.md](12-alarmdata.md)).

## Downloading BAFs

```r
library(baf)

# Download a block assignment file from the Census Bureau
# Returns a tibble mapping Census block GEOIDs to district identifiers
cd_baf <- baf(
  state = 'MD',
  year = 2024,
  geographies = 'cd'      # see geography codes below
)
# columns: BLOCKID (or GEOID20), district identifier
```

### Geography Codes

| Code | Geography |
|------|-----------|
| `"cd"` | Congressional districts |
| `"sldu"` | State legislative upper chamber |
| `"sldl"` | State legislative lower chamber |
| `"vtd"` | Voting tabulation districts |
| `"county"` | Counties |
| `"cousub"` | County subdivisions |
| `"concity"` | Consolidated cities |
| `"elsd"` | Elementary school districts |
| `"scsd"` | Secondary school districts |
| `"unsd"` | Unified school districts |

```r
# Multiple geographies at once
baf_data <- baf(state = 'MD', year = 2020,
  geographies = c('cd', 'sldu', 'sldl'))
# Returns a list of tibbles, one per geography
```

### Caching

```r
# Cache to a persistent directory (survives sessions)
cd_baf <- baf(state = 'MD', year = 2024, geographies = 'cd',
  cache_to = '~/data/baf_cache')

# Or set globally
options(baf.use_cache = TRUE)
cd_baf <- baf(state = 'MD', year = 2024, geographies = 'cd')
```

## Cleaning BAFs

```r
# Clean raw downloaded BAF data (standardizes column names and formats)
clean_baf <- clean_bafs(cd_baf)
```

## Example Data

```r
# Small example BAF (Adams County, Washington) for testing
data(example_baf)
head(example_baf)
```

## Using Census BAFs in Redistricting Analysis

### Verify an enacted plan

```r
library(baf)
library(redistverse)

# Download enacted congressional districts for Maryland
enacted_baf <- baf('MD', year = 2022, geographies = 'cd')

# Match to your precinct-level map
# Since BAFs are block-level, need to aggregate to precincts
# Use geomander::baf_to_vtd() if your map uses VTDs (see 10-geomander-spatial.md)
# Or manually aggregate:
enacted_plan <- enacted_baf |>
  left_join(block_to_precinct_crosswalk, by = c('BLOCKID' = 'GEOID20')) |>
  group_by(precinct_id) |>
  summarize(district = as.integer(names(which.max(table(cd))))) |>
  pull(district)

# Add to ensemble for comparison
plans <- alarm_add_plan(plans, enacted_plan, map, name = 'Enacted_2022')
```

### Download path utilities

```r
# Find where a BAF would be cached (without downloading)
path <- baf_download_path(state = 'MD', year = 2024, geographies = 'cd')

# Download a file explicitly
baf_download(state = 'MD', year = 2024, geographies = 'cd',
  path = '~/data/md_cd_2024.zip')
```

## See Also

- Convert BAF plans to VTD-level assignments: `geomander::baf_to_vtd()` in [10-geomander-spatial.md](10-geomander-spatial.md)
- Add plans to an ensemble: `alarm_add_plan()` in [12-alarmdata.md](12-alarmdata.md)
- Census block GEOIDs and geography: [13-pl94171.md](13-pl94171.md)
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 323kUpdated Claude CodeCodexVerifiedInstall
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: 198kUpdated Claude CodeCodexVerifiedInstall
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: 119kUpdated Claude CodeCodexVerifiedInstall
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: 118kUpdated Claude CodeCodexVerifiedInstall