-
Notifications
You must be signed in to change notification settings - Fork 0
/
sarpyMC.R
154 lines (126 loc) · 5.47 KB
/
sarpyMC.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
library("graph")
library("Rgraphviz")
library("RBGL")
library("dplyr")
getBoundary <- function(subgraph, graph) {
bdy <- unique(unlist(graph::boundary(subgraph, graph)))
return(bdy)
## returns boundary of subgraph in graph as R char vector in
## future, add some error checking for argument types
}
getSarpyPrecincts <- function(precList) {
sarpyBoundary <- precList[!(precList %in% c("lanc", "doug"))]
return(sarpyBoundary)
## returns list (char vector) of precincts stricly in Sarpy Cty in
## future, add some error checking for argument types
}
makeNewCongDist <- function(sarpyPrecincts, vertexDelete, vertexAdd, county) {
newSarpyPrecincts <- c(sarpyPrecincts[-match(vertexDelete, sarpyPrecincts)],
vertexAdd, county)
newCongDist <- graph::subGraph(newSarpyPrecincts, gSarpy)
return(newCongDist)
}
vEccentricity <- function(graph, vertex) {
ve <- max(RBGL::dijkstra.sp(graph, start = vertex)$distances)
return(ve)
## returns integer, longest shortest-path from vertex to every
## other vertex in graph in future, add some error checking for
## argument types
}
gDiameter <- function(graph) {
d <- max(RBGL::johnson.all.pairs.sp(graph))
return(d)
## returns integer, greatest distance between any pair of vertices
## in graph in future, add some error checking for argument types
}
contiguous <- function(graph) {
isconnected <- length(RBGL::connectedComp(graph)) == 1
return(isconnected)
## returns logical (boolean), TRUE if single connected component
## in future, add some error checking for argument types
}
## The following are compactness measures for the originally
## constituted congressional districts My goal is to have trial
## districts to have no greater eccentricities or diameters.
BASEVECCENCD1 <- 7
BASEGDIAMCD1 <- 7
BASEVECCENCD2 <- 4
BASEGDIAMCD2 <- 6
sarpyAdj <- as.matrix(read.table("sarpy_adj.txt"))
# n x 2 edgelist matrix, not adjacency matrix!
gSarpy <- ftM2graphNEL(sarpyAdj, edgemode = "undirected")
cd1Prec <- c("p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8",
"p9", "p10", "p11", "p12", "p13", "p16", "p17",
"p18", "p20", "p21", "p22", "p24", "p25", "p26",
"lanc")
cd2Prec <- setdiff(nodes(gSarpy), cd1Prec)
congDist1 <- subGraph(cd1Prec, gSarpy)
congDist2 <- subGraph(cd2Prec, gSarpy)
Elec2018 <- read.csv("Elec2018.csv")
mcData <- data.frame(mcStep = 0, rePartionTrial = 0,
EccenCD1 = 7, DiamCD1 = 7, CompCD1 = 1,
EccenCD2 = 4, DiamCD2 = 6, CompCD2 = 1,
bdyCD1 = 9, BdyCD2 = 9)
## districtsSeen <- c(1)
## names(districtsSeen) <- c(str_c(sort(nodes(congDist1)), collapse = ""))
elecOutcomes <- data.frame(mcStep = 0, cd2DEM = 121770, cd2REP = 126715)
for (mcStep in 1:10) {
rePartitionTrial <- 1
bdyCongDist1 <-
getSarpyPrecincts(getBoundary(congDist1, gSarpy)) # in CongDist2
bdyCongDist2 <-
getSarpyPrecincts(getBoundary(congDist2, gSarpy)) # in CongDist1
repeat {
# rePartition Trial
# randomly select a boundary precinct for each Congressional District
precFromCongDist1 <- sample(bdyCongDist2, 1)
precFromCongDist2 <- sample(bdyCongDist1, 1)
newCongDist1 <-
makeNewCongDist(getSarpyPrecincts(nodes(congDist1)),
precFromCongDist1, precFromCongDist2,
"lanc")
newCongDist2 <-
makeNewCongDist(getSarpyPrecincts(nodes(congDist2)),
precFromCongDist2, precFromCongDist1,
"doug")
testvEccenCD1 <- vEccentricity(newCongDist1, "lanc")
testgDiamCD1 <- gDiameter(newCongDist1)
testCompsCD1 <- length(RBGL::connectedComp(newCongDist1))
testvEccenCD2 <- vEccentricity(newCongDist2, "doug")
testgDiamCD2 <- gDiameter(newCongDist2)
testCompsCD2 <- length(RBGL::connectedComp(newCongDist1))
trialData <- c(mcStep, rePartitionTrial,
testvEccenCD1, testgDiamCD1, testCompsCD1,
testvEccenCD2, testgDiamCD2, testCompsCD2,
length(bdyCongDist1), length(bdyCongDist2))
mcData <- rbind(mcData, setNames(trialData, names(mcData)))
allConditions <- testvEccenCD1 <= BASEVECCENCD1 &&
testgDiamCD1 <= BASEGDIAMCD1 &&
contiguous(newCongDist1) &&
testvEccenCD2 <= BASEVECCENCD2 &&
testgDiamCD2 <= BASEGDIAMCD2 && contiguous(newCongDist2)
rePartitionTrial <- rePartitionTrial + 1
if (allConditions)
break
}
# From here on down, is now as of Fri, Feb 21.This need serious checking and
# vetting. also needs improvement, and simplification , now it is clumsy!
if (allConditions) {
congDist1 <- newCongDist1
congDist2 <- newCongDist2
## h <- str_c(sort(nodes(congDist1)), collapse = "")
## if (h %in% names(districtsSeen)) {
## districtsSeen[[h]] <- districtsSeen[[h]] + 1
## } else {
## districtsSeen[[h]] <- 1
## }
outcome <- filter(Elec2018, prec %in% nodes(congDist2)) %>%
group_by(party) %>%
summarise(newelec = sum(as.integer(votes)))
elecOutcomes <-
rbind(elecOutcomes,
setNames(c(mcStep, as.integer(outcome[1, 2]),
as.integer(outcome[2, 2])),
names(elecOutcomes)))
}
}