Tutorial: analyzing presidential elections

Before you start:

Given the timeliness of the 2012 US Presidential Election and the inherent intricacies of the electoral process, we internally used an interesting dataset on poll results to test RethinkDB’s query language support in the JavaScript, Python, and Ruby client libraries.

We’ll use this dataset to walk through RethinkDB’s JavaScript query language using the Data Explorer.

If you want to do follow the tutorial with the node driver:

Import the data

Download the datasets:

wget https://raw.github.com/rethinkdb/rethinkdb/next/demos/election/input_polls.json
wget https://raw.github.com/rethinkdb/rethinkdb/next/demos/election/county_stats.json

Import them in RethinkDB:

rethinkdb import -c localhost:28015 --table test.input_polls --pkey uuid -f input_polls.json --format json
rethinkdb import -c localhost:28015 --table test.county_stats --pkey uuid -f county_stats.json --format json

What does this data set contain?

You have imported two tables:

  • input_polls contains multiple poll results at state-level
  • county_stats contains various population stats at county-level

You can take a look at the documents in each set with these queries:


The result returned should be something similar to:

    "id":"New Hampshire",
    "Pollster":"Marist Coll.-2",
    "Date":"Jun 25"

For the table county_stats:


You will get back something with this schema:

    "ctyname":"Champaign County",

Data cleanup: chaining, group-map-reduce, simple map

We’ll first clean up the data in input_polls, as we want to calculate the average results of various polls at the state level. We’ll also get rid of unnecessary/empty attributes. Finally we’ll store the result in a new table:

First let’s create a new table that will contain the clean data.


Then let’s rework the data and save it in polls. We are going to group polls per state and compute the average score for each party.

     .group("id")         // We group the table by `id`, which is the state name.
     .pluck('Dem', 'GOP') // We pluck out the poll results we care about.
     .merge({polls: 1})   // And finally, we add an extra field `polls: 1` to each row.
     .reduce(function(left, right){
        // We reduce over the polls, adding up the results and keeping
        // track of the total number of polls.
        return {
            Dem: left("Dem").add(right("Dem")),
            GOP: left("GOP").add(right("GOP")),
            polls: left("polls").add(right("polls"))
         // We ungroup and divide the fields `Dem` and `GOP` for each state
         // by the number of polls to get the average result per state.
         return {
             Dem: state("reduction")("Dem").div(state("reduction")("polls")),
             GOP: state("reduction")("GOP").div(state("reduction")("polls")),
             polls: state("reduction")("polls"),
             id: state("group")

If everything went well, you should see that we inserted 51 documents (one per state plus one for Washington DC).


If you take a look at the Arizona state


You should get back this document:

	"Dem": 42.294117647058826,
	"GOP": 48.294117647058826,
	"polls": 17,
	"id": "Arizona"

Data analysis: projections, JOINs, orderby, group-map-reduce

Based on this data let’s try to see if we can figure out how many voters a party would need to turn to win the states. For the sake of this post, we’ll go with the Democrats.

Let’s start with what estimates polls project at the county level by JOINing the polls and county_stats tables:

r.table('county_stats').eqJoin('Stname', r.table('polls')) // equi join of the two tables
	.zip() // flatten the results
	.pluck('Stname', 'state', 'county', 'ctyname', 'CENSUS2010POP', 'POPESTIMATE2011', 'Dem', 'GOP') // projection

Building on this query, next we can find the counties where the Democrats are in minority:

r.table('county_stats').eqJoin('Stname', r.table('polls'))
	.pluck('Stname', 'state', 'county', 'ctyname', 'CENSUS2010POP', 'POPESTIMATE2011', 'Dem', 'GOP')
	.filter(function(doc) { return doc('Dem').lt(doc('GOP')) })

Or even better where Democrats are within 15% of the Republicans:

r.table('county_stats').eqJoin('Stname', r.table('polls'))
	.pluck('Stname', 'state', 'county', 'ctyname', 'CENSUS2010POP', 'POPESTIMATE2011', 'Dem', 'GOP')
	.filter(function(doc) { return doc('Dem').lt(doc('GOP')).and(doc('GOP').sub(doc('Dem')).lt(15)) })

The last step in answering the initial question of how many voters should the Democrats win to turn the results is just a group/map/sum away:

r.table('county_stats').eqJoin('Stname', r.table('polls')).zip()
    .pluck('Stname', 'state', 'county', 'ctyname', 'CENSUS2010POP', 'POPESTIMATE2011', 'Dem', 'GOP')
    .filter(function(doc) { return doc('Dem').lt(doc('GOP')).and(doc('GOP').sub(doc('Dem')).lt(15)) })
    .map(function(doc){return doc('POPESTIMATE2011').mul(doc("GOP").sub(doc("Dem"))).div(100);})

And the outcome of our quick presidential election data analysis that addresses the question how many voters the Democrat party would need to turn to win the states (this assumes 100% turnout of the entire population of a state):

Data analysis with RethinkDB

If you followed along, the queries above should have given you a taste of ReQL: chaining, projections, order by, JOINs, group. Of course this tutorial isn’t statistically significant. If you interested in statistically significant results, checkout the election statistics superhero Nate Silver.