Learn R Programming

⚠️There's a newer version (4.3.0) of this package.Take me there.

redist: Simulation Methods for Legislative Redistricting

This R package enables researchers to sample redistricting plans from a pre-specified target distribution using Sequential Monte Carlo and Markov Chain Monte Carlo algorithms. The package supports various constraints in the redistricting process, such as geographic compactness and population parity requirements. Tools for analysis, including computation of various summary statistics and plotting functionality, are also included.

Authors:

Contributors:

Papers:

Installation Instructions

redist is available on CRAN and can be installed using:

install.packages("redist")

You can also install the most recent development version of redist (which is usually quite stable) using the `remotes`` package.

if (!require(remotes)) install.packages("remotes")
remotes::install_github("alarm-redist/redist@dev", dependencies=TRUE)

Getting started

A basic analysis has two steps. First, you define a redistricting plan using redist_map. Then you simulate plans using one of the algorithm functions: redist_smc, redist_flip, and redist_mergesplit.

library(redist)
library(dplyr)

data(iowa)

# set a 0.1% population constraint
iowa_map = redist_map(iowa, existing_plan=cd_2010, pop_tol=0.001, total_pop = pop)
# simulate 500 plans using the SMC algorithm
iowa_plans = redist_smc(iowa_map, nsims=500)
#> SEQUENTIAL MONTE CARLO
#> Sampling 500 99-unit maps with 4 districts and population between 760,827 and 762,350.

After generating plans, you can use redist’s plotting functions to study the geographic and partisan characteristics of the simulated ensemble.

library(ggplot2)
library(patchwork) # for plotting

redist.plot.plans(iowa_plans, draws=c("cd_2010", "1", "2", "3"), shp=iowa_map)


iowa_plans = iowa_plans %>%
    mutate(Compactness = distr_compactness(iowa_map),
           `Population deviation` = plan_parity(iowa_map),
           `Democratic vote` = group_frac(iowa_map, dem_08, tot_08))
#> An earlier bug incorrectly in redist doubled the number of removed edges.
#> Current counts are correct.
#> This message is displayed once per session.

hist(iowa_plans, `Population deviation`) + hist(iowa_plans, Compactness) +
    plot_layout(guides="collect") +
    plot_annotation(title="Simulated plan characteristics")

redist.plot.scatter(iowa_plans, `Population deviation`, Compactness) +
    labs(title="Population deviation and compactness by plan")


plot(iowa_plans, `Democratic vote`, size=0.5, color_thresh=0.5) +
    scale_color_manual(values=c("tomato2", "dodgerblue")) +
    labs(title="Democratic vote share by district")

A more detailed introduction to redistricting methods and the package can be found in the Get Started page. The package vignettes contain more detailed information and guides to specific workflows.

Copy Link

Version

Install

install.packages('redist')

Monthly Downloads

281

Version

4.0.1

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Christopher T. Kenny

Last Published

June 16th, 2022

Functions in redist (4.0.1)

EPSG

EPSG Table
avg_by_prec

Average a variable by precinct
compare_plans

Make a comparison between two sets of plans
fl250

Florida 250 Precinct Shape File
fl25_adj

Florida 25 Precinct File
fl25

Florida 25 Precinct Shape File
constraints

Sampling constraints
classify_plans

Hierarchically classify a set of redistricting plans
add_reference

Add a reference plan to a set of plans
fl25_enum

All Partitions of 25 Precincts into 3 Congressional Districts (No Population Constraint)
number_by

Renumber districts to match a quantity of interest
get_mh_acceptance_rate

Extract the Metropolis Hastings Acceptance Rate
get_existing

Extract the existing district assignment from a redist_map object
get_plans_matrix

Extract the matrix of district assignments from a redistricting simulation
prec_assignment

Extract the district assignments for a precinct across all simulated plans
merge_by

Merge map units
min_move_parity

Calculates Sparse Population Moves to Minimize Population Deviation
prec_cooccurrence

Compute a matrix of precinct co-occurrences
get_plans_weights

Extract the sampling weights from a redistricting simulation.
is_county_split

Identify which counties are split by a plan
redist.calc.frontier.size

Calculate Frontier Size
is_contiguous

Check that a redist_map object is contiguous
print.redist_plans

Print method for redist_plans
print.redist_map

Generic to print redist_map
fl70

Florida 70 Precinct Shape File
iowa

Iowa County File
pullback

Pull back plans to unmerged units
get_target

Extract the target district population from a redist_map object
get_pop_tol

Get and set the population tolerance from a redist_map object
redist.combine.mpi

Combine successive runs of redist.mcmc.mpi
plot.redist_classified

Plot a plan classification
plot.redist_constr

Visualize constraints
distr_compactness

Calculate compactness measures for a set of plans
persily

Local Plan Optimization
redist-package

Simulation Methods for Legislative Redistricting
redist.district.splits

Counts the Number of Counties within a District
redist.county.id

Create County IDs
redist.county.relabel

Relabel Discontinuous Counties
redist.coarsen.adjacency

Coarsen Adjacency List
redist.dist.pop.overlap

Compare the Population Overlap Across Plans at the District Level
freeze

Freeze Parts of a Map
plan_distances

Compute Distance between Partitions
rbind.redist_plans

Combine multiple sets of redistricting plans
group_frac

Calculate Group Proportion by District
pick_a_plan

Pick One Plan from Many Plans
get_adj

Get and set the adjacency graph from a redist_map object
redist.flip.anneal

(Deprecated) Flip MCMC Redistricting Simulator using Simulated Annealing
redist.enumpart

Enumerate All Parititions
redist.flip

(Deprecated) Flip MCMC Redistricting Simulator
redist.plot.cores

Plot Cores
redist.plot.adj

Creates a Graph Overlay
partisan_metrics

Calculate gerrymandering metrics for a set of plans
redist.diagplot

Diagnostic plotting functionality for MCMC redistricting.
redist.crsg

Redistricting via Compact Random Seed and Grow Algorithm
redist.adjacency

Adjacency List functionality for redist
redist.ipw

Inverse probability reweighting for MCMC Redistricting
redist.mcmc.mpi

MCMC Redistricting Simulator using MPI
print.redist_classified

Print redist_classified objects
plans_diversity

Calculate the diversity of a set of plans
redist.multisplits

Counts the Number of Counties Split Between 3 or More Districts
redist.plot.trace

Make a traceplot for a summary statistic
redist.parity

Calculates Maximum Deviation from Population Parity
redist.plot.penalty

(Deprecated) Visualize Group Power Penalty
redist.read.enumpart

Read Results from enumpart
muni_splits

Counts the Number of Municipalities Split Between Districts
redist.plot.map

Plot a Map
redist.random.subgraph

Return a random subgraph of a shape
print.redist_constr

Generic to print redist_constr
make_cores

Identify Cores of a District (Heuristic)
redist.plot.varinfo

Static Variation of Information Plot
redist.wted.adj

Create Weighted Adjacency Data
redist.init.enumpart

Initialize enumpart
redist_plans

A set of redistricting plans
redist_shortburst

Redistricting Optimization through Short Bursts
redist_mergesplit_parallel

Parallel Merge-Split/Recombination MCMC Redistricting Sampler
redist_quantile_trunc

Helper function to truncate importance weights
redist.plot.interactive

Display an interactive map
redist.plot.plans

Plot a district assignment
redist.rsg

Redistricting via Random Seed and Grow Algorithm
redist.plot.scatter

Scatter plot of plan summary statistics
redist.plot.majmin

Majority Minority Plots
redist.run.enumpart

Runs the enumpart algorithm
redist.prec.pop.overlap

Compare the Population Overlap Across Plans at the Precinct Level
redist.plot.wted.adj

Plot Weighted Border Adjacency
get_sampling_info

Extract the sampling information from a redistricting simulation
last_plan

Extract the last plan from a set of plans
reexports

Objects exported from other packages
redist_constr

Set up constraints for sampling
redist.sink.plan

Sink Plans to 1:ndists
summary.redist_plans

Diagnostic information on sampled plans
segregation_index

Segregation index calculation for MCMC redistricting.
scorer-arith

Scoring function arithmetic
tally_var

Tally a variable by district
plot.redist_map

Plot a redist_map
redist.combine

(Deprecated) Combine successive runs of redist.flip
plot.redist_plans

Summary plots for \link{redist_plans}
match_numbers

Renumber districts to match an existing plan
redist.combine.anneal

(Deprecated) redist.combine.anneal
redist.constraint.helper

Create Constraints for SMC
competitiveness

Compute Competitiveness
redist.find.target

Find Majority Minority Remainder
redist.reduce.adjacency

Reduce Adjacency List
redist_map

Create a redist_map object.
redist_smc

SMC Redistricting Sampler
redist_smc_ci

Confidence Intervals for SMC Estimates
redist_mergesplit

Merge-Split/Recombination MCMC Redistricting Sampler
redist_flip_anneal

Flip MCMC Redistricting Simulator using Simulated Annealing
redist.reorder

Reorders district numbers
redist_flip

'Flip' Markov Chain Monte Carlo Redistricting Simulation
redist.prep.enumpart

Prepares a run of the enumpart algorithm by ordering edges
redist.plot.hist

Plot a histogram of a summary statistic
redist.findparams

Run parameter testing for redist.flip
redist.plot.distr_qtys

Plot quantities by district
county_splits

Count County Splits
redist.prep.polsbypopper

Prep Polsby Popper Perimeter Dataframe
redist.smc_is_ci

(Deprecated) Confidence Intervals for Importance Sampling Estimates
redist.subset

Subset a shp
scorer_group_pct

Scoring functions for redist_shortburst
redist.uncoarsen

Uncoarsen a District Matrix
subset_sampled

Subset to sampled or reference draws