Last update on Thursday June 11, 2020 at 07:37 AM EDT
Code for a dynamic multilevel Bayesian model to predict US presidential elections. Written in R and Stan.
Improving on Pierre Kremp’s implementation of Drew Linzer’s dynamic linear model for election forecasting (Linzer 2013), we (1) add corrections for partisan non-response, survey mode and survey population; (2) use informative state-level priors that update throughout the election year; and (3) specify empirical state-level correlations from political and demographic variables.
You can see the model’s predictions for 2020 here and read how it works here.
In terms of useful files, you should pay attention to the 3 scripts for
the 2008, 2012 and 2016 US presidential elections are located in the
scripts/model
directory. There are three R scripts that import data,
run models and parse results:
final_model_2008.R
final_model_2012.R
final_model_2016.R
And there are 3 different Stan scripts that will run different versions of our polling aggregate and election forecasting model:
poll_model_2020.stan
- the final model we use for the 2020 presidential electionpoll_model_2020_no_partisan_correction.stan
- a model that removes the correction for partisan non-response bias in the pollspoll_model_2020_no_mode_adjustment.stan
- a model that further removes the adjustments for the mode in which a survey is conducted (live phone, online, other) and its population (adult, likely voter, registered voter)
The model diagnostics displayed below are all results of the
poll_model_2020.stan
script.
Here is a graphical summary of the model’s performance in 2008, 2012 and 2016.
outlet | ev_wtd_brier | unwtd_brier | states_correct |
---|---|---|---|
economist (backtest) | 0.0333707 | 0.0302863 | 49 |
## [1] 0.02242826
state | mean | low | high | prob | se |
---|---|---|---|---|---|
NC | 0.503 | 0.454 | 0.555 | 0.527 | 0.031 |
MO | 0.510 | 0.461 | 0.561 | 0.635 | 0.030 |
IN | 0.486 | 0.434 | 0.537 | 0.330 | 0.031 |
MT | 0.483 | 0.427 | 0.534 | 0.297 | 0.031 |
FL | 0.519 | 0.472 | 0.569 | 0.734 | 0.030 |
GA | 0.476 | 0.425 | 0.528 | 0.201 | 0.031 |
VA | 0.527 | 0.479 | 0.574 | 0.818 | 0.028 |
OH | 0.527 | 0.478 | 0.576 | 0.818 | 0.029 |
AR | 0.472 | 0.423 | 0.524 | 0.190 | 0.031 |
AZ | 0.470 | 0.418 | 0.523 | 0.172 | 0.032 |
WV | 0.469 | 0.420 | 0.520 | 0.154 | 0.031 |
NV | 0.534 | 0.483 | 0.587 | 0.859 | 0.031 |
MS | 0.465 | 0.412 | 0.522 | 0.134 | 0.034 |
CO | 0.535 | 0.485 | 0.583 | 0.878 | 0.028 |
ND | 0.463 | 0.403 | 0.520 | 0.150 | 0.034 |
LA | 0.461 | 0.412 | 0.514 | 0.102 | 0.031 |
TX | 0.458 | 0.407 | 0.508 | 0.092 | 0.030 |
– | 0.544 | 0.513 | 0.575 | 0.996 | 0.018 |
SC | 0.455 | 0.408 | 0.508 | 0.066 | 0.032 |
SD | 0.453 | 0.401 | 0.506 | 0.074 | 0.031 |
NH | 0.552 | 0.503 | 0.602 | 0.956 | 0.030 |
PA | 0.557 | 0.508 | 0.606 | 0.966 | 0.029 |
WI | 0.559 | 0.507 | 0.606 | 0.974 | 0.028 |
KY | 0.440 | 0.391 | 0.491 | 0.030 | 0.031 |
NM | 0.560 | 0.502 | 0.614 | 0.951 | 0.032 |
MN | 0.561 | 0.512 | 0.611 | 0.976 | 0.030 |
TN | 0.438 | 0.388 | 0.491 | 0.030 | 0.032 |
IA | 0.564 | 0.512 | 0.612 | 0.978 | 0.029 |
MI | 0.565 | 0.516 | 0.612 | 0.981 | 0.028 |
AK | 0.427 | 0.375 | 0.479 | 0.007 | 0.031 |
OR | 0.575 | 0.524 | 0.624 | 0.993 | 0.030 |
KS | 0.424 | 0.375 | 0.475 | 0.009 | 0.030 |
ME | 0.587 | 0.537 | 0.635 | 0.997 | 0.029 |
WA | 0.587 | 0.536 | 0.636 | 0.998 | 0.030 |
NE | 0.412 | 0.363 | 0.462 | 0.001 | 0.029 |
AL | 0.407 | 0.359 | 0.460 | 0.009 | 0.031 |
NJ | 0.594 | 0.543 | 0.644 | 0.999 | 0.030 |
DE | 0.619 | 0.569 | 0.666 | 0.999 | 0.028 |
CA | 0.621 | 0.569 | 0.669 | 1.000 | 0.028 |
OK | 0.379 | 0.330 | 0.432 | 0.001 | 0.031 |
CT | 0.623 | 0.576 | 0.670 | 1.000 | 0.028 |
WY | 0.375 | 0.325 | 0.426 | 0.000 | 0.031 |
MD | 0.629 | 0.568 | 0.687 | 0.999 | 0.034 |
IL | 0.633 | 0.586 | 0.679 | 1.000 | 0.027 |
MA | 0.636 | 0.586 | 0.686 | 1.000 | 0.030 |
ID | 0.352 | 0.301 | 0.404 | 0.000 | 0.031 |
NY | 0.649 | 0.599 | 0.699 | 1.000 | 0.029 |
UT | 0.344 | 0.297 | 0.393 | 0.000 | 0.029 |
VT | 0.660 | 0.611 | 0.707 | 1.000 | 0.028 |
RI | 0.669 | 0.622 | 0.716 | 1.000 | 0.028 |
HI | 0.677 | 0.618 | 0.727 | 1.000 | 0.030 |
DC | 0.908 | 0.880 | 0.934 | 1.000 | 0.015 |
outlet | ev_wtd_brier | unwtd_brier | states_correct |
---|---|---|---|
Linzer | NA | 0.003800 | NA |
Wang/Ferguson | NA | 0.007610 | NA |
Silver/538 | NA | 0.009110 | NA |
Jackman/Pollster | NA | 0.009710 | NA |
Desart/Holbrook | NA | 0.016050 | NA |
economist (backtest) | 0.0327484 | 0.021624 | 50 |
Intrade | NA | 0.028120 | NA |
Enten/Margin of Error | NA | 0.050750 | NA |
## [1] 0.02187645
state | mean | low | high | prob | se |
---|---|---|---|---|---|
FL | 0.497 | 0.450 | 0.542 | 0.468 | 0.027 |
VA | 0.506 | 0.461 | 0.550 | 0.591 | 0.026 |
CO | 0.510 | 0.465 | 0.557 | 0.645 | 0.028 |
OH | 0.511 | 0.465 | 0.558 | 0.656 | 0.028 |
– | 0.512 | 0.480 | 0.542 | 0.773 | 0.018 |
NC | 0.488 | 0.442 | 0.534 | 0.343 | 0.027 |
NH | 0.517 | 0.470 | 0.564 | 0.726 | 0.028 |
IA | 0.518 | 0.473 | 0.561 | 0.749 | 0.026 |
NV | 0.521 | 0.474 | 0.568 | 0.787 | 0.028 |
WI | 0.524 | 0.478 | 0.569 | 0.805 | 0.027 |
PA | 0.531 | 0.485 | 0.577 | 0.862 | 0.027 |
MO | 0.463 | 0.416 | 0.510 | 0.095 | 0.028 |
MN | 0.538 | 0.493 | 0.582 | 0.916 | 0.027 |
OR | 0.539 | 0.492 | 0.585 | 0.919 | 0.027 |
AZ | 0.459 | 0.412 | 0.504 | 0.072 | 0.027 |
MI | 0.541 | 0.495 | 0.586 | 0.928 | 0.027 |
IN | 0.458 | 0.413 | 0.504 | 0.062 | 0.027 |
NM | 0.543 | 0.496 | 0.589 | 0.936 | 0.027 |
MT | 0.455 | 0.409 | 0.502 | 0.056 | 0.028 |
GA | 0.453 | 0.405 | 0.499 | 0.049 | 0.028 |
SC | 0.441 | 0.386 | 0.496 | 0.043 | 0.033 |
NJ | 0.559 | 0.510 | 0.606 | 0.975 | 0.028 |
SD | 0.439 | 0.392 | 0.487 | 0.016 | 0.029 |
ME | 0.561 | 0.515 | 0.606 | 0.982 | 0.027 |
WA | 0.565 | 0.519 | 0.612 | 0.987 | 0.028 |
CT | 0.571 | 0.523 | 0.619 | 0.993 | 0.029 |
ND | 0.425 | 0.380 | 0.469 | 0.005 | 0.026 |
TN | 0.425 | 0.378 | 0.472 | 0.004 | 0.028 |
NE | 0.422 | 0.375 | 0.470 | 0.003 | 0.028 |
WV | 0.418 | 0.368 | 0.469 | 0.002 | 0.031 |
MS | 0.415 | 0.351 | 0.478 | 0.015 | 0.037 |
TX | 0.415 | 0.373 | 0.461 | 0.002 | 0.027 |
CA | 0.590 | 0.547 | 0.632 | 0.998 | 0.025 |
MA | 0.590 | 0.541 | 0.637 | 0.996 | 0.028 |
LA | 0.405 | 0.359 | 0.452 | 0.002 | 0.028 |
KY | 0.405 | 0.354 | 0.455 | 0.000 | 0.030 |
KS | 0.399 | 0.342 | 0.460 | 0.005 | 0.036 |
DE | 0.604 | 0.549 | 0.659 | 0.997 | 0.033 |
IL | 0.604 | 0.556 | 0.649 | 1.000 | 0.027 |
MD | 0.607 | 0.559 | 0.653 | 1.000 | 0.027 |
AL | 0.389 | 0.342 | 0.437 | 0.000 | 0.028 |
AR | 0.378 | 0.333 | 0.425 | 0.000 | 0.028 |
RI | 0.623 | 0.573 | 0.672 | 1.000 | 0.029 |
NY | 0.623 | 0.576 | 0.668 | 1.000 | 0.027 |
AK | 0.366 | 0.306 | 0.428 | 0.001 | 0.037 |
OK | 0.339 | 0.290 | 0.393 | 0.000 | 0.032 |
HI | 0.662 | 0.615 | 0.707 | 1.000 | 0.027 |
VT | 0.674 | 0.622 | 0.721 | 1.000 | 0.028 |
ID | 0.325 | 0.276 | 0.375 | 0.000 | 0.030 |
WY | 0.317 | 0.250 | 0.391 | 0.000 | 0.044 |
UT | 0.275 | 0.231 | 0.323 | 0.000 | 0.029 |
DC | 0.899 | 0.848 | 0.938 | 1.000 | 0.023 |
outlet | ev_wtd_brier | unwtd_brier | states_correct |
---|---|---|---|
economist (backtest) | 0.0864787 | 0.0602528 | 47 |
538 polls-plus | 0.0928000 | 0.0664000 | 46 |
538 polls-only | 0.0936000 | 0.0672000 | 46 |
princeton | 0.1169000 | 0.0744000 | 47 |
nyt upshot | 0.1208000 | 0.0801000 | 46 |
kremp/slate | 0.1210000 | 0.0766000 | 46 |
pollsavvy | 0.1219000 | 0.0794000 | 46 |
predictwise markets | 0.1272000 | 0.0767000 | 46 |
predictwise overall | 0.1276000 | 0.0783000 | 46 |
desart and holbrook | 0.1279000 | 0.0825000 | 44 |
daily kos | 0.1439000 | 0.0864000 | 46 |
huffpost | 0.1505000 | 0.0892000 | 46 |
## [1] 0.03073332
state | mean | low | high | prob | se |
---|---|---|---|---|---|
FL | 0.502 | 0.457 | 0.547 | 0.509 | 0.027 |
NC | 0.496 | 0.448 | 0.542 | 0.455 | 0.027 |
OH | 0.492 | 0.447 | 0.537 | 0.386 | 0.027 |
IA | 0.491 | 0.443 | 0.541 | 0.384 | 0.030 |
NV | 0.514 | 0.465 | 0.562 | 0.676 | 0.029 |
– | 0.517 | 0.487 | 0.547 | 0.846 | 0.018 |
PA | 0.519 | 0.474 | 0.566 | 0.747 | 0.028 |
CO | 0.520 | 0.475 | 0.566 | 0.763 | 0.028 |
NH | 0.521 | 0.475 | 0.567 | 0.764 | 0.028 |
AZ | 0.475 | 0.425 | 0.525 | 0.204 | 0.029 |
MI | 0.527 | 0.482 | 0.572 | 0.844 | 0.027 |
WI | 0.528 | 0.485 | 0.574 | 0.846 | 0.027 |
VA | 0.528 | 0.483 | 0.572 | 0.857 | 0.026 |
GA | 0.471 | 0.426 | 0.518 | 0.148 | 0.028 |
MN | 0.534 | 0.486 | 0.580 | 0.884 | 0.027 |
NM | 0.540 | 0.495 | 0.587 | 0.918 | 0.028 |
SC | 0.456 | 0.410 | 0.501 | 0.052 | 0.027 |
ME | 0.551 | 0.506 | 0.597 | 0.968 | 0.027 |
MO | 0.446 | 0.401 | 0.492 | 0.025 | 0.028 |
OR | 0.555 | 0.509 | 0.600 | 0.976 | 0.027 |
TX | 0.442 | 0.397 | 0.488 | 0.018 | 0.027 |
MS | 0.441 | 0.394 | 0.488 | 0.021 | 0.028 |
WA | 0.573 | 0.528 | 0.619 | 0.996 | 0.027 |
CT | 0.573 | 0.526 | 0.616 | 0.995 | 0.026 |
AK | 0.426 | 0.380 | 0.470 | 0.003 | 0.026 |
IN | 0.426 | 0.379 | 0.472 | 0.002 | 0.028 |
DE | 0.576 | 0.532 | 0.621 | 0.997 | 0.027 |
NJ | 0.580 | 0.532 | 0.626 | 0.997 | 0.028 |
MT | 0.413 | 0.368 | 0.460 | 0.001 | 0.028 |
LA | 0.412 | 0.368 | 0.456 | 0.002 | 0.027 |
KS | 0.412 | 0.362 | 0.460 | 0.001 | 0.029 |
IL | 0.589 | 0.542 | 0.635 | 0.999 | 0.028 |
TN | 0.407 | 0.363 | 0.453 | 0.000 | 0.027 |
RI | 0.594 | 0.545 | 0.643 | 0.999 | 0.029 |
SD | 0.405 | 0.359 | 0.452 | 0.000 | 0.028 |
UT | 0.395 | 0.325 | 0.452 | 0.000 | 0.034 |
NE | 0.393 | 0.347 | 0.440 | 0.000 | 0.028 |
NY | 0.610 | 0.566 | 0.655 | 1.000 | 0.027 |
AR | 0.390 | 0.345 | 0.437 | 0.000 | 0.028 |
AL | 0.389 | 0.344 | 0.434 | 0.000 | 0.027 |
ND | 0.387 | 0.339 | 0.435 | 0.000 | 0.029 |
KY | 0.378 | 0.334 | 0.422 | 0.000 | 0.026 |
CA | 0.624 | 0.581 | 0.666 | 1.000 | 0.025 |
MA | 0.631 | 0.585 | 0.676 | 1.000 | 0.027 |
MD | 0.638 | 0.592 | 0.682 | 1.000 | 0.026 |
ID | 0.359 | 0.317 | 0.407 | 0.000 | 0.028 |
WV | 0.358 | 0.316 | 0.402 | 0.000 | 0.026 |
HI | 0.653 | 0.605 | 0.700 | 1.000 | 0.028 |
OK | 0.346 | 0.302 | 0.391 | 0.000 | 0.027 |
VT | 0.676 | 0.629 | 0.719 | 1.000 | 0.026 |
WY | 0.290 | 0.251 | 0.335 | 0.000 | 0.026 |
DC | 0.906 | 0.870 | 0.936 | 1.000 | 0.018 |
This software is published by The Economist under the MIT licence. The data generated by The Economist are available under the Creative Commons Attribution 4.0 International License.
The licences include only the data and the software authored by The Economist, and do not cover any Economist content or third-party data or content made available using the software. More information about licensing, syndication and the copyright of Economist content can be found here.