Predicting the US 2020 Presidential election utilizing a hidden vote REST API

LikesData
4 min readNov 9, 2020

Predicting the US 2020 Presidential election utilizing a hidden vote REST API

Like many people in America, I have had a nail biting experience watching the pundits going back and fourth on the cable news networks. As I’m sure many of you can relate, sometimes I need to see the data behind the story before being able to get comfortable with conclusions being made.

This post is to discuss how I utilized a JSON based REST API for county and state live lection data, and PowerBI to guesstimate the outcome of the election.

First column = final prediction, last column determine what percent of the remaining is needed for a Biden win

How did I end up here:

When Tuesday night came around I started with the standard election websites, CNN, FOX, AP — some had the data I needed, but not in one place, and definitely not in an easily accessible way.

However on one site, which I’ll leave anonymous for now, I noticed that the webpage was updating on a frequent and reoccurring basis without any page refresh.

DING … DING …. DING

For those of you with web development experience I’m sure you can tell where this story leads. If you don’t have web development experience, then I’d suggest reading into the basics of REST API’s.

Upon reviewing the network activity within the developer explorer in chrome I found a frequently updated election data REST api with easy to use endpoints.

Example data:

PowerBI set-up:

I had recently learned that PowerBI had a feature to quickly import JSON objects and tabulate the data into tables. What better time to test this feature out!

Let’s get into loading the data into PowerBI.

You can either write the M code yourself or utilize the new data source -> web -> enter in the URL of the .json endpoint.

Loading the REST API data

Now that the data is loaded, it is time to transform and model the results.

Modeling

I’m no election expert, but logically there’s a couple of key aspects when races come down to the wire:

  • Estimated remaining vote
  • Of the vote remaining, what is the historical voting percentage, in prior year or even better data for votes casted in the current race
  • County/State data
  • Votes casted so far

So lets summarize the modeling approach, which is calculated on a county level, and then grouped at a state level:

(1) Calculating estimated total turnout — We can utilize the reporting rate (i.e. how much a given county is estimated to have reported out of total turnout) to determine an ESTIMATE for total turnout. Formula = Current votes / reporting rate

(2) Calculating current voting rate — Determine the percentage of votes casted in favor of either party so far. Formula = Joe / Joe + Donald, OR Donald / Joe + Donald

(3) Calculating the percent needed of remaining vote to win (or at least be equal) — Using some algebra, we can solve for PERCENT in the following formula.

  • Current Trump votes + Remaining votes*Trump percent = Current Biden votes + Remaining Votes *(1-Trump percent)
  • Percent needed to be equal = -1*(-50*Current Biden votes- 50*Remaining Votes (estimate) + 50*Current Trump Votes)/Remaining/100

With a little bit of Python, and M we can multiply the turnout total * current voting rate to determine what the Estimated final vote will be

A little python, A LOT of BI steps

Results

If you made it this far I’m sure your wondering, how did this basic model perform?

Well if you keep in mind that this model is very basic, and it doesn’t account for too much other than the current voting ratio and an estimated number of votes remaining, then not bad for a short-term outlook, but definitely not useful for any form of long-term modeling .

Where it worked best was to get an idea of where the remaining votes (i.e. election in-process) would land a candidate in a given county, which rolls up to a final state estimate. So think of this as the FOX or CNN voting maps with a marginal amount of forward modeling added in.

Nonetheless, can’t say it didn’t make for some nice looking graphics!

Graphics for your viewing pleasure:

Votes remaining ratio gives an idea of the required number of votes remaining needed to win for a given candidate to win the race (based on estimated turnout)
That’s a lot of counties!

--

--