polls <- polls_us_election_2016 %>%
filter(state != "U.S." & enddate >= "2016-10-31") %>%
mutate(spread = rawpoll_clinton/100 - rawpoll_trump/100)
cis <- polls %>%
mutate(X_hat = (spread + 1)/2,
se = 2*sqrt(X_hat*(1-X_hat)/samplesize),
lower = spread - qnorm(0.975)*se,
upper = spread + qnorm(0.975)*se) %>%
select(state, startdate, enddate, pollster, grade, spread, lower, upper)
add <- results_us_election_2016 %>%
mutate(actual_spread = clinton/100 - trump/100) %>%
select(state, actual_spread)
ci_data <- cis %>%
mutate(state = as.character(state)) %>%
left_join(add, by = "state")
Calculating proportion of colleges that has the actual value in your confidence interval:
p_hits <- ci_data %>%
mutate(hit = (actual_spread >= lower & actual_spread <= upper)) %>%
summarize(hits = mean(hit))
p_hits$hits
Summarizing states that has 5 or more polls and showing the proportion of hits for each state:
p_hits <- ci_data %>%
mutate(hit = (actual_spread >= lower & actual_spread <= upper)) %>%
group_by(state) %>%
filter(n() >= 5) %>%
summarize(proportion_hits = mean(hit), n = n())
p_hits %>%
mutate(state = reorder(state, proportion_hits)) %>%
ggplot(aes(state, proportion_hits)) +
geom_bar(stat = "identity") +
coord_flip() +
theme(axis.text = element_text(size=14))
Calculating the difference between predicted and actual spreads:
errors <- ci_data %>%
mutate(error = spread - actual_spread,
hit = sign(spread) == sign(actual_spread))
median(errors$error)
Boxplot of the errors by state for polls with grades B+ or higher:
errors %>% filter(grade %in% c("A+","A","A-","B+")) %>%
mutate(state = reorder(state, error)) %>%
ggplot(aes(state, error)) +
geom_boxplot() +
geom_point() +
theme(axis.text = element_text(size=14),
axis.text.x = element_text(angle = 90, hjust = 1))
Boxplot of the errors by state for polls only for states with at least 5 polls with grades B+ or higher:
errors %>% filter(grade %in% c("A+","A","A-","B+")) %>%
group_by(state) %>%
filter(n() >= 5) %>%
ungroup() %>%
mutate(state = reorder(state, error)) %>%
ggplot(aes(state, error)) +
geom_boxplot() +
geom_point() +
theme(axis.text = element_text(size=14))