diff --git a/README.Rmd b/README.Rmd index 53ec3d2..0fa5e79 100755 --- a/README.Rmd +++ b/README.Rmd @@ -41,6 +41,9 @@ library(pbapply) library(urbnmapr) library(ggrepel) library(DT) + +# source('scripts/model/final_2016.R');source('scripts/model/final_2012.R');source('scripts/model/final_2008.R');beepr::beep(2); + ``` @@ -57,7 +60,7 @@ start_date <- as.Date("2008-03-01") # Keeping all polls after March 1, 2016 evs_df <- read_csv('data/2012.csv') # will read in the most recent back-test for 2008 -out <- read_rds(sprintf('models/backtest_2008/stan_model_%s.rds',RUN_DATE)) +out <- read_rds(sprintf('models/backtest_2008/stan_model_%s_normal_final.rds',RUN_DATE)) # get all the polling data #setwd(here("data/")) @@ -208,8 +211,8 @@ pct_obama <- pblapply(1:dim(predicted_score)[3], temp <- predicted_score[,,x] # put in tibble - tibble(low = apply(temp,2,function(x){(quantile(x,0.05))}), - high = apply(temp,2,function(x){(quantile(x,0.95))}), + tibble(low = apply(temp,2,function(x){(quantile(x,0.025))}), + high = apply(temp,2,function(x){(quantile(x,0.975))}), mean = apply(temp,2,function(x){(mean(x))}), prob = apply(temp,2,function(x){(mean(x>0.5))}), state = x) @@ -238,8 +241,8 @@ pct_obama_natl <- pblapply(1:dim(predicted_score)[2], pct_obama_natl <- pct_obama_natl %>% group_by(t) %>% - summarise(low = quantile(natl_vote,0.05), - high = quantile(natl_vote,0.95), + summarise(low = quantile(natl_vote,0.025), + high = quantile(natl_vote,0.975), mean = mean(natl_vote), prob = mean(natl_vote > 0.5)) %>% mutate(state = '--') @@ -251,7 +254,7 @@ pct_obama <- pct_obama %>% # look ex_states <- c('IA','FL','OH','WI','MI','PA','AZ','NC','NH','TX','GA','MN') -pct_obama %>% filter(t == RUN_DATE,state %in% c(ex_states,'--')) %>% mutate(se = (high - mean)/1.68) %>% dplyr::select(-t) +pct_obama %>% filter(t == RUN_DATE,state %in% c(ex_states,'--')) %>% mutate(se = (high - mean)/1.96) %>% dplyr::select(-t) map.2008.gg <- urbnmapr::states %>% left_join(pct_obama %>% filter(t == max(t)) %>% @@ -473,6 +476,32 @@ final_evs.2008.gg <- ggplot(final_evs,aes(x=dem_ev, subtitle=sprintf("p(dem win) = %s | full stan model",round(mean(final_evs$dem_ev>=270),3))) + +# get the p value of the actual results +p_values_2008 <- draws %>% + filter(state != '--', + t == election_day) %>% + left_join(politicaldata::pres_results %>% filter(year == 2008) %>% + mutate(actual = dem/(dem+rep)) %>% + dplyr::select(state,actual)) %>% + group_by(state) %>% + summarise(high = quantile(pct_obama, 0.975), + low = quantile(pct_obama, 0.025), + mean = mean(pct_obama), + prob = mean(pct_obama > 0.5), + actual = unique(actual), + p_value = + ( 2*sum(pct_obama < actual) + 1 ) / + ( 2 * n() + 2) #, + # p_value_2 = mean(ifelse(actual >= mean(pct_obama), + # pct_obama >= actual, + # pct_obama <= actual)) + ) %>% + arrange(p_value) %>% + mutate(year = 2008, + outside_ci = ifelse(actual > high | actual < low, TRUE,FALSE)) + + ``` #### Map @@ -547,7 +576,7 @@ start_date <- as.Date("2012-03-01") # Keeping all polls after March 1, 2016 evs_df <- read_csv('data/2012.csv') # will read in the most recent back-test for 2012 -out <- read_rds(sprintf('models/backtest_2012/stan_model_%s.rds',RUN_DATE)) +out <- read_rds(sprintf('models/backtest_2012/stan_model_%s_normal_final.rds',RUN_DATE)) # get all the polling data #setwd(here("data/")) @@ -693,8 +722,8 @@ pct_obama <- pblapply(1:dim(predicted_score)[3], temp <- predicted_score[,,x] # put in tibble - tibble(low = apply(temp,2,function(x){(quantile(x,0.05))}), - high = apply(temp,2,function(x){(quantile(x,0.95))}), + tibble(low = apply(temp,2,function(x){(quantile(x,0.025))}), + high = apply(temp,2,function(x){(quantile(x,0.975))}), mean = apply(temp,2,function(x){(mean(x))}), prob = apply(temp,2,function(x){(mean(x>0.5))}), state = x) @@ -723,8 +752,8 @@ pct_obama_natl <- pblapply(1:dim(predicted_score)[2], pct_obama_natl <- pct_obama_natl %>% group_by(t) %>% - summarise(low = quantile(natl_vote,0.05), - high = quantile(natl_vote,0.95), + summarise(low = quantile(natl_vote,0.025), + high = quantile(natl_vote,0.975), mean = mean(natl_vote), prob = mean(natl_vote > 0.5)) %>% mutate(state = '--') @@ -736,7 +765,7 @@ pct_obama <- pct_obama %>% # look ex_states <- c('IA','FL','OH','WI','MI','PA','AZ','NC','NH','TX','GA','MN') -pct_obama %>% filter(t == RUN_DATE,state %in% c(ex_states,'--')) %>% mutate(se = (high - mean)/1.68) %>% dplyr::select(-t) +pct_obama %>% filter(t == RUN_DATE,state %in% c(ex_states,'--')) %>% mutate(se = (high - mean)/1.96) %>% dplyr::select(-t) map.2012.gg <- urbnmapr::states %>% left_join(pct_obama %>% filter(t == max(t)) %>% @@ -962,6 +991,33 @@ final_evs.2012.gg <- ggplot(final_evs,aes(x=dem_ev, subtitle=sprintf("p(dem win) = %s | full stan model",round(mean(final_evs$dem_ev>=270),3))) + + +# get the p value of the actual results +p_values_2012 <- draws %>% + filter(state != '--', + t == election_day) %>% + left_join(politicaldata::pres_results %>% filter(year == 2012) %>% + mutate(actual = dem/(dem+rep)) %>% + dplyr::select(state,actual)) %>% + group_by(state) %>% + summarise(high = quantile(pct_obama, 0.975), + low = quantile(pct_obama, 0.025), + mean = mean(pct_obama), + prob = mean(pct_obama > 0.5), + actual = unique(actual), + p_value = + ( 2*sum(pct_obama < actual) + 1 ) / + ( 2 * n() + 2) #, + # p_value_2 = mean(ifelse(actual >= mean(pct_obama), + # pct_obama >= actual, + # pct_obama <= actual)) + ) %>% + arrange(p_value) %>% + mutate(year = 2012, + outside_ci = ifelse(actual > high | actual < low, TRUE,FALSE)) + + ``` @@ -1038,7 +1094,7 @@ start_date <- as.Date("2016-03-01") # Keeping all polls after March 1, 2016 evs_df <- read_csv('data/2012.csv') # will read in the most recent back-test for 2012 -out <- read_rds(sprintf('models/stan_model_%s.rds',RUN_DATE)) +out <- read_rds(sprintf('models/stan_model_%s_normal_final.rds',RUN_DATE)) # get all the polling data #setwd(here("data/")) @@ -1457,6 +1513,34 @@ final_evs.2016.gg <- ggplot(final_evs,aes(x=dem_ev, subtitle=sprintf("p(dem win) = %s | full stan model",round(mean(final_evs$dem_ev>=270),3))) + + +# get the p value of the actual results +p_values_2016 <- draws %>% + filter(state != '--', + t == election_day) %>% + left_join(politicaldata::pres_results %>% filter(year == 2016) %>% + mutate(actual = dem/(dem+rep)) %>% + dplyr::select(state,actual)) %>% + group_by(state) %>% + summarise(high = quantile(pct_clinton, 0.975), + low = quantile(pct_clinton, 0.025), + mean = mean(pct_clinton), + prob = mean(pct_clinton > 0.5), + actual = unique(actual), + p_value = + ( 2*sum(pct_clinton < actual) + 1 ) / + ( 2 * n() + 2) #, + # p_value_2 = mean(ifelse(actual >= mean(pct_clinton), + # pct_clinton >= actual, + # pct_clinton <= actual)) + ) %>% + arrange(p_value) %>% + mutate(year = 2016, + outside_ci = ifelse(actual > high | actual < low, TRUE,FALSE)) + + + ``` @@ -1523,7 +1607,7 @@ pct_clinton %>% ## Cumulative charts -### Calibration plot +### Probability calibration plot ```{r echo=F,message=F,warning=F} calibration_data <- model_v_actual.2016 %>% mutate(year = 2016) %>% @@ -1551,6 +1635,63 @@ print(calibration.gg) ``` +### Confidence interval coverage + +```{r echo=F, message=FALSE, warning=FALSE} + +calibration_data <- p_values_2016 %>% mutate(year = 2016) %>% + bind_rows(p_values_2012 %>% mutate(year = 2012) ) %>% + bind_rows(p_values_2008 %>% mutate(year = 2008)) %>% + filter(state != 'DC') + + +# sum(calibration_data$outside_ci) / nrow(calibration_data) + +p_value_hist.gg <- ggplot(calibration_data, aes(x=p_value)) + + geom_histogram(binwidth = 0.01,aes(group=paste0(state,year))) + + geom_vline(xintercept = 0.025,col='red',linetype=2) + + geom_vline(xintercept = 0.975,col='red',linetype=2) + + scale_x_continuous(breaks=seq(0,1,0.05)) + + scale_y_continuous(breaks=seq(0,100,2)) + + theme_minimal() + + theme(panel.grid.minor = element_blank()) + + +preds_p_value_scatter.gg <- ggplot(calibration_data, aes(x=mean*100,y=actual*100)) + + geom_abline() + + # states inside the ci + geom_point(data =. %>% filter(!outside_ci),col='black') + + # states outside + geom_point(data = . %>% filter(outside_ci),col='red') + + geom_text_repel(data = . %>% filter(outside_ci), + aes(label=state),col='red',min.segment.length = 0) + + geom_segment(data = . %>% filter(outside_ci), + aes(x=mean*100,xend=mean*100,y=low*100,yend=high*100),col='red') + + facet_wrap(~year,ncol=3) + + theme_minimal() + + theme(panel.grid.minor = element_blank()) + + labs( + # title='Model with normal(0, sigma) distributions for polling error and state priors', + subtitle='Results in highlighted states fall outside our uncertainty intervals', + x='Predicted Democratic share of the two-party vote', + y='Actual Democratic share' + ) + + coord_cartesian(xlim=c(25,75),ylim=c(25,75)) + + + + +``` + +```{r echo=F,message=F,warning=F,fig.width = 9, fig.asp = .42} +print(preds_p_value_scatter.gg) +``` + +```{r echo=F,message=F,warning=F} +print(p_value_hist.gg) +``` + + # Licence This software is published by _[The Economist](https://www.economist.com)_ under the [MIT licence](https://opensource.org/licenses/MIT). The data generated by _The Economist_ are available under the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/). diff --git a/README.md b/README.md index bdfbbf8..4fb2034 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ State and national presidential election forecasting model ================ -Last update on Wednesday August 05, 2020 at 09:53 AM EDT +Last update on Thursday October 15, 2020 at 12:29 PM EDT Code for a dynamic multilevel Bayesian model to predict US presidential elections. Written in R and Stan. @@ -72,68 +72,68 @@ Here is a graphical summary of the model’s performance in 2008, 2012 and | outlet | ev\_wtd\_brier | unwtd\_brier | states\_correct | | :------------------- | -------------: | -----------: | --------------: | -| economist (backtest) | 0.0327228 | 0.0290726 | 49 | +| economist (backtest) | 0.0321964 | 0.0289902 | 49 | ![](README_files/figure-gfm/unnamed-chunk-7-1.png) - ## [1] 0.02310776 + ## [1] 0.02318035 #### Predictions for each state | state | mean | low | high | prob | se | | :---- | ----: | ----: | ----: | ----: | ----: | -| NC | 0.501 | 0.471 | 0.533 | 0.525 | 0.019 | -| MO | 0.509 | 0.479 | 0.539 | 0.693 | 0.018 | -| FL | 0.513 | 0.483 | 0.545 | 0.774 | 0.019 | -| IN | 0.483 | 0.454 | 0.513 | 0.178 | 0.018 | -| AR | 0.477 | 0.443 | 0.508 | 0.112 | 0.019 | -| OH | 0.524 | 0.495 | 0.554 | 0.908 | 0.018 | -| VA | 0.526 | 0.495 | 0.557 | 0.922 | 0.018 | -| MT | 0.474 | 0.445 | 0.505 | 0.082 | 0.018 | -| GA | 0.473 | 0.441 | 0.504 | 0.078 | 0.019 | -| NV | 0.531 | 0.500 | 0.562 | 0.949 | 0.018 | -| WV | 0.469 | 0.439 | 0.501 | 0.053 | 0.019 | -| AZ | 0.468 | 0.437 | 0.500 | 0.048 | 0.019 | -| CO | 0.533 | 0.502 | 0.564 | 0.956 | 0.018 | -| LA | 0.460 | 0.425 | 0.494 | 0.026 | 0.021 | -| – | 0.540 | 0.520 | 0.557 | 1.000 | 0.011 | -| MS | 0.456 | 0.423 | 0.490 | 0.019 | 0.020 | -| TX | 0.453 | 0.418 | 0.488 | 0.015 | 0.020 | -| SD | 0.452 | 0.421 | 0.483 | 0.005 | 0.019 | -| NH | 0.550 | 0.519 | 0.580 | 0.996 | 0.018 | -| SC | 0.449 | 0.416 | 0.482 | 0.005 | 0.020 | -| ND | 0.448 | 0.417 | 0.480 | 0.004 | 0.019 | -| PA | 0.553 | 0.524 | 0.583 | 0.998 | 0.018 | -| TN | 0.444 | 0.414 | 0.476 | 0.002 | 0.019 | -| WI | 0.557 | 0.526 | 0.586 | 0.999 | 0.017 | -| KY | 0.441 | 0.412 | 0.472 | 0.001 | 0.018 | -| MN | 0.559 | 0.528 | 0.588 | 0.999 | 0.017 | -| NM | 0.560 | 0.527 | 0.592 | 0.997 | 0.019 | -| IA | 0.561 | 0.531 | 0.591 | 1.000 | 0.018 | -| MI | 0.564 | 0.534 | 0.593 | 1.000 | 0.017 | -| OR | 0.571 | 0.542 | 0.600 | 1.000 | 0.017 | -| KS | 0.423 | 0.393 | 0.454 | 0.000 | 0.018 | -| ME | 0.583 | 0.552 | 0.614 | 1.000 | 0.018 | -| WA | 0.584 | 0.553 | 0.613 | 1.000 | 0.018 | -| AK | 0.416 | 0.383 | 0.449 | 0.000 | 0.020 | -| NJ | 0.590 | 0.559 | 0.620 | 1.000 | 0.018 | -| AL | 0.405 | 0.373 | 0.438 | 0.000 | 0.020 | -| NE | 0.402 | 0.370 | 0.434 | 0.000 | 0.019 | -| DE | 0.615 | 0.582 | 0.648 | 1.000 | 0.020 | -| CA | 0.616 | 0.584 | 0.648 | 1.000 | 0.019 | -| CT | 0.618 | 0.586 | 0.649 | 1.000 | 0.018 | -| MD | 0.618 | 0.580 | 0.654 | 1.000 | 0.021 | -| OK | 0.378 | 0.347 | 0.411 | 0.000 | 0.020 | -| IL | 0.629 | 0.597 | 0.660 | 1.000 | 0.018 | -| WY | 0.366 | 0.335 | 0.396 | 0.000 | 0.018 | -| MA | 0.642 | 0.611 | 0.673 | 1.000 | 0.018 | -| NY | 0.646 | 0.617 | 0.675 | 1.000 | 0.017 | -| ID | 0.354 | 0.324 | 0.383 | 0.000 | 0.018 | -| VT | 0.654 | 0.623 | 0.683 | 1.000 | 0.018 | -| UT | 0.342 | 0.312 | 0.373 | 0.000 | 0.018 | -| HI | 0.662 | 0.628 | 0.697 | 1.000 | 0.021 | -| RI | 0.670 | 0.639 | 0.698 | 1.000 | 0.017 | -| DC | 0.933 | 0.920 | 0.944 | 1.000 | 0.007 | +| NC | 0.502 | 0.466 | 0.538 | 0.545 | 0.021 | +| MO | 0.509 | 0.474 | 0.543 | 0.704 | 0.020 | +| FL | 0.514 | 0.479 | 0.548 | 0.788 | 0.020 | +| IN | 0.484 | 0.448 | 0.519 | 0.179 | 0.021 | +| AR | 0.477 | 0.439 | 0.516 | 0.124 | 0.023 | +| OH | 0.524 | 0.489 | 0.559 | 0.917 | 0.021 | +| MT | 0.475 | 0.437 | 0.512 | 0.094 | 0.022 | +| VA | 0.527 | 0.491 | 0.564 | 0.917 | 0.022 | +| GA | 0.473 | 0.437 | 0.511 | 0.083 | 0.022 | +| WV | 0.470 | 0.434 | 0.507 | 0.058 | 0.022 | +| AZ | 0.468 | 0.429 | 0.507 | 0.055 | 0.023 | +| NV | 0.532 | 0.494 | 0.568 | 0.952 | 0.022 | +| CO | 0.533 | 0.496 | 0.571 | 0.961 | 0.022 | +| – | 0.539 | 0.517 | 0.562 | 1.000 | 0.013 | +| LA | 0.460 | 0.421 | 0.500 | 0.025 | 0.024 | +| MS | 0.457 | 0.418 | 0.497 | 0.019 | 0.024 | +| TX | 0.454 | 0.413 | 0.495 | 0.015 | 0.024 | +| SD | 0.453 | 0.413 | 0.492 | 0.008 | 0.023 | +| SC | 0.450 | 0.412 | 0.488 | 0.003 | 0.023 | +| NH | 0.550 | 0.514 | 0.587 | 0.998 | 0.022 | +| ND | 0.449 | 0.409 | 0.487 | 0.006 | 0.023 | +| PA | 0.554 | 0.518 | 0.588 | 0.999 | 0.020 | +| TN | 0.445 | 0.406 | 0.484 | 0.003 | 0.023 | +| WI | 0.557 | 0.521 | 0.592 | 0.999 | 0.021 | +| KY | 0.442 | 0.405 | 0.478 | 0.001 | 0.021 | +| MN | 0.559 | 0.523 | 0.595 | 1.000 | 0.021 | +| NM | 0.561 | 0.522 | 0.599 | 0.998 | 0.023 | +| IA | 0.562 | 0.526 | 0.597 | 1.000 | 0.021 | +| MI | 0.565 | 0.530 | 0.598 | 1.000 | 0.020 | +| OR | 0.572 | 0.535 | 0.608 | 1.000 | 0.021 | +| KS | 0.424 | 0.387 | 0.460 | 0.000 | 0.022 | +| AK | 0.417 | 0.377 | 0.458 | 0.000 | 0.024 | +| WA | 0.584 | 0.548 | 0.620 | 1.000 | 0.021 | +| ME | 0.584 | 0.545 | 0.620 | 1.000 | 0.021 | +| NJ | 0.590 | 0.552 | 0.627 | 1.000 | 0.022 | +| AL | 0.405 | 0.368 | 0.444 | 0.000 | 0.023 | +| NE | 0.402 | 0.365 | 0.440 | 0.000 | 0.023 | +| DE | 0.616 | 0.577 | 0.652 | 1.000 | 0.022 | +| CA | 0.617 | 0.578 | 0.653 | 1.000 | 0.022 | +| MD | 0.618 | 0.572 | 0.664 | 1.000 | 0.027 | +| CT | 0.619 | 0.579 | 0.655 | 1.000 | 0.022 | +| OK | 0.378 | 0.340 | 0.417 | 0.000 | 0.023 | +| IL | 0.629 | 0.591 | 0.663 | 1.000 | 0.020 | +| WY | 0.366 | 0.331 | 0.404 | 0.000 | 0.022 | +| MA | 0.642 | 0.603 | 0.680 | 1.000 | 0.023 | +| ID | 0.354 | 0.318 | 0.392 | 0.000 | 0.022 | +| NY | 0.647 | 0.610 | 0.683 | 1.000 | 0.021 | +| VT | 0.654 | 0.615 | 0.693 | 1.000 | 0.023 | +| UT | 0.343 | 0.306 | 0.381 | 0.000 | 0.023 | +| HI | 0.663 | 0.622 | 0.703 | 1.000 | 0.024 | +| RI | 0.670 | 0.633 | 0.706 | 1.000 | 0.021 | +| DC | 0.933 | 0.917 | 0.946 | 1.000 | 0.008 | ### 2012 @@ -166,70 +166,70 @@ Here is a graphical summary of the model’s performance in 2008, 2012 and | Silver/538 | NA | 0.0091100 | NA | | Jackman/Pollster | NA | 0.0097100 | NA | | Desart/Holbrook | NA | 0.0160500 | NA | -| economist (backtest) | 0.0320587 | 0.0188167 | 50 | +| economist (backtest) | 0.0324297 | 0.0193188 | 50 | | Intrade | NA | 0.0281200 | NA | | Enten/Margin of Error | NA | 0.0507500 | NA | ![](README_files/figure-gfm/unnamed-chunk-15-1.png) - ## [1] 0.02241299 + ## [1] 0.02247233 #### Predictions for each state | state | mean | low | high | prob | se | | :---- | ----: | ----: | ----: | ----: | ----: | -| VA | 0.504 | 0.475 | 0.533 | 0.589 | 0.017 | -| FL | 0.495 | 0.466 | 0.525 | 0.391 | 0.018 | -| CO | 0.506 | 0.476 | 0.535 | 0.620 | 0.017 | -| – | 0.509 | 0.490 | 0.529 | 0.785 | 0.012 | -| OH | 0.510 | 0.482 | 0.539 | 0.721 | 0.017 | -| NH | 0.513 | 0.483 | 0.543 | 0.768 | 0.018 | -| NC | 0.486 | 0.457 | 0.515 | 0.212 | 0.017 | -| IA | 0.515 | 0.486 | 0.544 | 0.793 | 0.017 | -| NV | 0.516 | 0.486 | 0.547 | 0.807 | 0.018 | -| WI | 0.521 | 0.491 | 0.551 | 0.884 | 0.018 | -| PA | 0.528 | 0.498 | 0.558 | 0.936 | 0.018 | -| MN | 0.534 | 0.504 | 0.564 | 0.971 | 0.018 | -| MI | 0.538 | 0.509 | 0.568 | 0.983 | 0.018 | -| OR | 0.539 | 0.508 | 0.571 | 0.979 | 0.019 | -| MO | 0.460 | 0.430 | 0.491 | 0.014 | 0.018 | -| NM | 0.540 | 0.509 | 0.572 | 0.983 | 0.019 | -| IN | 0.456 | 0.426 | 0.487 | 0.009 | 0.018 | -| MT | 0.453 | 0.423 | 0.484 | 0.004 | 0.018 | -| AZ | 0.453 | 0.421 | 0.483 | 0.006 | 0.018 | -| GA | 0.452 | 0.420 | 0.484 | 0.006 | 0.019 | -| NJ | 0.556 | 0.525 | 0.587 | 0.999 | 0.018 | -| ME | 0.558 | 0.527 | 0.589 | 0.999 | 0.018 | -| SC | 0.439 | 0.401 | 0.475 | 0.004 | 0.022 | -| WA | 0.562 | 0.531 | 0.591 | 1.000 | 0.017 | -| CT | 0.567 | 0.537 | 0.596 | 1.000 | 0.017 | -| SD | 0.431 | 0.398 | 0.465 | 0.000 | 0.020 | -| ND | 0.423 | 0.393 | 0.454 | 0.000 | 0.019 | -| MS | 0.420 | 0.382 | 0.458 | 0.000 | 0.023 | -| TN | 0.419 | 0.388 | 0.450 | 0.000 | 0.019 | -| WV | 0.416 | 0.384 | 0.452 | 0.000 | 0.021 | -| CA | 0.585 | 0.555 | 0.615 | 1.000 | 0.018 | -| MA | 0.588 | 0.559 | 0.617 | 1.000 | 0.017 | -| TX | 0.409 | 0.378 | 0.442 | 0.000 | 0.019 | -| NE | 0.408 | 0.377 | 0.438 | 0.000 | 0.018 | -| LA | 0.401 | 0.368 | 0.434 | 0.000 | 0.020 | -| IL | 0.600 | 0.569 | 0.629 | 1.000 | 0.018 | -| KY | 0.399 | 0.366 | 0.434 | 0.000 | 0.020 | -| DE | 0.602 | 0.565 | 0.638 | 1.000 | 0.021 | -| KS | 0.396 | 0.360 | 0.431 | 0.000 | 0.021 | -| MD | 0.607 | 0.573 | 0.639 | 1.000 | 0.019 | -| RI | 0.616 | 0.584 | 0.648 | 1.000 | 0.019 | -| AR | 0.384 | 0.352 | 0.418 | 0.000 | 0.020 | -| AL | 0.383 | 0.350 | 0.415 | 0.000 | 0.020 | -| NY | 0.620 | 0.590 | 0.649 | 1.000 | 0.017 | -| AK | 0.364 | 0.326 | 0.401 | 0.000 | 0.022 | -| VT | 0.660 | 0.628 | 0.693 | 1.000 | 0.019 | -| HI | 0.661 | 0.630 | 0.692 | 1.000 | 0.019 | -| ID | 0.332 | 0.301 | 0.364 | 0.000 | 0.019 | -| OK | 0.331 | 0.298 | 0.363 | 0.000 | 0.019 | -| WY | 0.313 | 0.281 | 0.347 | 0.000 | 0.020 | -| UT | 0.291 | 0.261 | 0.320 | 0.000 | 0.018 | -| DC | 0.903 | 0.884 | 0.920 | 1.000 | 0.010 | +| VA | 0.504 | 0.467 | 0.539 | 0.586 | 0.021 | +| CO | 0.505 | 0.471 | 0.541 | 0.617 | 0.021 | +| FL | 0.495 | 0.457 | 0.531 | 0.394 | 0.021 | +| OH | 0.510 | 0.474 | 0.545 | 0.704 | 0.021 | +| – | 0.510 | 0.489 | 0.533 | 0.773 | 0.014 | +| NH | 0.513 | 0.478 | 0.548 | 0.758 | 0.021 | +| IA | 0.514 | 0.480 | 0.550 | 0.785 | 0.021 | +| NC | 0.485 | 0.451 | 0.521 | 0.212 | 0.022 | +| NV | 0.516 | 0.478 | 0.554 | 0.797 | 0.022 | +| WI | 0.521 | 0.486 | 0.556 | 0.876 | 0.021 | +| PA | 0.528 | 0.492 | 0.563 | 0.937 | 0.021 | +| MN | 0.534 | 0.500 | 0.568 | 0.974 | 0.021 | +| MI | 0.537 | 0.502 | 0.572 | 0.979 | 0.021 | +| OR | 0.538 | 0.499 | 0.575 | 0.971 | 0.022 | +| MO | 0.460 | 0.424 | 0.496 | 0.015 | 0.022 | +| NM | 0.540 | 0.500 | 0.580 | 0.976 | 0.023 | +| IN | 0.455 | 0.420 | 0.492 | 0.006 | 0.022 | +| MT | 0.453 | 0.417 | 0.489 | 0.006 | 0.022 | +| GA | 0.452 | 0.413 | 0.492 | 0.009 | 0.024 | +| AZ | 0.452 | 0.414 | 0.491 | 0.009 | 0.023 | +| NJ | 0.556 | 0.518 | 0.593 | 0.999 | 0.022 | +| ME | 0.557 | 0.520 | 0.593 | 0.999 | 0.021 | +| WA | 0.561 | 0.526 | 0.596 | 1.000 | 0.021 | +| SC | 0.438 | 0.395 | 0.483 | 0.004 | 0.026 | +| CT | 0.567 | 0.529 | 0.603 | 1.000 | 0.022 | +| SD | 0.431 | 0.392 | 0.470 | 0.000 | 0.023 | +| ND | 0.422 | 0.385 | 0.461 | 0.000 | 0.023 | +| MS | 0.421 | 0.375 | 0.468 | 0.000 | 0.028 | +| TN | 0.419 | 0.383 | 0.458 | 0.000 | 0.023 | +| WV | 0.416 | 0.378 | 0.456 | 0.000 | 0.024 | +| CA | 0.585 | 0.549 | 0.622 | 1.000 | 0.022 | +| MA | 0.588 | 0.552 | 0.623 | 1.000 | 0.021 | +| TX | 0.409 | 0.372 | 0.448 | 0.000 | 0.023 | +| NE | 0.407 | 0.370 | 0.444 | 0.000 | 0.022 | +| IL | 0.599 | 0.562 | 0.635 | 1.000 | 0.021 | +| LA | 0.401 | 0.363 | 0.442 | 0.000 | 0.024 | +| DE | 0.602 | 0.555 | 0.646 | 1.000 | 0.026 | +| KY | 0.398 | 0.360 | 0.438 | 0.000 | 0.024 | +| KS | 0.395 | 0.355 | 0.435 | 0.000 | 0.024 | +| MD | 0.607 | 0.565 | 0.646 | 1.000 | 0.023 | +| RI | 0.616 | 0.575 | 0.654 | 1.000 | 0.023 | +| AR | 0.384 | 0.346 | 0.423 | 0.000 | 0.023 | +| AL | 0.383 | 0.342 | 0.423 | 0.000 | 0.024 | +| NY | 0.620 | 0.584 | 0.655 | 1.000 | 0.021 | +| AK | 0.363 | 0.320 | 0.407 | 0.000 | 0.026 | +| VT | 0.660 | 0.620 | 0.697 | 1.000 | 0.022 | +| HI | 0.661 | 0.620 | 0.699 | 1.000 | 0.023 | +| ID | 0.331 | 0.295 | 0.369 | 0.000 | 0.022 | +| OK | 0.330 | 0.293 | 0.369 | 0.000 | 0.023 | +| WY | 0.313 | 0.277 | 0.350 | 0.000 | 0.022 | +| UT | 0.291 | 0.257 | 0.326 | 0.000 | 0.021 | +| DC | 0.903 | 0.880 | 0.924 | 1.000 | 0.012 | ### 2016 @@ -257,7 +257,7 @@ Here is a graphical summary of the model’s performance in 2008, 2012 and | outlet | ev\_wtd\_brier | unwtd\_brier | states\_correct | | :------------------- | -------------: | -----------: | --------------: | -| economist (backtest) | 0.0746651 | 0.0520891 | 48 | +| economist (backtest) | 0.0725679 | 0.0508319 | 48 | | 538 polls-plus | 0.0928000 | 0.0664000 | 46 | | 538 polls-only | 0.0936000 | 0.0672000 | 46 | | princeton | 0.1169000 | 0.0744000 | 47 | @@ -272,71 +272,77 @@ Here is a graphical summary of the model’s performance in 2008, 2012 and ![](README_files/figure-gfm/unnamed-chunk-23-1.png) - ## [1] 0.0274812 + ## [1] 0.02724916 #### Predictions for each state | state | mean | low | high | prob | se | | :---- | ----: | ----: | ----: | ----: | ----: | -| FL | 0.497 | 0.458 | 0.536 | 0.436 | 0.023 | -| NC | 0.493 | 0.453 | 0.534 | 0.367 | 0.024 | -| NV | 0.509 | 0.468 | 0.549 | 0.667 | 0.023 | -| – | 0.514 | 0.483 | 0.543 | 0.831 | 0.017 | -| PA | 0.514 | 0.475 | 0.554 | 0.757 | 0.024 | -| NH | 0.514 | 0.474 | 0.555 | 0.746 | 0.024 | -| OH | 0.485 | 0.446 | 0.524 | 0.233 | 0.023 | -| CO | 0.516 | 0.476 | 0.556 | 0.773 | 0.024 | -| MI | 0.519 | 0.479 | 0.558 | 0.821 | 0.023 | -| WI | 0.521 | 0.481 | 0.559 | 0.860 | 0.022 | -| IA | 0.479 | 0.438 | 0.518 | 0.147 | 0.023 | -| VA | 0.523 | 0.482 | 0.564 | 0.862 | 0.025 | -| MN | 0.528 | 0.488 | 0.568 | 0.919 | 0.024 | -| AZ | 0.471 | 0.431 | 0.511 | 0.081 | 0.024 | -| GA | 0.470 | 0.431 | 0.511 | 0.076 | 0.024 | -| NM | 0.533 | 0.492 | 0.576 | 0.941 | 0.025 | -| ME | 0.542 | 0.501 | 0.583 | 0.977 | 0.024 | +| FL | 0.496 | 0.457 | 0.536 | 0.435 | 0.024 | +| NV | 0.508 | 0.467 | 0.548 | 0.652 | 0.024 | +| NC | 0.492 | 0.451 | 0.532 | 0.340 | 0.024 | +| – | 0.512 | 0.485 | 0.540 | 0.791 | 0.017 | +| NH | 0.514 | 0.475 | 0.554 | 0.736 | 0.024 | +| PA | 0.514 | 0.475 | 0.553 | 0.745 | 0.024 | +| CO | 0.516 | 0.476 | 0.555 | 0.774 | 0.023 | +| OH | 0.484 | 0.445 | 0.523 | 0.217 | 0.023 | +| MI | 0.518 | 0.479 | 0.558 | 0.816 | 0.024 | +| WI | 0.521 | 0.483 | 0.561 | 0.844 | 0.024 | +| IA | 0.478 | 0.439 | 0.517 | 0.148 | 0.023 | +| VA | 0.523 | 0.482 | 0.562 | 0.863 | 0.023 | +| MN | 0.527 | 0.489 | 0.568 | 0.909 | 0.024 | +| AZ | 0.471 | 0.430 | 0.510 | 0.077 | 0.024 | +| GA | 0.470 | 0.430 | 0.508 | 0.069 | 0.023 | +| NM | 0.532 | 0.489 | 0.575 | 0.926 | 0.025 | +| ME | 0.542 | 0.503 | 0.582 | 0.985 | 0.024 | | SC | 0.452 | 0.411 | 0.493 | 0.011 | 0.024 | -| OR | 0.549 | 0.509 | 0.589 | 0.990 | 0.024 | -| TX | 0.443 | 0.403 | 0.485 | 0.002 | 0.025 | -| MO | 0.440 | 0.402 | 0.480 | 0.001 | 0.024 | -| MS | 0.437 | 0.396 | 0.478 | 0.001 | 0.025 | -| CT | 0.565 | 0.524 | 0.606 | 0.999 | 0.025 | -| WA | 0.568 | 0.528 | 0.608 | 1.000 | 0.024 | -| DE | 0.569 | 0.527 | 0.612 | 1.000 | 0.026 | +| OR | 0.549 | 0.509 | 0.590 | 0.992 | 0.025 | +| TX | 0.443 | 0.403 | 0.483 | 0.002 | 0.024 | +| MO | 0.439 | 0.401 | 0.478 | 0.001 | 0.023 | +| MS | 0.436 | 0.397 | 0.476 | 0.001 | 0.024 | +| CT | 0.565 | 0.525 | 0.607 | 1.000 | 0.024 | +| WA | 0.567 | 0.528 | 0.607 | 1.000 | 0.024 | +| DE | 0.568 | 0.525 | 0.609 | 1.000 | 0.025 | | AK | 0.425 | 0.384 | 0.468 | 0.000 | 0.026 | -| NJ | 0.578 | 0.538 | 0.619 | 1.000 | 0.025 | -| IN | 0.419 | 0.379 | 0.458 | 0.000 | 0.023 | -| IL | 0.584 | 0.543 | 0.625 | 1.000 | 0.025 | -| LA | 0.411 | 0.373 | 0.451 | 0.000 | 0.024 | -| MT | 0.406 | 0.367 | 0.445 | 0.000 | 0.023 | -| RI | 0.596 | 0.553 | 0.637 | 1.000 | 0.025 | -| TN | 0.404 | 0.365 | 0.443 | 0.000 | 0.023 | -| KS | 0.404 | 0.365 | 0.442 | 0.000 | 0.023 | -| SD | 0.399 | 0.360 | 0.437 | 0.000 | 0.023 | -| ND | 0.390 | 0.351 | 0.429 | 0.000 | 0.023 | -| NY | 0.611 | 0.570 | 0.651 | 1.000 | 0.024 | -| NE | 0.388 | 0.351 | 0.427 | 0.000 | 0.023 | -| AL | 0.385 | 0.346 | 0.425 | 0.000 | 0.024 | -| AR | 0.382 | 0.344 | 0.423 | 0.000 | 0.024 | -| CA | 0.621 | 0.582 | 0.658 | 1.000 | 0.022 | -| UT | 0.375 | 0.338 | 0.414 | 0.000 | 0.023 | -| KY | 0.374 | 0.337 | 0.412 | 0.000 | 0.023 | -| MA | 0.629 | 0.590 | 0.668 | 1.000 | 0.023 | -| MD | 0.639 | 0.598 | 0.681 | 1.000 | 0.025 | -| WV | 0.353 | 0.316 | 0.391 | 0.000 | 0.023 | -| ID | 0.349 | 0.312 | 0.386 | 0.000 | 0.023 | -| OK | 0.342 | 0.305 | 0.381 | 0.000 | 0.023 | -| VT | 0.659 | 0.618 | 0.696 | 1.000 | 0.022 | -| HI | 0.662 | 0.622 | 0.700 | 1.000 | 0.023 | +| NJ | 0.578 | 0.537 | 0.619 | 1.000 | 0.024 | +| IN | 0.419 | 0.380 | 0.458 | 0.000 | 0.023 | +| IL | 0.583 | 0.543 | 0.623 | 0.999 | 0.024 | +| LA | 0.410 | 0.371 | 0.449 | 0.000 | 0.024 | +| MT | 0.406 | 0.368 | 0.446 | 0.000 | 0.024 | +| RI | 0.595 | 0.555 | 0.636 | 1.000 | 0.024 | +| KS | 0.403 | 0.368 | 0.441 | 0.000 | 0.022 | +| TN | 0.403 | 0.365 | 0.442 | 0.000 | 0.023 | +| SD | 0.398 | 0.361 | 0.438 | 0.000 | 0.023 | +| NY | 0.611 | 0.571 | 0.649 | 1.000 | 0.023 | +| ND | 0.389 | 0.352 | 0.429 | 0.000 | 0.024 | +| NE | 0.388 | 0.351 | 0.428 | 0.000 | 0.023 | +| AL | 0.384 | 0.346 | 0.423 | 0.000 | 0.023 | +| AR | 0.382 | 0.345 | 0.421 | 0.000 | 0.023 | +| CA | 0.620 | 0.581 | 0.658 | 1.000 | 0.023 | +| UT | 0.375 | 0.337 | 0.412 | 0.000 | 0.022 | +| KY | 0.373 | 0.336 | 0.411 | 0.000 | 0.023 | +| MA | 0.629 | 0.591 | 0.668 | 1.000 | 0.023 | +| MD | 0.639 | 0.598 | 0.677 | 1.000 | 0.023 | +| WV | 0.353 | 0.316 | 0.390 | 0.000 | 0.022 | +| ID | 0.349 | 0.313 | 0.386 | 0.000 | 0.022 | +| OK | 0.342 | 0.305 | 0.378 | 0.000 | 0.022 | +| VT | 0.658 | 0.619 | 0.696 | 1.000 | 0.023 | +| HI | 0.661 | 0.620 | 0.699 | 1.000 | 0.023 | | WY | 0.289 | 0.255 | 0.324 | 0.000 | 0.021 | -| DC | 0.908 | 0.886 | 0.928 | 1.000 | 0.012 | +| DC | 0.908 | 0.885 | 0.928 | 1.000 | 0.012 | ## Cumulative charts -### Calibration plot +### Probability calibration plot ![](README_files/figure-gfm/unnamed-chunk-25-1.png) +### Confidence interval coverage + +![](README_files/figure-gfm/unnamed-chunk-27-1.png) + +![](README_files/figure-gfm/unnamed-chunk-28-1.png) + # Licence This software is published by *[The diff --git a/README_files/figure-gfm/unnamed-chunk-10-1.png b/README_files/figure-gfm/unnamed-chunk-10-1.png index db4f67e..d6564d6 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-10-1.png and b/README_files/figure-gfm/unnamed-chunk-10-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-11-1.png b/README_files/figure-gfm/unnamed-chunk-11-1.png index b50bccd..55f3df9 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-11-1.png and b/README_files/figure-gfm/unnamed-chunk-11-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-12-1.png b/README_files/figure-gfm/unnamed-chunk-12-1.png index 7e35f62..d5cebcf 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-12-1.png and b/README_files/figure-gfm/unnamed-chunk-12-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-13-1.png b/README_files/figure-gfm/unnamed-chunk-13-1.png index 53d5cf4..a0e6f12 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-13-1.png and b/README_files/figure-gfm/unnamed-chunk-13-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-14-1.png b/README_files/figure-gfm/unnamed-chunk-14-1.png index 03312da..0d9ace8 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-14-1.png and b/README_files/figure-gfm/unnamed-chunk-14-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-15-1.png b/README_files/figure-gfm/unnamed-chunk-15-1.png index 501232a..4116a56 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-15-1.png and b/README_files/figure-gfm/unnamed-chunk-15-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-18-1.png b/README_files/figure-gfm/unnamed-chunk-18-1.png index 9ae092e..2d61ce1 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-18-1.png and b/README_files/figure-gfm/unnamed-chunk-18-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-19-1.png b/README_files/figure-gfm/unnamed-chunk-19-1.png index ca8206a..46dca56 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-19-1.png and b/README_files/figure-gfm/unnamed-chunk-19-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-2-1.png b/README_files/figure-gfm/unnamed-chunk-2-1.png index 4befea8..c41b081 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-2-1.png and b/README_files/figure-gfm/unnamed-chunk-2-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-20-1.png b/README_files/figure-gfm/unnamed-chunk-20-1.png index 8b64318..f454264 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-20-1.png and b/README_files/figure-gfm/unnamed-chunk-20-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-21-1.png b/README_files/figure-gfm/unnamed-chunk-21-1.png index d0615da..f11c4af 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-21-1.png and b/README_files/figure-gfm/unnamed-chunk-21-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-22-1.png b/README_files/figure-gfm/unnamed-chunk-22-1.png index caecf44..63a2b01 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-22-1.png and b/README_files/figure-gfm/unnamed-chunk-22-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-23-1.png b/README_files/figure-gfm/unnamed-chunk-23-1.png index 10eceb6..e5c628f 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-23-1.png and b/README_files/figure-gfm/unnamed-chunk-23-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-25-1.png b/README_files/figure-gfm/unnamed-chunk-25-1.png index f83dfff..24b81da 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-25-1.png and b/README_files/figure-gfm/unnamed-chunk-25-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-27-1.png b/README_files/figure-gfm/unnamed-chunk-27-1.png new file mode 100644 index 0000000..939c7e2 Binary files /dev/null and b/README_files/figure-gfm/unnamed-chunk-27-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-28-1.png b/README_files/figure-gfm/unnamed-chunk-28-1.png new file mode 100644 index 0000000..1fa90f2 Binary files /dev/null and b/README_files/figure-gfm/unnamed-chunk-28-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-3-1.png b/README_files/figure-gfm/unnamed-chunk-3-1.png index 6ea0e42..dc14c36 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-3-1.png and b/README_files/figure-gfm/unnamed-chunk-3-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-4-1.png b/README_files/figure-gfm/unnamed-chunk-4-1.png index 3c81595..bc575c4 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-4-1.png and b/README_files/figure-gfm/unnamed-chunk-4-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-5-1.png b/README_files/figure-gfm/unnamed-chunk-5-1.png index fef1aad..f3639df 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-5-1.png and b/README_files/figure-gfm/unnamed-chunk-5-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-6-1.png b/README_files/figure-gfm/unnamed-chunk-6-1.png index 357b519..816b86a 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-6-1.png and b/README_files/figure-gfm/unnamed-chunk-6-1.png differ diff --git a/README_files/figure-gfm/unnamed-chunk-7-1.png b/README_files/figure-gfm/unnamed-chunk-7-1.png index b8a3fc0..408d92b 100644 Binary files a/README_files/figure-gfm/unnamed-chunk-7-1.png and b/README_files/figure-gfm/unnamed-chunk-7-1.png differ diff --git a/model_summary.Rmd b/model_summary.Rmd index 06ab8e0..db718af 100644 --- a/model_summary.Rmd +++ b/model_summary.Rmd @@ -19,7 +19,8 @@ library(ggrepel) library(DT) # final model version -# source('scripts/model/final_2016.R');source('scripts/model/final_2012.R');source('scripts/model/final_2008.R');rmarkdown::render('model_summary.Rmd',output_file = 'model_reports/v4_cov_error_rewrite');beepr::beep(2); +# source('scripts/model/final_2016.R');source('scripts/model/final_2012.R');source('scripts/model/final_2008.R');beepr::beep(2); +# rmarkdown::render('model_summary.Rmd',output_file = 'model_reports/v4_cov_error_rewrite') ``` ## Model performance {.tabset .tabset-fade} diff --git a/scripts/model/final_2008.R b/scripts/model/final_2008.R index 7ed89fe..f20acf2 100644 --- a/scripts/model/final_2008.R +++ b/scripts/model/final_2008.R @@ -64,7 +64,7 @@ check_cov_matrix <- function(mat,wt=state_weights){ ## Master variables RUN_DATE <- ymd("2008-11-03") -#RUN_DATE <- ymd("2008-08-04") +#RUN_DATE <- ymd("2008-08-11") election_day <- ymd("2008-11-03") start_date <- as.Date("2008-03-01") # Keeping all polls after March 1, 2008 @@ -433,7 +433,7 @@ names(mu_b_prior) == names(prior_diff_score) # correct order? national_mu_prior <- weighted.mean(inv.logit(mu_b_prior), state_weights) -cat(sprintf('Prior Clinton two-party vote is %s\nWith a national sd of %s\n', +cat(sprintf('Prior Obama two-party vote is %s\nWith a national sd of %s\n', round(national_mu_prior,3),round(mu_b_T_scale,3))) # The model uses national polls to complement state polls when estimating the national term mu_a. diff --git a/scripts/model/final_2012.R b/scripts/model/final_2012.R index ca0a77f..b13ba36 100644 --- a/scripts/model/final_2012.R +++ b/scripts/model/final_2012.R @@ -64,7 +64,7 @@ check_cov_matrix <- function(mat,wt=state_weights){ ## Master variables RUN_DATE <- ymd("2012-11-06") -#RUN_DATE <- ymd("2012-08-04") +#RUN_DATE <- ymd("2012-08-11") election_day <- ymd("2012-11-06") start_date <- as.Date("2012-03-01") # Keeping all polls after March 1, 2012 diff --git a/scripts/model/final_2016.R b/scripts/model/final_2016.R index 66f680d..e574642 100644 --- a/scripts/model/final_2016.R +++ b/scripts/model/final_2016.R @@ -64,7 +64,7 @@ check_cov_matrix <- function(mat,wt=state_weights){ ## Master variables RUN_DATE <- ymd("2016-11-08") -#RUN_DATE <- ymd("2016-08-04") +#RUN_DATE <- ymd("2016-10-19") election_day <- ymd("2016-11-08") start_date <- as.Date("2016-03-01") # Keeping all polls after March 1, 2016 @@ -766,6 +766,25 @@ ex_states <- c('IA','FL','OH','WI','MI','PA','AZ','NC','NH','NV','GA','MN') pct_clinton %>% filter(t == RUN_DATE,state %in% c(ex_states,'--')) %>% mutate(se = (high - mean)/1.96) %>% dplyr::select(-t) %>% print pct_clinton %>% filter(t == election_day,state %in% c(ex_states,'--')) %>% mutate(se = (high - mean)/1.96) %>% dplyr::select(-t) %>% print +#pct_clinton %>% filter(t == election_day) %>% select(state, clinton=mean) %>% write_csv('~/Desktop/today_2016.csv') + +mu_b_t_results_plt <- rbind(mu_b_T_prior_draws, mu_b_T_posterior_draws) %>% + bind_rows( + politicaldata::pres_results %>% filter(year == 2016) %>% + mutate(mean = dem/(dem+rep)) %>% + select(state,mean) %>% + mutate(type = 'actual') + ) %>% + arrange(mean) %>% + filter(state != 'DC') %>% + ggplot(.) + + geom_point(aes(y = mean, x = reorder(state, mean), color = type), position = position_dodge(width = 0.5)) + + geom_errorbar(aes(ymin = low, ymax = high, x = state, color = type), width = 0, position = position_dodge(width = 0.5)) + + coord_flip() + + theme_bw() +mu_b_t_results_plt + + map.gg <- urbnmapr::states %>% left_join(pct_clinton %>% filter(t == max(t)) %>% select(state_abbv=state,prob)) %>% diff --git a/scripts/model/poll_model_2020 b/scripts/model/poll_model_2020 index 5bec80b..9c754f2 100755 Binary files a/scripts/model/poll_model_2020 and b/scripts/model/poll_model_2020 differ diff --git a/scripts/model/poll_model_2020.stan b/scripts/model/poll_model_2020.stan index 8d16eb7..4cb9c1a 100644 --- a/scripts/model/poll_model_2020.stan +++ b/scripts/model/poll_model_2020.stan @@ -65,7 +65,7 @@ parameters { vector[N_national_polls] raw_measure_noise_national; vector[N_state_polls] raw_measure_noise_state; vector[S] raw_polling_bias; - real mu_b_T_model_estimation_error; + // real mu_b_T_model_estimation_error; } transformed parameters { //*** parameters @@ -114,7 +114,7 @@ transformed parameters { model { //*** priors - raw_mu_b_T ~ std_normal(); + raw_mu_b_T ~ std_normal(); // student_t(4,0,1); // std_normal(); //mu_b_T_model_estimation_error ~ scaled_inv_chi_square(7, 1); to_vector(raw_mu_b) ~ std_normal(); raw_mu_c ~ std_normal(); @@ -125,7 +125,7 @@ model { raw_e_bias ~ std_normal(); raw_measure_noise_national ~ std_normal(); raw_measure_noise_state ~ std_normal(); - raw_polling_bias ~ std_normal(); + raw_polling_bias ~ std_normal(); // student_t(4,0,1); // std_normal(); //*** likelihood n_democrat_state ~ binomial_logit(n_two_share_state, logit_pi_democrat_state); n_democrat_national ~ binomial_logit(n_two_share_national, logit_pi_democrat_national); diff --git a/scripts/model/poll_model_2020_no_mode_adjustment b/scripts/model/poll_model_2020_no_mode_adjustment index 3f00934..3d89e18 100755 Binary files a/scripts/model/poll_model_2020_no_mode_adjustment and b/scripts/model/poll_model_2020_no_mode_adjustment differ diff --git a/scripts/model/poll_model_2020_no_mode_adjustment.stan b/scripts/model/poll_model_2020_no_mode_adjustment.stan index 7a8c6a5..31d7230 100644 --- a/scripts/model/poll_model_2020_no_mode_adjustment.stan +++ b/scripts/model/poll_model_2020_no_mode_adjustment.stan @@ -65,7 +65,7 @@ parameters { vector[N_national_polls] raw_measure_noise_national; vector[N_state_polls] raw_measure_noise_state; vector[S] raw_polling_bias; - real mu_b_T_model_estimation_error; + // real mu_b_T_model_estimation_error; } transformed parameters { //*** parameters @@ -114,7 +114,7 @@ transformed parameters { model { //*** priors - raw_mu_b_T ~ std_normal(); + raw_mu_b_T ~ std_normal(); // student_t(4,0,1); // std_normal(); //mu_b_T_model_estimation_error ~ scaled_inv_chi_square(7, 1); to_vector(raw_mu_b) ~ std_normal(); raw_mu_c ~ std_normal(); @@ -125,7 +125,7 @@ model { //raw_e_bias ~ std_normal(); raw_measure_noise_national ~ std_normal(); raw_measure_noise_state ~ std_normal(); - raw_polling_bias ~ std_normal(); + raw_polling_bias ~ std_normal(); // student_t(4,0,1); // std_normal(); //*** likelihood n_democrat_state ~ binomial_logit(n_two_share_state, logit_pi_democrat_state); n_democrat_national ~ binomial_logit(n_two_share_national, logit_pi_democrat_national);