Function to scrape, format, and plot forecaster data for the 2016 U.S. Presidential Election
For information about the SAFE model and other data about the election, see here.
The pres_forecasts
function will first scrape the state-level forecasts aggregated by The Upshot:
After scraping the data, the function will extract the numerical data from each state/forecaster cell, melt the data, and then create variables for the numerical probability of a Democratic and Republican victory for each state/forecaster combination.
The data is then re-cast so that average probabilities can be calculated across the six forecasters. This is done for both parties, with cumulative electoral votes calculated sorted by the probability of a party winning a state. A date column is also added.
Three plots are also provided. The first plots the cumulative probability for each party in a faceted chart, with a dashed horizontal line plotted at 270 electoral votes. The second plot simply combines both parties into one chart. And, finally, the third plots each of the forecasters in a comparative way based on data from the current date.
The function will also assign the plots and two main data sets to the Global Environment as well as exporting them to the working directory with a unique name based on the date the function was run.