-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-compare-methods.R
More file actions
135 lines (114 loc) · 3.16 KB
/
Copy path03-compare-methods.R
File metadata and controls
135 lines (114 loc) · 3.16 KB
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
### kiernan nicholls
### american university
### spring, 2020
### markets and models
### check predictions against results
# isolate predictions ------------------------------------------------------------------------
## we only need probability for one candidate in each race
## some markets only have data on candidate from 1 party
# take the complimentary probability if only GOP data
# find race codes for markets with data on only one candidate
single_party_markets <- markets %>%
group_by(date, race) %>%
summarise(n = n()) %>%
filter(n == 1) %>%
ungroup() %>%
pull(race) %>%
unique()
# invert the GOP prices for markets with only GOP candidates
invert <- function(x) 1 - x
invert_gop <- markets %>%
filter(
race %in% single_party_markets,
party == "R"
) %>%
mutate(
close = invert(close),
party = "D"
)
# take all but the only GOP markets
original_dem <- markets %>%
filter(
!race %in% invert_gop$race,
party == "D"
)
# combined both back together
markets2 <-
bind_rows(original_dem, invert_gop) %>%
select(date, race, close) %>%
arrange(date, race)
# create model data with only dem party info
model2 <- model %>%
group_by(date, race, party) %>%
summarise(prob = sum(prob)) %>%
ungroup() %>%
filter(party == "D") %>%
select(-party)
# join wide ----------------------------------------------------------------------------------
# join democratic predictions from both markets and models for comparison
# Keep market and model data in seperate columns
messy <-
inner_join(
markets2, model2,
by = c("date", "race")
) %>%
filter(
date >= "2018-08-01",
date <= "2018-11-05"
) %>%
rename(
model = prob,
market = close
)
# pivot longer -------------------------------------------------------------------------------
# make the data tidy with each prediction as an observation
tidy <- messy %>%
pivot_longer(
cols = c("model", "market"),
names_to = "method",
values_to = "prob"
) %>%
arrange(date, race, method)
# join results -------------------------------------------------------------------------------
hits <- tidy %>%
mutate(pred = prob > 0.5) %>%
inner_join(results, by = "race") %>%
mutate(hit = pred == winner) %>%
select(date, race, method, prob, pred, winner, hit)
# statistical tests --------------------------------------------------------------------------
# run a welch two sample t-test
# p-value = 0.001691
test_student <- t.test(
formula = hit ~ method,
data = hits,
alternative = "greater"
)
# run a 2-sample test for equality of proportions
# p-value = 0.1324
test_prop <- hits %>%
select(date, race, method, hit) %>%
pivot_wider(
names_from = "method",
values_from = "hit"
) %>%
select(market, model) %>%
colSums() %>%
prop.test(n = nrow(hits)/2 %>% rep(2))
# all
hits %>%
group_by(pred, winner, method) %>%
summarise(prob = mean(prob), n = n()) %>%
arrange(pred, winner)
compare <- mutate(hits, brier = round((winner - prob)^2, 4))
# save text file
write_csv(
x = compare,
path = "data/new/compare.csv",
na = ""
)
# run a brier score t-test
# p-value = 0.001691
test_brier <- t.test(
formula = brier ~ method,
data = compare
)