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/13-pl94171.md

View on GitHub
# PL94171 — Census Decennial Data

## Overview

`PL94171` ingests Census P.L. 94-171 redistricting data — the official data file used for redistricting after each decennial Census. It provides block-level population and race/ethnicity counts required to meet equal-population and Voting Rights Act standards.

## Loading Census Data

```r
library(PL94171)

# Download P.L. 94-171 data for a state
md_data <- pl_raw('MD', year = 2020)   # 2020 Census redistricting file

# Process into a tidy format
md_tidy <- pl_tidy(md_data)
```

## Key Tables

The P.L. 94-171 file has three tables:

| Table | Contents |
|-------|---------|
| P1 | Race alone (total population) |
| P2 | Hispanic or Latino + race (total population) |
| P3 | Race alone (18+ voting-age population) |
| P4 | Hispanic or Latino + race (18+ voting-age population) |

```r
# Access specific tables
p1 <- pl_tidy(md_data, table = 'P1')   # total population by race
p3 <- pl_tidy(md_data, table = 'P3')   # VAP by race
```

## Standard Population Variables

After processing with `pl_tidy()`:

| Variable | Description |
|----------|-------------|
| `pop` | Total population |
| `pop_hisp` | Hispanic/Latino (any race) |
| `pop_white` | Non-Hispanic White alone |
| `pop_black` | Non-Hispanic Black/AA alone |
| `pop_aian` | Non-Hispanic American Indian/Alaska Native |
| `pop_asian` | Non-Hispanic Asian alone |
| `pop_nhpi` | Non-Hispanic Native Hawaiian/Pacific Islander |
| `pop_other` | Non-Hispanic other/multiracial |
| `vap` | Voting-age population (18+) |
| `vap_hisp` | Hispanic/Latino VAP |
| `vap_white` | Non-Hispanic White VAP |
| `vap_black` | Non-Hispanic Black VAP |
| (etc.) | Same race breakdown for VAP |

## Geographic Levels

```r
# Load at different geographic levels
blocks <- pl_raw('MD', 2020, level = 'block')      # Census blocks (finest)
tracts <- pl_raw('MD', 2020, level = 'tract')
counties <- pl_raw('MD', 2020, level = 'county')

# Most redistricting work uses blocks, then aggregates to precincts
```

## Aggregating Blocks to Precincts

Census blocks must typically be aggregated to the precinct or VTD (voting tabulation district) level for redistricting analysis:

```r
library(tidyverse)
library(PL94171)
library(sf)

# 1. Load block data with geometry
blocks <- pl_raw('MD', 2020, level = 'block', geometry = TRUE)
blocks <- pl_tidy(blocks)

# 2. Load precinct shapefile
precincts <- st_read('path/to/precincts.shp')

# 3. Match blocks to precincts by centroid
block_assignments <- geo_match(st_centroid(blocks), precincts)

# 4. Aggregate population to precincts
precinct_pop <- blocks |>
  mutate(precinct = block_assignments) |>
  group_by(precinct) |>
  summarize(across(c(pop, pop_black, pop_hisp, vap, vap_black), sum))

# 5. Join to precinct shapefile
precincts <- precincts |>
  left_join(precinct_pop, by = ...)
```

## VTDs (Voting Tabulation Districts)

The Census also provides VTDs, which roughly correspond to precincts:

```r
vtds <- pl_raw('MD', 2020, level = 'vtd', geometry = TRUE)
vtds <- pl_tidy(vtds)
```

VTDs are often the best starting point for precinct-level data since they are designed to align with election administration boundaries.

## Building a Custom `redist_map`

After assembling precinct-level data:

```r
library(redistverse)

map <- redist_map(
  shp = precincts_sf,    # sf object with geometry + pop columns
  pop_col = 'pop',
  ndists = 8,
  pop_tol = 0.005
)
```

## See Also

- Pre-built maps (skip manual Census assembly for common states): [12-alarmdata.md](12-alarmdata.md)
- Census variable names and ACS data: [14-census-utilities.md](14-census-utilities.md)
- Creating `redist_map` from custom data: [01-redist-simulation.md](01-redist-simulation.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