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/09-geomander-adjacency.md

View on GitHub
# geomander — Adjacency and Contiguity

## Overview

`geomander` provides the primary tools for building, inspecting, and repairing precinct adjacency graphs in redistricting. This file covers adjacency construction, contiguity checking, edge editing, and the seam tools for removing edges along administrative boundaries. See also [10-geomander-spatial.md](10-geomander-spatial.md) for spatial estimation and [11-geomander-data.md](11-geomander-data.md) for data downloads.

## Building an Adjacency List

```r
library(geomander)

# Build from an sf shapefile (returns a zero-indexed list of neighbor vectors)
adj <- adjacency(shp = precincts_sf)

# Store in a redist_map
map <- redist_map(precincts_sf, pop_col = 'pop', ndists = 8, adj = adj)
```

Note: The `adj` package's `adj_from_shp()` provides a similar function with a validated S3 class. The `geomander::adjacency()` output is a plain list — compatible with `redist` which accepts either.

## Checking Contiguity

After building or modifying an adjacency graph, always verify connectivity within each group (e.g., each county or district):

```r
# Full contiguity report — returns tibble with group, group_number, component
check_contiguity(adj = adj, group = map$county)
# component == 1 for all rows = fully connected within each county
# component > 1 indicates disconnected islands

# Aliases:
cct(adj, group)   # frequency table of component counts (1 = all contiguous)
ccm(adj, group)   # max component number (1 = all contiguous)

# Check overall graph (no groups — are all precincts reachable?)
check_contiguity(adj = adj)  # group defaults to 1 (single group)

# Check polygon contiguity (geometry-based, no adj needed)
check_polygon_contiguity(shp = precincts_sf, group = precincts_sf$county)
```

**Interpretation**: If `ccm(adj, group) > 1`, some group has a disconnected component — precincts in that group that cannot reach each other. This usually needs fixing before simulation.

## Finding and Fixing Disconnected Components

```r
# Suggest which edges to add to connect isolated precincts
suggest_neighbors(adj = adj, shp = precincts_sf, idx = isolated_precinct_idx)

# Suggest how to connect disconnected components within groups
suggest_component_connection(adj = adj, shp = precincts_sf, group = map$county)
# Returns a data frame of suggested (from, to) edge pairs to add
```

## Editing Edges

```r
# Add an edge between precinct i and precinct j (zero-indexed)
adj <- add_edge(adj, v1 = 42L, v2 = 107L)

# Remove an edge
adj <- subtract_edge(adj, v1 = 42L, v2 = 107L)

# Vectorized (add/remove multiple edges at once)
adj <- subtract_edge(adj, v1 = c(3L, 8L), v2 = c(15L, 22L))
```

Note: `add_edge()` and `subtract_edge()` are geomander's versions; the `adj` package provides equivalent `adj_add_edges()` and `adj_subtract_edges()`.

## Comparing Adjacency Lists

Useful for checking what changed after manual edits:

```r
compare_adjacencies(adj1 = original_adj, adj2 = modified_adj)
# Returns a tibble of edges that differ between the two graphs
```

## Seam Tools — Removing Edges Along Administrative Boundaries

The seam functions handle edges that cross between specified administrative units. This is the primary approach for water-body adjacency correction.

### `seam_rip()` — Remove edges along a boundary

Removes all adjacency edges that cross between precincts on opposite sides of a specified seam.

```r
seam_rip(
  adj = adj,
  shp = map,           # sf tibble with the admin column
  admin = 'county',      # column defining the administrative units
  seam = c('Orange', 'Rockland'),  # units to disconnect from each other
  epsg = 3857           # projection for spatial operations
)
```

**Key distinction from `adj_subtract_edges()`**: `seam_rip()` works at the administrative unit level — you name which counties/units to separate, and it finds and removes all edges between them automatically. `adj_subtract_edges()` requires you to specify exact precinct pairs.

### `seam_adj()` — Inspect edges along a border

Returns only the edges that run along a specified boundary (useful for verification):

```r
border_edges <- seam_adj(adj = adj, shp = map, admin = 'county',
  seam = c('Orange', 'Rockland'))
# tibble of (from, to) pairs that cross the seam
```

### `seam_geom()` — Subset precincts along a border

Returns the subset of precincts that lie along a specified boundary:

```r
border_precincts <- seam_geom(shp = map, admin = 'county',
  seam = c('Orange', 'Rockland'))
```

### `seam_sew()` — Suggest connections across a border

When a border should be crossable (e.g., a bridge connection), suggest which edges to add:

```r
suggested_edges <- seam_sew(adj = adj, shp = map, admin = 'county',
  seam = c('Bronx', 'Queens'))
# Returns (from, to) pairs to add with add_edge()
```

## Visualization

```r
# Plot the shapefile with row numbers labeled (useful for identifying precinct indices)
geo_plot(shp = precincts_sf)

# Plot groups with connected components colored differently
# Disconnected components appear in different colors
geo_plot_group(shp = precincts_sf, adj = adj, group = precincts_sf$county)
```

## Common Workflow: Water-Body Adjacency Correction

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

# Load map
map <- alarm_50state_map('MD')

# Build adjacency (or use the pre-built one)
adj <- map$adj[[1]]

# Identify which counties are separated by the Chesapeake Bay
eastern_shore_counties <- c('Kent', "Queen Anne's", 'Talbot', 'Caroline',
  'Dorchester', 'Wicomico', 'Somerset', 'Worcester')
western_counties <- c('Anne Arundel', 'Baltimore', ...)  # counties on west side

# Check if cross-bay edges exist
border_edges <- seam_adj(adj, map, admin = 'county',
  seam = eastern_shore_counties)

# Remove cross-bay edges
adj_fixed <- seam_rip(adj, map, admin = 'county',
  seam = eastern_shore_counties)

# Verify fix
ccm(adj_fixed, map$county)   # should still be 1 within each county

# Store back in map
map$adj <- list(adj_fixed)
```

## See Also

- Spatial estimation and block aggregation: [10-geomander-spatial.md](10-geomander-spatial.md)
- Data downloads (VEST, ALARM, DRA): [11-geomander-data.md](11-geomander-data.md)
- `adj` package equivalents (validated S3 class): [08-adj.md](08-adj.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