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/05-redistmetrics-partisan.md
View on GitHub# redistmetrics — Partisan Fairness Metrics
## Overview
`redistmetrics` provides a suite of partisan fairness measures for evaluating how district plans translate votes into seats. These all require election data columns (typically `ndv` for Democratic votes and `nrv` for Republican votes, as provided by the ALARM datasets). All work with `pl()` in `mutate()` pipelines.
## Vote Share Basics
Before computing partisan metrics, understand what columns you're working with:
```r
# Standard ALARM column names
map$ndv # total Democratic votes (summed across recent elections)
map$nrv # total Republican votes
# Two-party Democratic vote share for a district
dvs <- ndv / (ndv + nrv) # values ~0.5 = competitive, >0.5 = Dem-leaning
```
Some metrics aggregate across districts, some return one value per district.
## Efficiency Gap
Measures wasted votes — the difference in wasted votes (votes for losing candidate + votes beyond 50%+1 for the winner) between the two parties, as a fraction of total votes. Negative = Republican advantage.
```r
plans <- plans |>
mutate(egap = part_egap(pl(), ndv, nrv))
# Returns one value per plan (plan-level scalar)
```
```r
# Seats-votes version (uses projected seat share instead of actual seats)
plans <- plans |>
mutate(egap_sv = part_egap_sv(pl(), ndv, nrv))
```
## Mean-Median Difference
Difference between the mean and median district-level Democratic vote share. Positive values favor Democrats; negative values favor Republicans. Robust to outlier districts.
```r
plans <- plans |>
mutate(mm = part_mean_median(pl(), ndv, nrv))
# Returns one value per plan
```
## Partisan Bias
At the hypothetical 50% statewide vote share, what fraction of seats does each party win? Bias = Democratic seat share - 0.5. Negative = Republican advantage.
```r
plans <- plans |>
mutate(bias = part_bias(pl(), ndv, nrv))
```
## Declination
Measures asymmetry in the geographic distribution of party vote shares across districts. Compares the "angle" of Democratic vs. Republican district vote distributions. Values near 0 = symmetric.
```r
plans <- plans |>
mutate(decl = part_decl(pl(), ndv, nrv))
```
## Responsiveness
Measures how much seat share changes in response to a 1-point swing in vote share. Higher responsiveness means the plan is more proportional to vote swings.
```r
plans <- plans |>
mutate(resp = part_resp(pl(), ndv, nrv))
```
## Partisan Advantage (Seat Share)
Count Democratic seats directly:
```r
plans <- plans |>
mutate(
dem_seats = part_dseats(pl(), ndv, nrv), # expected Dem seats
rep_seats = part_rseats(pl(), ndv, nrv) # expected Rep seats
)
```
## District-Level Vote Shares
To get district-level vote shares (one value per district per plan):
```r
plans <- plans |>
mutate(dvs = part_dvs(pl(), ndv, nrv)) # Dem two-party vote share per district
```
## Multiple Elections
The ALARM datasets include multiple election cycles. Compute metrics for each and average for robustness:
```r
# If you have multiple election columns (e.g., gov18, sen18, pre20)
plans <- plans |>
mutate(
egap_gov18 = part_egap(pl(), gov18_dem, gov18_rep),
egap_sen18 = part_egap(pl(), sen18_dem, sen18_rep),
egap_pre20 = part_egap(pl(), pre20_dem, pre20_rep),
egap_avg = (egap_gov18 + egap_sen18 + egap_pre20) / 3
)
```
## Comparing Enacted Plan to Baseline
```r
library(ggplot2)
# Efficiency gap distribution with enacted plan highlighted
enacted_egap <- plans |> filter(draw == 'Enacted') |> pull(egap) |> unique()
plans |>
filter(draw != 'Enacted') |>
ggplot(aes(egap)) +
geom_histogram(bins = 40) +
geom_vline(xintercept = enacted_egap, color = 'red', linewidth = 1) +
labs(x = 'Efficiency Gap', title = 'Simulated distribution vs. enacted plan')
```
## Summary Table
| Function | Returns | Interpretation |
|----------|---------|---------------|
| `part_egap()` | plan-level | Wasted vote diff; <0 = Rep advantage |
| `part_egap_sv()` | plan-level | Seats-votes efficiency gap |
| `part_mean_median()` | plan-level | Mean−median; <0 = Rep advantage |
| `part_bias()` | plan-level | Seat bias at 50% vote; <0 = Rep advantage |
| `part_decl()` | plan-level | Geometric asymmetry measure |
| `part_resp()` | plan-level | Responsiveness to vote swings |
| `part_dseats()` | plan-level | Projected Democratic seat count |
| `part_dvs()` | district-level | Democratic two-party vote share |
## See Also
- Compactness metrics: [04-redistmetrics-compactness.md](04-redistmetrics-compactness.md)
- Splits, minority representation, other metrics: [06-redistmetrics-other.md](06-redistmetrics-other.md)
- Pre-built election data: [12-alarmdata.md](12-alarmdata.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 |