alarm-redist / redist

Simulation methods for legislative redistricting.
https://alarm-redist.github.io/redist/
GNU General Public License v2.0
67 stars 23 forks source link

manually editing adj-matrix doesn't work #144

Closed martial00120 closed 2 years ago

martial00120 commented 2 years ago

I want to split the German county of Unterallgaeu into two districts. Unterallgaeu has an exclave called Buxheim. The nearest other municipial association is Boos. I am able to manually assign an adjacency link and I am able to plot it. However it is not possible to setup the map with the command redist_map. exclave_adj_unterallgaeu.zip

CoryMcCartan commented 2 years ago

Hi! Could you provide a bit more detail on the problem you're running into, and maybe copy into the website here the relevant code snippet?

If your adjacency graph is stored in e.g. an object called adj_graph, then you should be able to use it like this:

map = redist_map(... , adj=adj_graph)
martial00120 commented 2 years ago

The Code I use is this:

library(redist)
library(sf)
county_ua <-st_read('unterallgaeu.shp')
adj_ua <- redist.adjacency(county_ua)
redist.plot.adj(county_ua, adj=adj_ua)
adj_ua[[2]] <- c(12L)
redist.plot.adj(county_ua, adj=adj_ua)
lkr_map <- redist_map(county_ua, total_pop=POP,pop_tol=0.25,ndists=2, adj=adj_ua)

The shapefile I have uploaded already in the zip-file. I have an exclave community (called Buxheim) for which I edited the adj-table. I am able to plot the adj-plot. However I am not able to setup the redistricting problem with redist_map.

I get the following error:

> lkr_map <- redist_map(county_ua, total_pop=POP,pop_tol=0.25,ndists=2, adj=adj_ua)
Error in `redist_map()`:
! Adjacency graph not contiguous.
→ Try manually editing the output of `redist.adjacency()`.
ℹ Disconnected precincts: c(2)
Run `rlang::last_error()` to see where the error occurred.
martial00120 commented 2 years ago

unterallgaeu

I attach also the plot which shows that I could successfully edit the adj-table to link the exclace with its closest neighbor.

CoryMcCartan commented 2 years ago

Ah— So our adjacency graphs are undirected, which means you need to add the edge from 2 -> 12 as well as back from 12 -> 2. And it's also zero-indexed. So I think your code would look something like this:

adj_ua[[2]] <- 12
adj_ua[[13]] <- 1

We recommend using geomander::add_edge() for this kind of stuff (https://www.christophertkenny.com/geomander/reference/add_edge.html), since it takes care of all these low-level details for you.

@christopherkenny should we add a check/warning to redist.plot.adj if the input adjacency has any 1-sided edges?

martial00120 commented 2 years ago

I see! Now it works.

martial00120 commented 2 years ago

Thank you for your help and for the work you do.