### by_plan examples Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/by_plan.Rd Two examples showing by_plan applied to a vector of letters and to a vector where each letter is repeated twice. The first returns the input unchanged, the second returns the first of each repeated pair. ```r by_plan(letters) ``` ```r by_plan(rep(letters, each = 2)) ``` -------------------------------- ### Install redistmetrics development version from GitHub Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.Rmd Installs the development version of redistmetrics from GitHub, first ensuring the remotes package is installed. ```r if (!requireNamespace('remotes')) install.packages('remotes') remotes::install_github('alarm-redist/redistmetrics') ``` -------------------------------- ### comp_fh usage examples for single and multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_fh.Rd The examples load the `nh` and `nh_m` datasets and show how to calculate Fryer Holden Compactness for a single plan using `comp_fh(plans = nh$r_2020, shp = nh, total_pop = pop)` and for multiple plans with `comp_fh(plans = nh_m[, 3:5], shp = nh, pop)`. The second example omits the `total_pop` argument name, relying on positional matching. This snippet assumes the `nh` data and `pop` object are available. ```r data(nh) data(nh_m) # For a single plan: comp_fh(plans = nh$r_2020, shp = nh, total_pop = pop) # Or many plans: comp_fh(plans = nh_m[, 3:5], shp = nh, pop) ``` -------------------------------- ### list_fn('part_') example Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/list_fn.Rd Returns the names of all functions in the package whose names start with the prefix 'part_'. The prefix corresponds to the type of measure. ```r list_fn('part_') ``` -------------------------------- ### Load example data and compute Schwartzberg compactness for a single plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_schwartz.Rd Loads the example datasets nh and nh_m, then computes the Schwartzberg compactness for a single plan (nh$r_2020) using the shapefile nh. ```R data(nh) data(nh_m) # For a single plan: comp_schwartz(plans = nh$r_2020, shp = nh) ``` -------------------------------- ### Load New Hampshire example data Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.md Loads the example New Hampshire dataset. ```r data(nh) ``` -------------------------------- ### Example: splits_district_fuzzy() with single and multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_district_fuzzy.Rd Loads example data and creates a non-nesting county shapefile, then calls splits_district_fuzzy() for a single plan and for multiple plans. The thresh and epsg arguments are optional and default to 0.01 and 3857 respectively. ```R data(nh) data(nh_m) # toy example, # suppose we care about the splits of the counties and they don't nest nh_cty <- nh %>% dplyr::group_by(county) %>% dplyr::summarize() # For a single plan: splits_district_fuzzy(plans = nh$r_2020, shp = nh, nbr = nh_cty) # Or many plans: splits_district_fuzzy(plans = nh_m[, 3:5], shp = nh, nbr = nh_cty) ``` -------------------------------- ### Load redist_plans example data Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.Rmd Loads the nh_plans dataset, which is a redist_plans object for New Hampshire. ```r data(nh_plans) ``` -------------------------------- ### Install redistmetrics from CRAN Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.Rmd Installs the stable version of the redistmetrics package from CRAN. ```r install.packages('redistmetrics') ``` -------------------------------- ### Load matrix of plans example data Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.Rmd Loads the nh_m dataset, which is a matrix of plans where each column represents a plan. ```r data(nh_m) ``` -------------------------------- ### Example: prep_perims(nh) Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/prep_perims.Rd Prepares perimeter data for the built-in nh dataset. The function returns a tibble of perimeters and lengths. ```r data(nh) prep_perims(nh) ``` -------------------------------- ### comp_edges_rem example: single plan and multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_edges_rem.Rd These examples show how to compute edges removed compactness for a single plan using redistricting data from the nh dataset, and for multiple plans using a slice of the nh_m plans matrix. Both require the adjacency list nh$adj. ```r data(nh) data(nh_m) # For a single plan: comp_edges_rem(plans = nh$r_2020, shp = nh, nh$adj) # Or many plans: comp_edges_rem(plans = nh_m[, 3:5], shp = nh, nh$adj) ``` -------------------------------- ### Calculate X Symmetry Compactness with comp_x_sym Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_x_sym.Rd Loads the example data and demonstrates computing X symmetry compactness for a single plan and for multiple plans. The multi-plan call is wrapped in \donttest{} and is slower. ```r data(nh) data(nh_m) ``` ```r # For a single plan: comp_x_sym(plans = nh$r_2020, shp = nh) ``` ```r # Or many plans: \donttest{ # slower, beware! comp_x_sym(plans = nh_m[, 3:5], shp = nh) } ``` -------------------------------- ### Tally population and HVAP with tally() Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/tally.Rd Loads example data and demonstrates tally() for total population and for calculating the Hispanic voting-age population (HVAP) percentage. ```r data(nh) data(nh_m) tally(nh_m, nh, pop) # total population tally(nh_m, nh, vap_hisp) / tally(nh_m, nh, vap) # HVAP ``` -------------------------------- ### Load redistmetrics and New Hampshire data Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.Rmd Loads the redistmetrics package and the New Hampshire example dataset. ```r library(redistmetrics) ``` ```r data(nh) ``` -------------------------------- ### Load redist_map example data Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.Rmd Loads the nh_map dataset, which is a redist_map object for New Hampshire. ```r data(nh_map) ``` -------------------------------- ### Compute Polsby-Popper score for a single plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_polsby.Rd Loads example data and computes the Polsby-Popper compactness for a single redistricting plan using comp_polsby(). Requires the redistmetrics package and example data. ```r data(nh) data(nh_m) # For a single plan: comp_polsby(plans = nh$r_2020, shp = nh) # Or many plans: comp_polsby(plans = nh_m[, 3:5], shp = nh) ``` -------------------------------- ### Calculate responsiveness with part_resp() Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_resp.Rd Loads example data and demonstrates calculating responsiveness for a single plan using nh$r_2020 and for multiple plans using columns 3 through 5 of nh_m. The function requires a redist_plans object or plans_matrix, a shp object with vote columns, and unquoted column names for rvote and dvote. ```R data(nh) data(nh_m) # For a single plan: part_resp(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_resp(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Count splits with splits_count() Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_count.Rd Loads the example datasets and calls splits_count() for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). The admin argument is passed unquoted as county. ```r data(nh) data(nh_m) # For a single plan: splits_count(plans = nh$r_2020, shp = nh, admin = county) # Or many plans: splits_count(plans = nh_m[, 3:5], shp = nh, admin = county) ``` -------------------------------- ### part_dil_asym Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_dil_asym.Rd Documentation for the part_dil_asym function, including usage, arguments, return value, and examples. ```APIDOC ## part_dil_asym ### Description Calculates the dilution asymmetry metric for a set of redistricting plans, as described in Gordon and Yntiso (2024). ### Usage ```r part_dil_asym(plans, shp, dvote, rvote) ``` ### Arguments - **plans** (`redist_plans` object or plans_matrix) - Required - A `redist_plans` object or plans_matrix where each row indicates a district assignment and each column is a plan. - **shp** (`redist_map` object, tibble, or data frame) - Required - A `redist_map` object, tibble, or data frame containing other columns. - **dvote** (unquoted column name) - Required - Unquoted name of column in `shp` with total population. - **rvote** (unquoted column name) - Required - Unquoted name of column in `shp` with group population. ### Value A numeric vector. Can be shaped into a district-by-plan matrix. ### Examples ```r data(nh) data(nh_m) # For a single plan: part_dil_asym(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_dil_asym(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` ### References Sanford C. Gordon and Sidak Yntiso. 2024. Base Rate Neglect and the Diagnosis of Partisan Gerrymanders. Election Law Journal: Rules, Politics, and Policy. doi:10.1089/elj.2023.0005. ``` -------------------------------- ### Calculate length-width compactness for multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_lw.Rd Computes compactness for plans in columns 3 to 5 of the nh_m matrix. This example is wrapped in \donttest{} because it is slower and may take time to run. ```r \donttest{ # slower, beware! comp_lw(plans = nh_m[, 3:5], shp = nh) } ``` -------------------------------- ### Load data and compute Y symmetry for a single plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_y_sym.Rd Loads the example data and computes Y symmetry compactness for a single plan using the comp_y_sym function with the default EPSG 3857 projection. ```r data(nh_m) ``` ```r comp_y_sym(plans = nh$r_2020, shp = nh) ``` -------------------------------- ### Calculate Hamming distances with dist_ham Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/dist_ham.Rd Loads example data and calls dist_ham on a single plan (returns 0) and on multiple plans (columns 3 to 5 of nh_m). ```r data(nh) data(nh_m) # For a single plan (distance is trivial, 0): dist_ham(plans = nh$r_2020) # Or many plans: dist_ham(plans = nh_m[, 3:5]) ``` -------------------------------- ### Calculate Reock compactness with comp_reock Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_reock.Rd Loads example data and demonstrates computing Reock compactness for a single plan and for multiple plans. The first call uses a single plan from nh$r_2020, while the second uses columns 3 through 5 of nh_m. ```R data(nh) data(nh_m) # For a single plan: comp_reock(plans = nh$r_2020, shp = nh) # Or many plans: comp_reock(plans = nh_m[, 3:5], shp = nh) ``` -------------------------------- ### Get citation for redistmetrics Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/redistmetrics.Rmd Displays the citation information for the redistmetrics package. ```r citation('redistmetrics') ``` -------------------------------- ### Compute Box Reock for multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_box_reock.Rd Computes Box Reock compactness for multiple plans using the nh_m matrix. The example is wrapped in \donttest{} and warns that it is slower. It passes columns 3 through 5 of nh_m as the plans. ```R # Or many plans: \donttest{ # slower, beware! comp_box_reock(plans = nh_m[, 3:5], shp = nh) } ``` -------------------------------- ### Calculate Boyce Clark Ratio with comp_bc Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_bc.Rd Loads the example data and calls comp_bc for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). The multi-plan call is wrapped in \donttest{} because it is slower. ```r data(nh) data(nh_m) # For a single plan: comp_bc(plans = nh$r_2020, shp = nh) # Or many plans: \donttest{ # slower, beware! comp_bc(plans = nh_m[, 3:5], shp = nh) } ``` -------------------------------- ### Calculate Bounding Box Reock Compactness with comp_bbox_reock Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_bbox_reock.Rd Loads the example datasets and shows two calls to comp_bbox_reock: one for a single plan using nh$r_2020 and one for multiple plans using the first five columns of nh_m. The function returns a numeric vector that can be shaped into a district-by-plan matrix. ```r data(nh) data(nh_m) # For a single plan: comp_bbox_reock(plans = nh$r_2020, shp = nh) # Or many plans: comp_bbox_reock(plans = nh_m[, 1:5], shp = nh) ``` -------------------------------- ### Calculate dilution asymmetry with part_dil_asym Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_dil_asym.Rd Loads the example datasets and calls part_dil_asym for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). Requires the nh and nh_m datasets and columns nrv and ndv in shp. ```R data(nh) data(nh_m) # For a single plan: part_dil_asym(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_dil_asym(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Calculate Tau Gap for a single plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_tau_gap.Rd Loads example data and calculates the Tau Gap for a single plan using the part_tau_gap function. Requires the nh and nh_m datasets. ```r data(nh) data(nh_m) # For a single plan: part_tau_gap(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Compute Y symmetry for multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_y_sym.Rd Computes Y symmetry compactness for multiple plans (columns 3 to 5 of the nh_m object). This example is wrapped in \donttest{} and includes a comment warning that it is slower. ```r \donttest{ # slower, beware! comp_y_sym(plans = nh_m[, 3:5], shp = nh) } ``` -------------------------------- ### Compute Talismanic competitiveness with compet_talisman Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/compet_talisman.Rd Loads the example data and calls compet_talisman for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). Requires shp to contain the columns named by rvote and dvote. ```r data(nh) data(nh_m) # For a single plan: compet_talisman(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: compet_talisman(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Calculate mean-median score with part_mean_median Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_mean_median.Rd Loads example data and calls part_mean_median for a single plan and for multiple plans. The comment notes that the result is zero for the two-district case. ```r data(nh) data(nh_m) # zero for the two district case: # For a single plan: part_mean_median(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_mean_median(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Calculate Efficiency Gap with part_egap_ep Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_egap_ep.Rd Loads the example data and calls part_egap_ep for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). The function expects unquoted column names for rvote and dvote. ```r data(nh) data(nh_m) # For a single plan: part_egap_ep(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_egap_ep(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### part_dislocation Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_dislocation.Rd Documentation for the part_dislocation function, including usage, arguments, return value, and examples. ```APIDOC ## part_dislocation ### Description Calculates partisan dislocation, a precinct-level measure of representation and gerrymandering, for one or more redistricting plans. ### Usage ```r part_dislocation( plans, shp, dvote, rvote, total_pop = dvote + rvote, epsg = 3857, by_precinct = FALSE, signed = TRUE ) ``` ### Arguments - **plans** (redist_plans or matrix) - Required - A `redist_plans` object or plans_matrix where each row indicates a district assignment and each column is a plan. - **shp** (redist_map, tibble, or data.frame) - Required - A `redist_map` object, tibble, or data frame with an `sf` geometry column. - **dvote** (unquoted column name) - Required - Unquoted name of column in `shp` with total population. - **rvote** (unquoted column name) - Required - Unquoted name of column in `shp` with group population. - **total_pop** (unquoted column name) - Optional - Unquoted name of column in `shp` with total population. Default is `dvote + rvote`. - **epsg** (numeric) - Optional - Numeric EPSG code to use to project the shapefile, if needed. Default is 3857. - **by_precinct** (logical) - Optional - Defaults to `FALSE`, returning district-level values. If `TRUE`, returns precinct-level values. - **signed** (logical) - Optional - Defaults to `TRUE`. Should the output for district-level values be signed? Setting to `TRUE` returns precinct-level values. ### Value A numeric vector. Can be shaped into a district-by-plan matrix. ### Examples ```r # For a single plan: part_dislocation(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_dislocation(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` ### References DeFord, D. R., Eubank, N., & Rodden, J. (2022). Partisan dislocation: A precinct-level measure of representation and gerrymandering. Political Analysis, 30(3), 403-425. ``` -------------------------------- ### Compute splits_multi for single and multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_multi.Rd Loads example data and shows two calls to splits_multi: one for a single plan using nh$r_2020 and one for multiple plans using columns 3 through 5 of nh_m. The admin argument is the unquoted column name county. ```r data(nh) data(nh_m) # For a single plan: splits_multi(plans = nh$r_2020, shp = nh, admin = county) # Or many plans: splits_multi(plans = nh_m[, 3:5], shp = nh, admin = county) ``` -------------------------------- ### Calculate skew compactness for multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_skew.Rd Computes skew compactness for multiple plans (columns 3 to 5 of nh_m) using the shapefile nh. This example is wrapped in \donttest{} and includes a comment warning that it is slower. ```r # Or many plans: \donttest{ # slower, beware! comp_skew(plans = nh_m[, 3:5], shp = nh) } ``` -------------------------------- ### Compute administrative unit splits with splits_admin Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_admin.Rd Loads the example datasets and calls splits_admin() for a single plan using nh$r_2020 and for multiple plans using columns 3 through 5 of nh_m. The admin argument is passed the unquoted column name county. ```r data(nh) data(nh_m) # For a single plan: splits_admin(plans = nh$r_2020, shp = nh, admin = county) # Or many plans: splits_admin(plans = nh_m[, 3:5], shp = nh, admin = county) ``` -------------------------------- ### Count sub-administrative splits for many plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_sub_total.Rd Example using the nh and nh_m datasets. Calls splits_sub_total with multiple plans (nh_m[, 3:5]) and the county column as the sub_admin identifier. ```r splits_sub_total(plans = nh_m[, 3:5], shp = nh, sub_admin = county) ``` -------------------------------- ### Calculate fraction kept compactness for multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_frac_kept.Rd Computes the fraction kept compactness for several plans at once by passing a plans matrix subset (nh_m[, 3:5]) to the plans argument. The shp and adj arguments are the same as in the single-plan example. ```R # Or many plans: comp_frac_kept(plans = nh_m[, 3:5], shp = nh, nh$adj) ``` -------------------------------- ### Count incumbent pairings with inc_pairs() Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/inc_pairs.Rd Loads the nh and nh_m example datasets, creates a logical vector marking incumbents in rows 3 and 4, then calls inc_pairs() for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). ```r data(nh) data(nh_m) # Use incumbent data: fake_inc <- rep(FALSE, nrow(nh)) fake_inc[3:4] <- TRUE # For a single plan: inc_pairs(plans = nh$r_2020, shp = nh, inc = fake_inc) # Or many plans: inc_pairs(plans = nh_m[, 3:5], shp = nh, inc = fake_inc) ``` -------------------------------- ### Compute dissimilarity index for many plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/seg_dissim.Rd Uses the nh_m plans matrix and selects columns 3 through 5 to compute the dissimilarity index for multiple plans at once. Requires the same shp and population columns as the single-plan example. ```r data(nh) data(nh_m) # Or many plans: seg_dissim(plans = nh_m[, 3:5], shp = nh, group_pop = vap_hisp, total_pop = vap) ``` -------------------------------- ### Compute Length-Width compactness with comp_lw() Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/compactness.Rmd Computes the Length-Width compactness score for each district. Scores range from 0 (least compact) to 1 (most compact). The example uses the nh dataset with the 2020 redistricting plan. ```r comp_lw(plans = nh$r_2020, shp = nh) ``` -------------------------------- ### Plot New Hampshire Republican plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/compactness.Rmd Creates a ggplot2 map of New Hampshire districts colored by the `r_2020` plan. This is a visualization example, not a compactness computation. ```r library(ggplot2) nh %>% ggplot() + geom_sf(aes(fill = as.character(r_2020))) + labs(fill = 'none') ``` -------------------------------- ### Calculate length-width compactness for a single plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_lw.Rd Loads the New Hampshire example data and computes the compactness score for the plan in column 'r_2020' of the nh object. The shapefile is provided as nh. ```r data(nh) data(nh_m) # For a single plan: comp_lw(plans = nh$r_2020, shp = nh) ``` -------------------------------- ### Calculate simplified declination with part_decl_simple Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_decl_simple.Rd Loads the example datasets and calls part_decl_simple for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). The function expects unquoted column names for rvote and dvote. ```r data(nh) data(nh_m) # For a single plan: part_decl_simple(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_decl_simple(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Calculate lopsided wins with part_lop_wins Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_lop_wins.Rd Loads the example datasets and calls part_lop_wins for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). The function expects unquoted column names for rvote and dvote. ```r data(nh) data(nh_m) # For a single plan: part_lop_wins(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_lop_wins(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Calculate Efficiency Gap with part_egap Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_egap.Rd Loads the example datasets and calls part_egap for a single plan using nh$r_2020, then for multiple plans using columns 3 through 5 of nh_m. The rvote and dvote arguments are passed as unquoted column names nrv and ndv. ```r data(nh) data(nh_m) # For a single plan: part_egap(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_egap(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Count sub-administrative splits for a single plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_sub_total.Rd Example using the nh and nh_m datasets. Calls splits_sub_total with a single plan (nh$r_2020) and the county column as the sub_admin identifier. ```r data(nh) data(nh_m) # For a single plan: splits_sub_total(plans = nh$r_2020, shp = nh, sub_admin = county) ``` -------------------------------- ### Count splits for many plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_total.Rd Counts the total number of administrative splits for multiple plans by passing a matrix of plans to splits_total. The example uses columns 3 to 5 of the nh_m matrix. ```R data(nh) data(nh_m) # Or many plans: splits_total(plans = nh_m[, 3:5], shp = nh, admin = county) ``` -------------------------------- ### Compute Incumbent Pairings with inc_pairs Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/other.Rmd Computes the number of incumbent pairings for the 2020 redistricting plan in New Hampshire, using the fake incumbent data created in the previous example. The function takes the plan, shapefile, and incumbent vector as arguments. ```r inc_pairs(plans = nh$r_2020, shp = nh, inc = fake_inc) ``` -------------------------------- ### Compute Boyce-Clark compactness with comp_bc() Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/compactness.Rmd Computes the Boyce-Clark compactness score using 16 evenly spaced radials from the interior center. Scores range from 0 (most compact) to infinity (least compact). The example uses the nh dataset. ```r comp_bc(plans = nh$r_2020, shp = nh) ``` -------------------------------- ### Count splits per sub-administrative unit with splits_sub_count Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_sub_count.Rd Loads example data and demonstrates calling splits_sub_count for a single plan and for multiple plans. The function tallies unique sub-administrative unit-districts; an unsplit unit returns 1 and each additional sub-administrative unit-district adds 1. ```r data(nh) data(nh_m) # For a single plan: splits_sub_count(plans = nh$r_2020, shp = nh, sub_admin = county) # Or many plans: splits_sub_count(plans = nh_m[, 3:5], shp = nh, sub_admin = county) ``` -------------------------------- ### Calculate Smoothed Seat Count Deviation with part_sscd Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_sscd.Rd Loads the example data and calls part_sscd for a single plan (nh$r_2020) and for multiple plans (nh_m[, 3:5]). The function requires a redist_plans object or plans_matrix, a shp object with columns, and unquoted column names for rvote and dvote. ```r data(nh) data(nh_m) # For a single plan: part_sscd(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_sscd(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### View head of redist_plans object Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.Rmd Displays the first few rows of the redist_plans object to show the computed compactness values. ```r head(nh_plans) ``` -------------------------------- ### Compute responsiveness with part_resp Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/party.Rmd Computes responsiveness for the 2020 plan in the nh dataset using default settings. The v argument defaults to 0.5 and the bandwidth defaults to 0.1. ```r part_resp(plans = nh$r_2020, shp = nh, dvote = ndv, rvote = nrv) ``` -------------------------------- ### part_rmd for many plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_rmd.Rd Calculates ranked marginal deviation for multiple plans by passing a matrix of plans (columns 3 to 5) from `nh_m`. Requires the same datasets as the single-plan example. ```r # Or many plans: part_rmd(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Compute Manhattan distance with dist_man() Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/distances.Rmd Computes the Manhattan distance between the plans in nh_m, measuring how many 'blocks' you would need to move to get between plans. Most useful in MCMC contexts. ```r dist_man(plans = nh_m) ``` -------------------------------- ### View redist_plans with compactness column Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.md Displays the first rows of the redist_plans object after adding the polsby column, showing the tidy format with draw, district, total_pop, and polsby columns. ```r head(nh_plans) #> # A tibble: 6 × 4 #> draw district total_pop polsby #> #> 1 d_2020 1 688739 0.184 #> 2 d_2020 2 688790 0.180 #> 3 r_2020 1 688676 0.232 #> 4 r_2020 2 688853 0.158 #> 5 1 1 688961 0.235 #> 6 1 2 688568 0.349 ``` -------------------------------- ### list_fn(prefix) Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/list_fn.Rd Returns the functions in the package that match a given prefix. The package uses prefixes for each function that correspond to the type of measure. ```APIDOC ## list_fn(prefix) ### Description Returns the functions in the package that match a given prefix. The package uses prefixes for each function that correspond to the type of measure. ### Usage ```r list_fn(prefix) ``` ### Arguments - **prefix** (character) - Required - character prefix of functions to return ### Value character vector of functions ### Examples ```r list_fn('part_') ``` ``` -------------------------------- ### Compute declination with part_decl Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/party.Rmd Computes the declination for a set of plans using the part_decl function. By default, the result is normalized to a fraction of an angle. Set normalization = FALSE to get the variant in radians. ```R part_decl(plans = nh$r_2020, shp = nh, dvote = ndv, rvote = nrv) ``` -------------------------------- ### Calculate Partisan Dislocation for a Single Plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_dislocation.Rd Loads example data and calculates partisan dislocation for a single plan using the nh dataset. The function is called with the plan, shapefile, and vote columns. ```r data(nh) data(nh_m) # For a single plan: part_dislocation(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### Compute Euclidean distance with dist_euc() Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/distances.Rmd Computes the Euclidean distance between the plans in nh_m, measuring the square root of the summed distances you would need to move to get between plans. Most useful in MCMC contexts. ```r dist_euc(plans = nh_m) ``` -------------------------------- ### Calculate declination for many plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_decl.Rd Shows how to compute the declination for multiple plans using columns 3 to 5 of `nh_m`. ```r # Or many plans: part_decl(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### comp_lw(plans, shp, epsg = 3857, ncores = 1) Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_lw.Rd Computes the Length-Width compactness metric for each district in the provided plan(s). ```APIDOC ## comp_lw(plans, shp, epsg = 3857, ncores = 1) ### Description Computes the Length-Width compactness metric for each district in the provided plan(s). ### Usage comp_lw(plans, shp, epsg = 3857, ncores = 1) ### Parameters #### plans - **plans** (redist_plans or plans_matrix) - Required - A `redist_plans` object or plans_matrix where each row indicates a district assignment and each column is a plan. #### shp - **shp** (redist_map, tibble, or data frame) - Required - A `redist_map` object, tibble, or data frame with an `sf` geometry column. #### epsg - **epsg** (numeric) - Optional - Numeric EPSG code to use to project the shapefile, if needed. Default is 3857. #### ncores - **ncores** (integer) - Optional - Integer number of cores to use. Default is 1. ### Return Value A numeric vector. Can be shaped into a district-by-plan matrix. ### Examples ```r data(nh) data(nh_m) # For a single plan: comp_lw(plans = nh$r_2020, shp = nh) # Or many plans: \donttest{ # slower, beware! comp_lw(plans = nh_m[, 3:5], shp = nh) } ``` ### References Harris, Curtis C. 1964. "A scientific method of districting". Behavioral Science 3(9), 219–225. ``` -------------------------------- ### Calculate partisan bias for multiple plans Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_bias.Rd Computes partisan bias for several plans by passing a matrix of plans (`nh_m[, 3:5]`) to the `plans` argument. Uses the same `nh` dataset and vote columns as the single-plan example. ```r part_bias(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### dist_man with multiple plans (dist_man(plans = nh_m[, 3:5])) Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/dist_man.Rd Computes Manhattan distances for multiple plans. Uses the nh_m dataset and selects columns 3 through 5. ```r data(nh) data(nh_m) # Or many plans: dist_man(plans = nh_m[, 3:5]) ``` -------------------------------- ### Load redistmetrics Source: https://github.com/alarm-redist/redistmetrics/blob/main/README.md Loads the redistmetrics package. ```r library(redistmetrics) ``` -------------------------------- ### Compute plan-level Efficiency Gap with by_plan() Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/redistmetrics.Rmd Computes the Efficiency Gap and pipes the result through by_plan() to return a single plan-level metric instead of repeating it per district. ```r part_egap(plans = nh$r_2020, shp = nh, rvote = nh$pre_20_rep_tru, dvote = nh$pre_20_dem_bid) %>% by_plan() ``` -------------------------------- ### Compute sub-administrative unit splits with splits_sub_admin() Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/splits_sub_admin.Rd Loads the nh and nh_m datasets, then calls splits_sub_admin() for a single plan using nh$r_2020 and for multiple plans using columns 3 through 5 of nh_m. The sub_admin argument is passed the unquoted column name county. ```r data(nh) data(nh_m) # For a single plan: splits_sub_admin(plans = nh$r_2020, shp = nh, sub_admin = county) # Or many plans: splits_sub_admin(plans = nh_m[, 3:5], shp = nh, sub_admin = county) ``` -------------------------------- ### Compute Fryer-Holden compactness with comp_fh() Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/compactness.Rmd Computes the Fryer-Holden compactness score, which measures squared person-distance. Smaller scores are more compact; when normalized, a score of 1 is most compact. Requires total population (total_pop) argument. The example uses the nh dataset. ```r comp_fh(plans = nh$r_2020, shp = nh, total_pop = pop) ``` -------------------------------- ### Compute Reock compactness with comp_reock() Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/redistmetrics.Rmd Computes the Reock compactness score for each district in the plan. The output is two scores, one per district. ```r comp_reock(plans = nh$r_2020, shp = nh) ``` -------------------------------- ### Compute X-Symmetry compactness Source: https://github.com/alarm-redist/redistmetrics/blob/main/vignettes/compactness.Rmd Calculates X-Symmetry compactness for the New Hampshire Republican plan. Scores range from 0 (least compact) to 1 (most compact). ```r comp_x_sym(plans = nh$r_2020, shp = nh) ``` -------------------------------- ### comp_reock(plans, shp, epsg = 3857, ncores = 1) Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_reock.Rd Function to calculate Reock compactness for redistricting plans. ```APIDOC ## comp_reock(plans, shp, epsg = 3857, ncores = 1) ### Description Calculate Reock Compactness for one or more redistricting plans. ### Usage comp_reock(plans, shp, epsg = 3857, ncores = 1) ### Arguments - **plans** (redist_plans or plans_matrix) - Required - A `redist_plans` object or plans_matrix where each row indicates a district assignment and each column is a plan. - **shp** (redist_map, tibble, or data frame) - Required - A `redist_map` object, tibble, or data frame with an `sf` geometry column. - **epsg** (numeric) - Optional - Numeric EPSG code to use to project the shapefile, if needed. Default is 3857. - **ncores** (integer) - Optional - Integer number of cores to use. Default is 1. ### Value A numeric vector. Can be shaped into a district-by-plan matrix. ### Examples ```r data(nh) data(nh_m) # For a single plan: comp_reock(plans = nh$r_2020, shp = nh) # Or many plans: comp_reock(plans = nh_m[, 3:5], shp = nh) ``` ### References Reock, E. 1961. A Note: Measuring Compactness as a Requirement of Legislative Apportionment. Midwest Journal of Political Science, 5(1), 70-74. ``` -------------------------------- ### part_dseats(plans, shp, dvote, rvote) Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_dseats.Rd Computes the number of Democratic seats for each plan based on election vote totals. ```APIDOC ## part_dseats(plans, shp, dvote, rvote) ### Description Calculates the number of Democratic seats in a redistricting plan or set of plans, based on the provided Democratic and Republican vote totals. ### Usage part_dseats(plans, shp, dvote, rvote) ### Arguments - **plans** (redist_plans or plans_matrix) - Required - A `redist_plans` object or plans_matrix where each row indicates a district assignment and each column is a plan. - **shp** (redist_map, tibble, or data.frame) - Required - A `redist_map` object, tibble, or data frame containing other columns. - **dvote** (unquoted column name) - Required - Unquoted name of column in `shp` with Democratic votes. - **rvote** (unquoted column name) - Required - Unquoted name of column in `shp` with Republican votes. ### Value A numeric vector. Can be shaped into a district-by-plan matrix. ### Examples ```r # For a single plan: part_dseats(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) # Or many plans: part_dseats(plans = nh_m[, 3:5], shp = nh, rvote = nrv, dvote = ndv) ``` ``` -------------------------------- ### prep_perims Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/prep_perims.Rd Function to prepare a perimeter tibble for Polsby-Popper compactness calculations. ```APIDOC ## prep_perims ### Description Prepares a tibble of perimeters and lengths for use in Polsby-Popper compactness calculations. Replaces `redist.prep.polsbypopper`. ### Usage ```r prep_perims(shp, epsg = 3857, perim_path, ncores = 1) ``` ### Arguments - **shp** (redist_map, tibble, or data frame) - Required - A `redist_map` object, tibble, or data frame with an `sf` geometry column. - **epsg** (numeric) - Optional - Numeric EPSG code to use to project the shapefile, if needed. Default is 3857. - **perim_path** (character) - Required - A path to save an rds. - **ncores** (integer) - Optional - Integer number of cores to use. Default is 1. ### Value A tibble of perimeters and lengths. ### Examples ```r data(nh) prep_perims(nh) ``` ``` -------------------------------- ### Calculate partisan bias for a single plan Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/part_bias.Rd Computes partisan bias for one plan using the `nh` dataset. Requires `data(nh)` and `data(nh_m)` to load the example data. The `plans` argument is set to `nh$r_2020`, and vote columns are `nrv` and `ndv`. ```r data(nh) data(nh_m) # For a single plan: part_bias(plans = nh$r_2020, shp = nh, rvote = nrv, dvote = ndv) ``` -------------------------------- ### comp_log_st Source: https://github.com/alarm-redist/redistmetrics/blob/main/man/comp_log_st.Rd Documentation for the comp_log_st function, which calculates log spanning tree compactness for redistricting plans. ```APIDOC ## comp_log_st ### Description Calculate Log Spanning Tree Compactness. ### Usage ```r comp_log_st(plans, shp, counties = NULL, adj) ``` ### Arguments - **plans** (redist_plans object or plans_matrix) - Required - A `redist_plans` object or plans_matrix where each row indicates a district assignment and each column is a plan. - **shp** (redist_map object, tibble, or data frame) - Required - A `redist_map` object, tibble, or data frame with an `sf` geometry column. - **counties** (column name in shp) - Optional - Column name in shp containing counties. - **adj** (Zero-indexed adjacency list) - Required - Zero-indexed adjacency list. Not required if a `redist_map` is supplied for `shp`. ### Value A numeric vector. Can be shaped into a district-by-plan matrix. ### Examples ```r data(nh) data(nh_m) # For a single plan: comp_log_st(plans = nh$r_2020, shp = nh, counties = county, adj = nh$adj) # Or many plans: comp_log_st(plans = nh_m[, 3:5], shp = nh, counties = county, adj = nh$adj) ``` ### References Cory McCartan and Kosuke Imai. 2020. Sequential Monte Carlo for Sampling Balanced and Compact Redistricting Plans. ```