In the results table, the median of all standard deviations is assigned to those who have no standard deviation defined.
polls <- polls_us_election_2016 %>%
filter(state != "U.S." & !grepl("CD", state) & enddate >= "2016-10-31" &
(grade %in% c("A+", "A", "A-", "B+") | is.na(grade))) %>%
mutate(spread = rawpoll_clinton/100 - rawpoll_trump/100)
results <- polls %>%
group_by(state) %>%
summarize(avg = mean(spread), sd = sd(spread), n = n()) %>%
mutate(state = as.character(state))
results <- results %>%
mutate(sd = ifelse(is.na(sd), median(results$sd, na.rm=TRUE), sd))
Showing 10 closest races, the battleground states:
results %>% mutate(sigma = sd/sqrt(n),
B = sigma^2/ (sigma^2 + tau^2),
posterior_mean = B*mu + (1-B)*avg,
posterior_se = sqrt( 1 / (1/sigma^2 + 1/tau^2))) %>%
arrange(abs(posterior_mean)) %>% head(10)
| state | avg | sd | n | sigma | B | posterior_mean | posterior_se |
|---|---|---|---|---|---|---|---|
| <chr> | <dbl> | <dbl> | <int> | <dbl> | <dbl> | <dbl> | <dbl> |
| Florida | 0.003557143 | 0.01634960 | 7 | 0.006179569 | 0.03023074 | 0.003449608 | 0.006085446 |
| North Carolina | -0.007300000 | 0.03062083 | 9 | 0.010206942 | 0.07838031 | -0.006727824 | 0.009798769 |
| Ohio | -0.010416667 | 0.02522478 | 6 | 0.010297974 | 0.07967275 | -0.009586742 | 0.009879227 |
| Iowa | -0.019733333 | 0.04366478 | 3 | 0.025209875 | 0.34158821 | -0.012992659 | 0.020455942 |
| Nevada | 0.016857143 | 0.04406809 | 7 | 0.016656171 | 0.18465312 | 0.013744419 | 0.015039949 |
| Michigan | 0.020950000 | 0.02026068 | 6 | 0.008271386 | 0.05289547 | 0.019841840 | 0.008049655 |
| Arizona | -0.032644444 | 0.02695474 | 9 | 0.008984912 | 0.06182651 | -0.030626152 | 0.008702728 |
| Pennsylvania | 0.035333333 | 0.01160528 | 9 | 0.003868426 | 0.01206867 | 0.034906907 | 0.003845012 |
| New Mexico | 0.038933333 | 0.02255187 | 6 | 0.009206761 | 0.06471732 | 0.036413672 | 0.008903860 |
| Georgia | -0.044850000 | 0.02381155 | 4 | 0.011905776 | 0.10371155 | -0.040198537 | 0.011271497 |
Monte Carlo simulation to generate 10,000 outcomes from simulated elections, to make probability statements with Bayesian approach.
Considerations: $\mu$ = 0, $\tau$ = 0.02, $\sigma_{b}$ = 0.03
mu <- 0
tau <- 0.02
biassd <- 0.03
results <- left_join(results, results_us_election_2016, by="state")
clinton_EV <- replicate(1000, {
results %>% mutate(sigma = sqrt(sd^2/n + biassd^2),
B = sigma^2/ (sigma^2 + tau^2),
posterior_mean = B*mu + (1-B)*avg,
posterior_se = sqrt( 1 / (1/sigma^2 + 1/tau^2)),
simulated_result = rnorm(length(posterior_mean), posterior_mean, posterior_se),
clinton = ifelse(simulated_result > 0, electoral_votes, 0)) %>% # award votes if Clinton wins state
summarize(clinton = sum(clinton)) %>% # total votes for Clinton
.$clinton + 7 # 7 votes for Rhode Island and DC
})
mean(clinton_EV > 269) # over 269 votes wins election
Histogram of the Monte Carlos outcomes:
data.frame(clinton_EV) %>%
ggplot(aes(clinton_EV)) +
geom_histogram(binwidth = 1) +
geom_vline(xintercept = 269) + # real valor
theme(axis.text = element_text(size=14))