-
Notifications
You must be signed in to change notification settings - Fork 3
/
gerry.R
437 lines (369 loc) · 11.5 KB
/
gerry.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
## POLITICAL GERRYMANDERING IN OHIO
## Authors: Desh Raj, Tara Abrishami
# Install function for packages
packages<-function(x){
x<-as.character(match.call()[[2]])
if (!require(x,character.only=TRUE)){
install.packages(pkgs=x,repos="http://cran.r-project.org")
require(x,character.only=TRUE)
}
}
packages(rgdal)
packages(maptools)
packages(ggplot2)
packages(broom)
packages(rgeos)
packages(igraph)
packages(spatialEco)
packages(rlist)
##--- WEIGHTS (Hyperparameters)------------
##-------- SCORE FUNCTIONS ----------------
## Score functions for a redistricting
## This function takes as input a possible
## redistricting, and assigns a score to it
## which is the weighted sum of several
## scores.
## Two redistricting formats are available:
## D => list of vectors, where each vector
## represents a district, and each element in a
## vector represents a precinct
## E => dictionary of precinct-to-district
## allocation
# Total score: section 3.1
score.total <- function(state, D, iso.score, w=c(1,1,1)){
return (w[1]*score.pop(D) + w[2]*iso.score + w[3]*score.county(state,D))
}
#function to calculate precinct populations
#voter turnout in Ohio: 64.2% (according to http://www.electproject.org/2016g)
#population of Ohio: 11.66 million (2017)
# Population score: section 3.1.1
parse.num <- function(x) {
if (is.na(x)) {
return(0)
} else {
return(x)
}
}
score.pop <- function(D){
ohio.population <- 11660000
district.pop <- rep(0, length(D))
for (i in 1:length(D)) {
district <- D[[i]]
for (j in 1:length(district)) {
district.pop[i] = district.pop[i] + parse.num(ohio$PRES_DEM16[j]) + parse.num(ohio$PRES_REP16[j]) + parse.num(ohio$PRES_GRN16[j])
}
}
pop.ideal <- ohio.population/length(D)
pop.score <-0
for (i in 1:length(D)) {
pop.score = pop.score + ((district.pop[i]/0.642)/pop.ideal - 1)^2
}
return(sqrt(pop.score))
}
# Isoperimetric score: section 3.1.2
score.isoperimetric <- function(district){
district.map <- unionSpatialPolygons(ohio[district,], rep(1,length(district)))
perimeter <- polyPerimeter(district.map)
return(perimeter^2/district.map@polygons[[1]]@Polygons[[1]]@area)
}
# County score: section 3.1.3
score.county <- function(state, D, w = c(1,1)){
all_counties = vector()
for (i in 1:length(D)) {
district = D[[i]]
district.counties = rep(NA, length(district))
for (j in 1:length(district)) {
district.counties[j] = state$CNTY_GE[district[j]]
}
district.counties = unique(district.counties)
all_counties = c(all_counties, district.counties)
}
frequencies = as.vector(table(all_counties))
split.two = sum(frequencies > 2)
split.three = sum(frequencies > 3)
return (w[1]*split.two + w[2]*split.three)
}
# Minority score: section 3.1.4
score.minority <- function(E){
#is not implemented because we do not have the data
}
equals <- function(x, y) {
if (sum(x == y) == 2) {
return(TRUE)
} else {
return(FALSE)
}
}
#----------- MH SAMPLING ------------------------
## Sample new redistrictings using MH algorithm
## Input: A (adjacency matrix), D (redistricting
## by district), E (redistricting by precinct)
## Output: generated redistricting samples
## section 3.3
sampleMH <- function(A, D, E, beta=0.4, T=1000, weights = c(200,0.005,1)){
isoperimetric.scores <- rep(0, length(D))
for (i in 1:length(D)) {
isoperimetric.scores[i] = score.isoperimetric(D[[i]])
}
conflicted.edges <- findConflictingEdges(E, A)
accept <- 0
samples <- matrix(, nrow = T, ncol = length(E))
# step 1
dist <- rep(NA, length(D))
samples[1,] <- E
for (i in 2:T){
# step 2
edge <- pickConflictingEdge(conflicted.edges)
while (sum(edge == c(0, 0)) == 2) {
edge <- pickConflictingEdge(conflicted.edges)
}
u <- edge[1]; v <- edge[2]
E.new <- E
x <- u
if (runif(1,0,1) < 0.5){
E.new[u] <- E[v]
} else {
E.new[v] <- E[u]
x <- v
}
D.new <- getDistrictsFromPrecincts(E.new)
iso.score.new <- isoperimetric.scores
conflicted.edges.new <- conflicted.edges
old_dist <- E[x]
new_dist <- E.new[x]
iso.score.new[old_dist] <- score.isoperimetric(D.new[[old_dist]])
iso.score.new[new_dist] <- score.isoperimetric(D.new[[new_dist]])
edges <- which(A[x,]==1, arr.ind = TRUE)
for (e in 1:length(edges)) {
if (E[e] != E[x]) {
for (j in 1:length(conflicted.edges.new)) {
if (sum(conflicted.edges.new[[j]] == c(e, x)) == 2 || sum(conflicted.edges.new[[j]] == c(x, e)) == 2) {
conflicted.edges.new = removeEdge(conflicted.edges.new, c(e, x), c(x, e))
}
}
}
if (E.new[e] != E.new[x]) {
list.append(conflicted.edges.new, c(e, x))
list.append(conflicted.edges.new, c(x, e))
}
}
# step 3
new_total_score = score.total(ohio, D.new, sum(iso.score.new), weights)
old_total_score = score.total(ohio, D, sum(isoperimetric.scores), weights)
accept.prob <- (length(conflicted.edges)/length(conflicted.edges.new)) *
exp(-beta * (score.total(ohio, D.new, sum(iso.score.new)) -
score.total(ohio, D, sum(isoperimetric.scores))))
accept.prob = min(accept.prob,1)
print (accept.prob)
unif <- runif(1,0,1)
print (unif)
if (accept.prob > unif){
D <- D.new
E <- E.new
accept <- accept + 1
samples[i,] <- E.new
conflicted.edges <- conflicted.edges.new #update conflicted edges
isoperimetric.scores <- iso.score.new #update isoperimetric scores
print("accepted")
} else {
samples[i,] <- E
print("rejected")
}
}
return (list(samples, accept))
}
#-------- HELPER FUNCTIONS FOR SAMPLING -----------
removeEdge <- function(my.list, my.tuple1, my.tuple2) {
new.list <- list()
j <- 1
for (i in 1:length(my.list)) {
if (sum(my.list[[i]] == my.tuple1) != 2 && sum(my.list[[i]] == my.tuple2) !=2) {
new.list[[j]] <- my.list[[i]]
j = j + 1
}
}
return(new.list)
}
## Pick any conflicting edge randomly in graph
## Conflicting edge means the vertices belong to
## different districts, i.e., the edge
## crosses a district boundary.
pickConflictingEdge <- function(edges) {
n <- length(edges)
ind <- ceiling(runif(1, 0, n))
return (edges[[ind]])
}
## Find all conflicting edges in graph
## Inputs: E (precinct-wise allocation dict),
## A (adjacency matrix)
## Output: list of conflicting edges
findConflictingEdges <- function(E, A){
edges <- which(A==1, arr.ind = TRUE)
conflict <- list()
j <- 1
for (i in 1:dim(edges)[1]){
if(!is.na(E[edges[i,1]]) && !is.na(E[edges[i,2]]) &&
(E[edges[i,1]] != E[edges[i,2]])){
conflict[[j]] <- edges[i,]
j <- j+1
}
}
return (conflict)
}
## Create random districts from given precincts
randomDist <- function(precincts, num_dist){
precincts.shuffled <- sample(precincts, size=length(precincts))
rDist <- split(precincts.shuffled, sort(precincts.shuffled%%num_dist))
return (rDist)
}
#------- DATA PROCESSING FUNCTIONS -------------
## Get full adjacency matrix from dense lists
getAdjMatrix <- function(adj){
n <- length(adj)
A <- matrix(0, nrow = n, ncol = n)
for (i in 1:n){
for (j in 1:length(adj[[i]])){
A[i,adj[[i]][j]] = 1
}
}
return (A)
}
## Find all connected components in the graph
## Note: This function is not being used in the
## code at present, but available in case of
## future requirement.
findConnectedComponents <- function(A){
n <- dim(A)[1]
C <- matrix(0, nrow = n, ncol = n)
g <- graph.adjacency(A)
groups <- groups(components(g))
for (i in 1:length(groups)){
group <- groups[[i]]
for (j in 1:length(group)){
for (k in 1:length(group)){
C[group[j],group[k]] = 1
}
}
}
return (C)
}
## Function to preprocess data fields
preprocess <- function(state){
data <- cbind(c(as.numeric(state@data[["id"]])+1),
c(state@data[["PREC_EL"]]),
c(state@data[["MET_ARE"]]),
c(state@data[["PRES_DEM16"]]),
c(state@data[["PRES_IN"]]),
c(state@data[["PRES_GRN16"]]),
c(state@data[["PRES_REP16"]]))
# remove precincts with <NA> districts
data.df <- data.frame(data)
data.new <- data.df[complete.cases(data.df),]
return(data.new)
}
## Functions to get redistricting
getRedistrictingByDistrict <- function(data){
districts <- unique(data[,2])
n <- length(districts)
D <- list()
for (i in 1:length(districts)){
D[[i]] <- subset(data[,1], data[,2]==districts[i])
}
return (D)
}
getRedistrictingByPrecinct <- function(data, n){
p <- rep(NA, n)
for (i in 1:n){
p[i] <- data[toString(i),2]
}
return (p)
}
## Function to get District wise list, given a
## dictionary of precinct allocations
getDistrictsFromPrecincts <- function(E){
n <- ifelse( !all(is.na(E)), max(E, na.rm=T), NA)
D <- vector("list", n)
for (i in 1:length(E)){
if(!is.na(E[i])){
D[[E[i]]] <- c(D[[E[i]]], i)
}
}
return (D)
}
getInitialDistrict <-function(state, county_file) {
## code to get initial districting
district_by_county <- readLines(county_file)
county_membership <- state$CNTY_NA
precinct_to_district <- rep(0, length(county_membership))
for (i in 1:length(county_membership)) {
for (j in 1:16) {
if (!is.na(county_membership[i])) {
if (grepl(county_membership[i], district_by_county[j])) {
precinct_to_district[i] = j
}
} else {
precinct_to_district[i] = NA
}
}
}
return(precinct_to_district)
}
assignNAprecincts <- function(E, A){
while(anyNA(E)){
print(sum(is.na(E)))
NAs = which(is.na(E))
for (i in 1:length(NAs)) {
precinct = NAs[i]
adjacent_precincts = which(A[precinct,] == 1)
adj.precinct = which(!is.na(E[adjacent_precincts]))[1]
if (!is.na(adj.precinct)) {
E[precinct] = E[adj.precinct]
}
}
}
return(E)
}
## Functions for data analysis
#plots the districts in sixteen unique colors
plotDistrict <- function(state, E) {
map = unionSpatialPolygons(state, E)
plot(map, col = c("turquoise", "red", "orange", "yellow", "blue", "coral2", "cornflowerblue", "darkmagenta","darkseagreen2", "deeppink", "forestgreen", "chocolate4", "burlywood4", "azure1", "cornsilk3", "darkorchid1"))
}
#returns a tuple (dems, reps) with the number of Democrat representatives and the number
#of Republican representatives
getElectionResults <- function(state, D) {
reps <- 0
dems <- 0
for (i in 1:length(D)) {
dem_votes <- 0
rep_votes <- 0
for (j in 1:length(D[[i]])) {
dem_votes = dem_votes + parse.num(state$PRES_DEM16[D[[i]][j]])
rep_votes = rep_votes + parse.num(state$PRES_REP16[D[[i]][j]])
}
if (dem_votes > rep_votes) {
dems = dems + 1
} else {
reps = reps + 1
}
}
return(c(dems, reps))
}
##-----------DRIVER CODE--------------------------
set.seed(1)
## read shapefile
ohio <- readOGR("ohio/precincts_results.shp")
## create adjacency matrix
adj_matrix <- gTouches(ohio, byid=TRUE, returnDense=FALSE)
## find connected components
A <- getAdjMatrix(adj_matrix)
## random redistricting
#E <- getRedistrictingByPrecinct(data, length(ohio))
#D <- getRedistrictingByDistrict(data)
## initial redistricting from county data (with correct number of districts)
E <- getInitialDistrict(ohio, "counties_to_districts.txt")
E <- assignNAprecincts(E, A)
D <- getDistrictsFromPrecincts(E)
## plot the initial district mapping because it's beautiful
# plotDistrict(ohio, E)
sample <- sampleMH(A, D, E)