-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMCsarpy.R
194 lines (158 loc) · 6.35 KB
/
MCMCsarpy.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
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 by using
## these functions to measure against the base.
## Positive is bad (low probability of choosing),
## negative is good (higher prob of choosing)
eccenDiff <- function(graph1, graph2) {
BASEVECCENCD1 <- 7
BASEVECCENCD2 <- 4
totalVEccen <-
(vEccentricity(graph1, "lanc") - BASEVECCENCD1) +
(vEccentricity(graph2, "doug") - BASEVECCENCD2)
return(totalVEccen)
}
diamDiff <- function(graph1, graph2) {
BASEGDIAMCD1 <- 7
BASEGDIAMCD2 <- 6
totalGDiam <- (gDiameter(graph1) - BASEGDIAMCD1) +
(gDiameter(graph2) - BASEGDIAMCD2)
return(totalGDiam)
}
energy <- function(b, w, graph1, graph2) {
e <-
exp( -b * (eccenDiff(graph1, graph2) +
w * diamDiff(graph1, graph2)))
return(e)
}
Q <- function(E,F) {
## Note that transition probability only depend on the number of
## precincts in the FROM state, it's uniform across all TO states
bdyCongDist1 <-
getSarpyPrecincts(getBoundary(E, gSarpy)) # in CongDist2
bdyCongDist2 <-
getSarpyPrecincts(getBoundary(F, gSarpy)) # in CongDist1
q <- 1/( length(bdyCongDist1) * length(bdyCongDist2) )
return(q)
}
acceptance <- function(b, Eprime, Fprime, E, F) {
ratio <-
(energy(b, 2, Eprime, Fprime) * Q(Eprime, Fprime))/
(energy(b, 2, E, F) * Q(E, F))
accep <- min(c(1, ratio))
return(accep)
}
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")
elecOutcomes <- data.frame(mcStep = 0,
cd2DEM = 121770, cd2REP = 126715,
winner = as.character("R"),
vEccCD1 = 7, vEccCD2 = 4,
gDiamCD1 = 7, gDiam = 6,
rePart = 0)
for (b in seq(0, 1, 0.25) ) {
for (mcStep in 1:100) {
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 Cong 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")
rePartitionTrial <- rePartitionTrial + 1
if (contiguous(newCongDist1) && contiguous(newCongDist2)) {
u <- runif(1) #1 sample from default [0,1]
p <- acceptance(b, newCongDist1, newCongDist2, congDist1, congDist2)
if (u <= p) {
congDist1 <- newCongDist1
congDist2 <- newCongDist2
break
}
}
if (rePartitionTrial == 150) {
cat("rePartition Trials reached 150 without new partition\n")
}
}
if (b == 1.0) {
outcome <- filter(Elec2018, prec %in% nodes(congDist2)) %>%
group_by(party) %>%
summarise(newelec = sum(as.integer(votes)))
winner < if ( as.integer(outcome[2, 2]) > as.integer(outcome[1, 2]) ) {
"R" } else {
"D"
}
elecOutcomes <-
rbind(elecOutcomes,
setNames(c(mcStep,
as.integer(outcome[1, 2]),
as.integer(outcome[2, 2]),
winner,
vEccentricity(congDist1, "lanc"),
vEccentricity(congDist2, "doug"),
gDiameter(congDist1),
gDiameter(congDist2),
rePartitionTrial),
names(elecOutcomes)))
}
}
}