/
04-explore-visually.R
375 lines (354 loc) · 9.29 KB
/
04-explore-visually.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
### kiernan nicholls
### american university
### spring, 2020
### markets and models
### create exploratory visuals
color_model <- "#ED713A" # 538 brand color
color_market <- "#07A0BB" # PredictIt brand color
color_blue <- "royalblue3" # Democratic
color_red <- "red3" # Republican
elect_date <- as_date("2018-11-06")
# Distribution of original probabilities by method
plot_distribution <-
# Join market onto model keep all model races
full_join(x = model, y = markets, by = c("date", "race", "party")) %>%
# Show only 1 candidate per race
filter(date == "2018-11-05") %>%
select(date, race, close, prob) %>%
rename(markets = close, model = prob) %>%
gather(markets, model, key = method, value = prob) %>%
mutate(method = method %>% recode("model" = "Forecasting Model",
"markets" = "Prediction Markets")) %>%
ggplot(mapping = aes(x = prob, fill = method)) +
geom_histogram(binwidth = 0.10) +
facet_wrap(~method, scales = "free_y", drop = TRUE) +
scale_fill_manual(values = c(color_model, color_market)) +
theme(legend.position = "none",
legend.key = element_blank()) +
scale_x_continuous(breaks = seq(from = 0, to = 1, by = 0.2),
minor_breaks = 0,
labels = scales::percent) +
labs(title = "Distribution of Race Probabilities by Predictive Method",
x = "Democratic Win Probability",
y = "Number of Races") +
theme(legend.position = "none")
ggsave(
plot = plot_distribution,
filename = "plots/plot_distribution.png",
dpi = "retina",
height = 5,
width = 9
)
plot_cartesian <- messy %>%
mutate(party = "D") %>%
filter(date == "2018-11-05") %>%
left_join(model, by = c("date", "race", "party")) %>%
inner_join(results, by = "race") %>%
ggplot(mapping = aes(x = model, y = market)) +
geom_hline(yintercept = 0.5) +
geom_vline(xintercept = 0.5) +
geom_label(
label.size = 0,
fill = "#ebebeb",
size = 6,
mapping = aes(
label = "Market Predicts Win",
x = 0.25,
y = 0.75
)
) +
geom_label(
label.size = 0,
fill = "#ebebeb",
size = 6,
mapping = aes(
label = "Model Predicts Win",
x = 0.75,
y = 0.25
)
) +
geom_label(
label.size = 0,
fill = "#ebebeb",
size = 6,
mapping = aes(
label = "Both Predict Loss",
x = 0.25,
y = 0.25
)
) +
geom_label(
label.size = 0,
fill = "#ebebeb",
size = 6,
mapping = aes(
label = "Both Predict Win",
x = 0.75,
y = 0.75
)
) +
geom_abline(
slope = 1,
intercept = 0,
lty = 2
) +
geom_point(
size = 6,
alpha = 0.66,
mapping = aes(
color = winner,
shape = chamber
)
) +
scale_y_continuous(labels = scales::dollar) +
scale_x_continuous(labels = scales::percent) +
scale_color_manual(values = c("red", "forestgreen")) +
theme(
legend.position = "bottom",
legend.key = element_blank()
) +
labs(
title = "Races by Democratic Probability",
subtitle = "November 5th, 2018",
x = "Model Probability",
y = "Market Price",
shape = "Chamber",
color = "Won"
)
ggsave(
plot = plot_cartesian,
filename = "plots/plot_cartesian.png",
dpi = "retina",
height = 5,
width = 9
)
# weird NJ-02 Market Error
plot_manipulation <- markets %>%
filter(race == "NJ-02", date > "2018-10-25") %>%
ggplot(aes(x = date, y = close)) +
geom_hline(yintercept = 0.5) +
geom_line(mapping = aes(color = party), size = 2) +
scale_color_manual(values = c(color_blue, color_red)) +
scale_y_continuous(labels = scales::dollar) +
scale_x_date() +
labs(
title = "Price History of New Jersey 2nd Betting Market",
color = "Method",
x = "Date",
y = "Closing Price"
)
ggsave(
plot = plot_manipulation,
filename = "plots/plot_manipulation.png",
dpi = "retina",
height = 5.625,
width = 10
)
plot_proportion <- hits %>%
mutate(week = week(date)) %>%
group_by(week, method) %>%
summarise(prop = mean(hit, na.rm = TRUE)) %>%
ggplot(mapping = aes(x = week, y = prop, color = method)) +
geom_line(size = 2) +
coord_cartesian(ylim = c(0.75, 0.95)) +
scale_y_continuous(labels = scales::percent) +
scale_color_manual(values = c(color_market, color_model)) +
theme(
legend.position = "bottom",
legend.key = element_blank()
) +
labs(
title = "Proportion Accuracy",
color = "Method",
y = "Proportion",
x = "Week of Year"
)
ggsave(
plot = plot_proportion,
filename = "plots/plot_proportion.png",
dpi = "retina",
height = 5,
width = 9
)
plot_brier <- hits %>%
mutate(brier = (prob - winner)^2) %>%
mutate(prior = week(date) - 45) %>%
group_by(prior, method) %>%
summarise(mean = mean(brier, na.rm = TRUE)) %>%
ggplot(aes(x = prior, y = mean, color = method)) +
geom_line(size = 2) +
scale_color_manual(values = c(color_market, color_model)) +
scale_x_continuous(breaks = seq(-20, 0, by = 2), labels = abs) +
theme(
legend.position = "bottom",
legend.key = element_blank()
) +
labs(
title = "Prediction Score",
color = "Method",
y = "Weekly Mean Brier Score",
x = "Weeks Until Election"
)
ggsave(
plot = plot_brier,
filename = "plots/plot_brier.png",
dpi = "retina",
height = 5,
width = 9
)
plot_calibration <- hits %>%
mutate(bin = prob %>% round(digits = 1)) %>%
group_by(method, bin) %>%
summarise(prop = mean(winner), n = n()) %>%
ggplot(mapping = aes(bin, prop)) +
geom_abline(intercept = 0, slope = 1, lty = 2) +
geom_point(mapping = aes(color = method, size = n), alpha = 0.75) +
geom_label(
label.size = 0,
fill = "#ebebeb",
size = 6,
mapping = aes(
label = "Underconfident",
x = 0.25,
y = 0.75
)
) +
geom_label(
label.size = 0,
fill = "#ebebeb",
size = 6,
mapping = aes(
label = "Overconfident",
x = 0.75,
y = 0.25
)
) +
scale_x_continuous(
breaks = seq(0, 1, 0.1), minor_breaks = 0,
labels = scales::percent
) +
scale_y_continuous(
breaks = seq(0, 1, 0.1), minor_breaks = 0,
labels = scales::percent
) +
scale_color_manual(
values = c(color_market, color_model),
guide = FALSE
) +
scale_size(range = c(5, 20), guide = FALSE) +
theme(
legend.position = "bottom",
legend.key = element_blank()
) +
labs(
title = "Prediction Calibration",
y = "Actual Proportion",
x = "Expected Proportion"
)
ggsave(
plot = plot_calibration,
filename = "plots/plot_calibration.png",
dpi = "retina",
height = 5,
width = 9
)
plot_confidence <- hits %>%
mutate(week = week(date)) %>%
group_by(week, method, hit) %>%
summarise(mean = mean(prob)) %>%
ggplot(mapping = aes(x = week, y = mean)) +
geom_hline(yintercept = 0.50, lty = 2) +
geom_line(
size = 2,
mapping = aes(
color = method,
linetype = hit
)
) +
scale_color_manual(values = c(color_market, color_model)) +
scale_y_continuous(labels = scales::percent) +
scale_linetype_manual(values = c("twodash", "solid")) +
theme(
legend.position = "bottom",
legend.key = element_blank()
) +
labs(
title = "Prediction Confidence",
y = "Mean Probability",
color = "Method",
linetype = "Correct"
)
ggsave(
plot = plot_confidence,
filename = "plots/plot_confidence.png",
dpi = "retina",
height = 5,
width = 9
)
# Number of dollars traded over time
plot_dollars <- markets %>%
filter(date >= "2018-01-01", date <= "2018-11-05") %>%
group_by(date) %>%
mutate(traded = close * volume) %>%
summarise(sum = sum(traded, na.rm = TRUE)) %>%
mutate(cumsum = cumsum(sum)) %>%
ggplot(mapping = aes(x = date, y = cumsum)) +
geom_line(color = color_market, size = 2) +
geom_vline(xintercept = as.Date("2018-08-01"), size = 0.5) +
geom_vline(xintercept = as.Date("2018-11-05"), size = 0.5) +
scale_y_continuous(labels = scales::dollar) +
labs(
title = "Cumulative Dollars Traded on Election Markets",
x = "Date",
y = "Dollars Traded to Date"
)
ggsave(
plot = plot_dollars,
filename = "plots/plot_dollars.png",
dpi = "retina",
height = 5,
width = 9
)
# Number of markets opened over time
plot_markets <- markets %>%
filter(date > "2018-01-01", date < "2018-11-05") %>%
group_by(date) %>%
summarise(count = n()) %>%
ggplot(mapping = aes(x = date, y = count)) +
geom_line(color = color_market, size = 2) +
geom_vline(xintercept = as_date("2018-08-01"), size = 0.5) +
geom_vline(xintercept = as_date("2018-11-05"), size = 0.5) +
labs(
title = "Cumulative Number of Election Markets",
x = "Date",
y = "Markets to Date"
)
ggsave(
plot = plot_markets,
filename = "plots/plot_markets.png",
dpi = "retina",
height = 5,
width = 9
)
# Number of polls conducted over time
plot_polls <- polling %>%
group_by(start_date) %>%
summarise(n = n()) %>%
mutate(cumsum = cumsum(n)) %>%
filter(start_date >= "2018-01-01", start_date <= "2018-11-05") %>%
ggplot(mapping = aes(x = start_date, y = cumsum)) +
geom_line(color = color_model, size = 2) +
geom_vline(xintercept = as.Date("2018-08-01"), size = 0.5) +
geom_vline(xintercept = as.Date("2018-11-04"), size = 0.5) +
labs(
title = "Cumulative Number of Congressional Polls",
x = "Date",
y = "Polls to Date"
)
ggsave(
plot = plot_polls,
filename = "plots/plot_polls.png",
dpi = "retina",
height = 5,
width = 9
)