Regardless of your political leanings, if you're a data nerd, Election Night can be a very interesting time. Here are some R resources that might come in handy tonight.
Compare actual returns with forecasts. Sociologist Pierre-Antoine Kremp has implemented an election forecast model by statistician Drew Linzer in R. Now that Election Day is here, you might like to see how real results match up with that model as returns come in.
Kremp has created an R function to do just this, update_prob()
, along with linking to files you need on GitHub. He explains how this all works in Updating the Forecast on Election Night with R.
Bill Petti, who works at Gallup, has another model in R as a personal project that he calls SAFE -- Simple Averaged Forecasters Estimate. He uses a simple (non-weighted) average forecasts from six major sources.
Petti has also posted R code for scraping, formatting and plotting forecaster data.
If you want to import polling data into R and create your own model or forecast -- or just analyze who ended up most accurate -- the pollstR package pulls data from the Huffington Post's Pollster API. See the intro vignette on how the package works.
Or, here's a how-to on using US presidential polling data from the popular FiveThirtyEight site, by Peter Ellis ("a professional statistician - or possibly a data scientist - managing an analytics team in a New Zealand government Ministry").
Mapping results. If you'd like to make your own maps for any returns that come in -- national, state and local -- all you need are geographic shape files of states, precincts or whatever you're measuring, data and an R package or two. See my how-to, Create maps in R in 10 (fairly) easy steps.
If instead you prefer "tilegrams" -- maps that are sized for importance instead of actual geography (so, in the case of the presidential election, you don't end up with large, sparsely populated states looking more important than they are), check out the tilegramR package and a demo of how to use it.
And finally, if, like me, you like to match up election results with demographic information, accessing U.S. Census Bureau data is very helpful. I wrote a guide to downloading Census data using the censusapi package.