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/04-redistmetrics-compactness.md
View on GitHub# redistmetrics — Compactness Metrics
## Overview
`redistmetrics` computes plan-level and district-level metrics for evaluating redistricting plans. All functions work with integer vectors, matrices, or `redist_plans` objects. In `mutate()` pipelines, use `pl()` as the pronoun for the plan assignment.
## The `pl()` Pronoun
Inside `mutate()` on a `redist_plans` object, `pl()` returns the current plan assignments. This is the standard way to compute metrics across an ensemble:
```r
plans <- plans |>
mutate(
polsby = comp_polsby(pl(), map), # district-level (vector per plan)
fk = comp_frac_kept(pl(), map) # plan-level scalar per plan
)
```
## Precomputing Perimeters
Geometric compactness measures (Polsby-Popper, Schwartzberg) require perimeter calculations. Precompute once and pass the result to all geometric metrics:
```r
perim_df <- prep_perims(map) # returns a data frame; expensive to compute
plans <- plans |>
mutate(
polsby = comp_polsby(pl(), map, perim_df = perim_df),
schw = comp_schwartzberg(pl(), map, perim_df = perim_df)
)
```
## Geometric Compactness
These measure the shape of district boundaries. Higher = more compact.
### Polsby-Popper
Most commonly used. Ratio of district area to the area of a circle with the same perimeter. Range: (0, 1].
```r
# District-level (one value per district per plan)
pp <- comp_polsby(plans, shp = map, perim_df = perim_df)
# Or in mutate:
plans <- plans |> mutate(polsby = comp_polsby(pl(), map, perim_df))
```
### Reock
Ratio of district area to the area of the smallest enclosing circle. Range: (0, 1].
```r
plans <- plans |> mutate(reock = comp_reock(pl(), map))
```
### Convex Hull
Ratio of district area to the area of its convex hull. Range: (0, 1].
```r
plans <- plans |> mutate(cvx = comp_convex_hull(pl(), map))
```
### Schwartzberg
Ratio of district perimeter to the perimeter of a circle with the same area. Range: ≥ 1 (lower = more compact).
```r
plans <- plans |> mutate(schw = comp_schwartzberg(pl(), map, perim_df))
```
## Graph-Based Compactness
These use the adjacency graph rather than geometry. Faster to compute and useful when geometric data is unavailable.
### Fraction of Edges Kept (`comp_frac_kept`)
Proportion of precinct-level adjacency edges that are internal to a district (not cut). Plan-level scalar. Higher = more compact.
```r
plans <- plans |> mutate(frac_kept = comp_frac_kept(pl(), map))
```
This is the compactness measure built into `redist_smc` via the `compactness` parameter.
### Log Spanning Trees (`comp_log_st`)
Log of the number of spanning trees within each district. Related to the probability of the district being sampled. Useful for weighting analyses.
```r
plans <- plans |> mutate(log_st = comp_log_st(pl(), map))
```
## Summary Statistics
Compactness metrics return district-level vectors. To get plan-level summaries for comparing against the enacted plan:
```r
plans <- plans |>
mutate(
polsby = comp_polsby(pl(), map, perim_df),
polsby_min = min(polsby), # worst district in each plan
polsby_mean = mean(polsby) # average across districts
)
```
Or use `group_by(draw)` if working outside `mutate()`:
```r
plans |>
group_by(draw) |>
summarize(min_polsby = min(polsby), mean_polsby = mean(polsby))
```
## Comparison Table
| Function | Type | Input needed | Range | Higher = |
|----------|------|-------------|-------|---------|
| `comp_polsby()` | geometric | `perim_df` | (0, 1] | more compact |
| `comp_reock()` | geometric | geometry | (0, 1] | more compact |
| `comp_convex_hull()` | geometric | geometry | (0, 1] | more compact |
| `comp_schwartzberg()` | geometric | `perim_df` | ≥ 1 | less compact |
| `comp_frac_kept()` | graph | adjacency | (0, 1] | more compact |
| `comp_log_st()` | graph | adjacency | unbounded | more connected |
## See Also
- Add compactness as a soft constraint: [02-redist-constraints.md](02-redist-constraints.md) (`add_constr_polsby`)
- Partisan metrics: [05-redistmetrics-partisan.md](05-redistmetrics-partisan.md)
- Other metrics (splits, segregation): [06-redistmetrics-other.md](06-redistmetrics-other.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 |