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/12-alarmdata.md

View on GitHub
# alarmdata — Pre-built Redistricting Datasets

## Overview

`alarmdata` provides access to the ALARM Project's pre-compiled redistricting datasets hosted on Harvard Dataverse. These include precinct-level shapefiles, 2020 Census demographics, VEST election returns, and pre-simulated ensembles of 5,000 plans for all 50 U.S. states. Using these saves hours of data wrangling for common redistricting analyses.

## Loading Pre-built Map Data

```r
library(redistverse)

# Download map for a state (returns redist_map object)
map <- alarm_50state_map('MD')   # Maryland — use 2-letter postal code
map <- alarm_50state_map('NC')
map <- alarm_50state_map('TX')
```

The returned `redist_map` includes:
- Precinct-level `sf` geometry
- Pre-built adjacency graph (`adj` column)
- Population and demographic variables
- Election return variables
- Enacted district assignment (e.g., `cd_2020` for congressional)

## Standard Column Names

| Column | Description |
|--------|-------------|
| `pop` | 2020 Census total population |
| `pop_white` | Non-Hispanic White population |
| `pop_black` | Black/African American population |
| `pop_hisp` | Hispanic/Latino population |
| `pop_asian` | Asian population |
| `pop_aian` | American Indian/Alaska Native |
| `pop_nhpi` | Native Hawaiian/Pacific Islander |
| `pop_other` | Other/multiracial |
| `pop_vap` | Voting-age population (18+) |
| `pop_bvap` | Black voting-age population |
| `ndv` | Composite Democratic votes (across recent elections) |
| `nrv` | Composite Republican votes |
| `cd_2020` | Enacted congressional district assignment |
| `state_sen_2020` | Enacted state senate assignment (where available) |
| `state_leg_2020` | Enacted state house assignment (where available) |
| `adj` | Pre-built adjacency list |
| `geometry` | Precinct polygon geometry (sf) |

Individual election columns follow patterns like `pre_20_dem_bid` (2020 presidential, Democratic, Biden).

## Loading Pre-simulated Plan Ensembles

Download the ALARM Project's pre-computed ensemble of 5,000 plans:

```r
plans <- alarm_50state_plans('MD')    # returns redist_plans with 5000 simulated plans

# Plans already include key metrics:
# draw, district, pop_dev, comp_edge, etc.
names(plans)
```

Download statistics only (no full plan matrix, smaller download):

```r
stats <- alarm_50state_stats('MD')
```

## Adding Reference Plans

Compare an enacted or proposed plan to the simulated ensemble:

```r
# From an integer vector (precinct → district, 1-indexed)
plans <- alarm_add_plan(plans, ref_plan = map$cd_2020, map = map, name = 'Enacted')

# From a BAF data frame (GEOID → district mapping)
plans <- alarm_add_plan(plans, ref_plan = baf_df, map = map, name = 'Proposed')

# From an sf object with district polygons
plans <- alarm_add_plan(plans, ref_plan = district_sf, map = map, name = 'Commission')
```

## Caching

By default, downloaded files go to a temporary directory and are not retained between sessions. Enable persistent caching:

```r
# Enable caching (survives R sessions)
options(alarm.use_cache = TRUE)

# Check cache size
alarm_cache_size()

# Clear the cache
alarm_cache_clear()
```

The cache location is managed by `rappdirs` (platform-appropriate user data directory).

## Typical Full Workflow

```r
library(redistverse)

# 1. Load data
map <- alarm_50state_map('MD')
plans <- alarm_50state_plans('MD')

# 2. Add enacted plan for comparison
plans <- alarm_add_plan(plans, map$cd_2020, map, 'Enacted')

# 3. Compute additional metrics
plans <- plans |>
  mutate(
    egap = part_egap(pl(), ndv, nrv),
    splits = splits_admin(pl(), map, map$county)
  )

# 4. Summarize
plans |>
  filter(draw != 'Enacted') |>
  summarize(
    mean_egap = mean(egap),
    pct_enacted = mean(egap < filter(plans, draw == 'Enacted')$egap[[1]])
  )
```

## State Coverage

All 50 U.S. states for both congressional (`ndists` = state's delegation) and, for most states, state legislative chambers. Use `alarm_50state_map()` with the postal code. The dataset uses 2020 Census geography.

## See Also

- Census data (when building your own map): [13-pl94171.md](13-pl94171.md)
- Block assignment file downloads: [15-baf.md](15-baf.md)
- Simulation workflow: [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