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/11-geomander-data.md

View on GitHub
# geomander — Data Downloads

## Overview

`geomander` includes functions for downloading pre-processed election and geographic datasets from several sources: VEST (Voting and Election Science Team), the ALARM Project, Dave's Redistricting App (DRA), and the Harvard Election Data Archive (HEDA). These are useful when you need election data for states not yet in `alarmdata` or when working with historical elections.

## VEST Election Data

VEST provides precinct-level election returns for many states and election cycles, harmonized to a consistent format.

```r
library(geomander)

# List available states
vest_states()    # returns a tibble of state/year combinations available

# Download VEST data for a state and year
vest_data <- get_vest(state = 'VA', year = 2018)
# Returns an sf object with precinct geometry + election columns
```

Column naming convention: `G{YY}{OFFICE}{PARTY}{CANDIDATE}` e.g., `G18USSRKAV` = 2018 US Senate, Republican, Kavanaugh.

```r
# Clean VEST column names to a standardized format
cleaned <- clean_vest(vest_data)
```

## ALARM Project Data

Download ALARM Project datasets (alternative to the `alarmdata` package functions):

```r
# List available states in the ALARM dataset
alarm_states()   # tibble of states and what's available

# Download an ALARM dataset
alarm_data <- get_alarm(state = 'MD')
# Returns a list with map and plans components
```

Note: For most work with ALARM data, `alarmdata::alarm_50state_map()` and `alarm_50state_plans()` are more convenient (see [12-alarmdata.md](12-alarmdata.md)).

## Dave's Redistricting App (DRA)

Import and export plans to/from Dave's Redistricting App format:

```r
# Download a plan from DRA (requires the plan's share link/ID)
dra_plan <- get_dra(url = 'https://davesredistricting.org/maps#...')

# Convert a DRA-exported CSV to an R assignment vector
# DRA exports block-level assignments as CSV
r_plan <- dra2r(
  dra = dra_export_df,   # data frame from DRA CSV export
  shp = precincts_sf,    # precinct shapefile to match against
  id = 'GEOID20'        # GEOID column in shapefile
)

# Convert an R plan vector back to DRA format
dra_df <- r2dra(
  plan = r_plan_vector,
  shp = precincts_sf,
  id = 'GEOID20'
)
```

## Harvard Election Data Archive (HEDA)

Older historical election returns at the county level:

```r
# List available states
heda_states()

# Download HEDA data
heda_data <- get_heda(state = 'VA')
```

## Historical Congressional District Shapefiles

```r
# Download historical US congressional district boundaries (Lewis et al.)
# Available for many Congresses going back to the 1st
hist_districts <- get_lewis(congress = 117)   # 117th Congress
hist_districts <- get_lewis(congress = 100)   # historical Congresses
```

## Racially Polarized Voting Data

```r
# Download RPV (Racially Polarized Voting) dataset for nearby precincts
rpv_data <- get_rpvnearme(state = 'VA', county = '013')
```

## Regionalization

Estimate geographic regions based on spatial features (useful for identifying natural communities or geographic clusters):

```r
regions <- regionalize(
  shp = precincts_sf,
  adj = adj,
  nregions = 5,
  wts = precincts_sf$pop
)
```

## Count Connections

Count how many times precincts share adjacency connections (useful for analyzing plan robustness):

```r
connection_counts <- count_connections(plans = plans, adj = adj)
```

## Typical Workflow: Adding VEST Data to a Custom Map

```r
library(redistverse)
library(geomander)

# 1. Load precinct shapefile (from local source or tinytiger)
precincts <- st_read('md_precincts_2020.shp')

# 2. Download VEST election data for Maryland 2020
vest_md <- get_vest(state = 'MD', year = 2020)

# 3. Match VEST precincts to your precincts
matches <- geo_match(from = vest_md, to = precincts, method = 'area')

# 4. Transfer election columns using estimation
for (col in grep('^G20', names(vest_md), value = TRUE)) {
  precincts[[col]] <- estimate_up(
    estimate_down(wts = vest_md$vap, value = vest_md[[col]],
                  group = seq_len(nrow(vest_md))),
    group = matches
  )
}

# 5. Build redist_map
map <- redist_map(precincts, pop_col = 'pop', ndists = 8, pop_tol = 0.005)
```

## See Also

- Adjacency tools: [09-geomander-adjacency.md](09-geomander-adjacency.md)
- Spatial estimation (geo_match, estimate_down): [10-geomander-spatial.md](10-geomander-spatial.md)
- Pre-built ALARM data (easier for common states): [12-alarmdata.md](12-alarmdata.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