Analysis of Canadian Federal Elections Data

Overview

Canadian Federal Elections

election is a small Rust program for processing vote data from Canadian Federal Elections.

After building, see election --help for the available options.

Table of Contents

Installation

You will need git and cargo installed. To fetch the code and build the election tool:

git clone [email protected]:fosskers/election.git
cd election
cargo build --release

Getting the Data

election currently supports election years 2008, 2011, 2015 and 2019, although 2008 and 2011 require manual conversion from LATIN to UTF-8 beforehand. 2021 will be supported as soon as the data is made public.

Federal election data can be found here. For example, to get the 2019 data, navigate as follows:

  • 43rd General Election, October 21, 2019
  • Go to the raw data version (for researchers)
  • Canada / Provinces / Territories
  • Canada (Poll-by-poll Results - Format 2)

You must use the Format 2 data.

Download this data and unzip its contents into data/2019/ within your clone of this repository. For example:

mkdir -p data/2019
cd data/2019
wget https://elections.ca/res/rep/off/ovr2019app/51/data_donnees/pollresults_resultatsbureauCanada.zip
unzip pollresults_resultatsbureauCanada.zip
rm pollresults_resultatsbureauCanada.zip

Interpretting the Output

All results are output as JSON. I use another tool, Nushell, to format the results into nice tables.

Party Results per Riding

For example, to see how the Rhinoceros Party (a joke party) did in each riding:

cargo run --release -- --party=rin | from json | reverse | first 10
───┬────────────────────────────────────┬───────┬─────────────────────┬───────────────┬───────┬────────┬───────
 # │               riding               │ party │      last_name      │  first_name   │ votes │ ratio  │  won
───┼────────────────────────────────────┼───────┼─────────────────────┼───────────────┼───────┼────────┼───────
 0 │ Beauce                             │ Rhino │ Bernier             │ Maxime        │ 1084  │ 0.0182 │ false
 1 │ Gaspésie--Les Îles-de-la-Madeleine │ Rhino │ Cowboy              │ Jay           │  353  │ 0.0091 │ false
 2 │ Calgary Signal Hill                │ Rhino │ Bassett             │ Christina     │  511  │ 0.0080 │ false
 3 │ Papineau                           │ Rhino │ Cacereco Berthiaume │ Jean-Patrick  │  363  │ 0.0071 │ false
 4 │ Chicoutimi--Le Fjord               │ Rhino │ Bélanger            │ Line          │  299  │ 0.0068 │ false
 5 │ Québec                             │ Rhino │ CoRhino             │ Sébastien     │  349  │ 0.0064 │ false
 6 │ Hochelaga                          │ Rhino │ Blais-Leduc         │ Chinook       │  314  │ 0.0059 │ false
 7 │ Rosemont--La Petite-Patrie         │ Rhino │ Lavoie              │ Jos Guitare   │  346  │ 0.0057 │ false
 8 │ Mégantic--L'Érable                 │ Rhino │ Roy                 │ Damien        │  256  │ 0.0053 │ false
 9 │ LaSalle--Émard--Verdun             │ Rhino │ Bélanger            │ Rhino Jacques │  265  │ 0.0050 │ false
───┴────────────────────────────────────┴───────┴─────────────────────┴───────────────┴───────┴────────┴───────

Party Results per Riding (won seats only)

The big parties have many more candidates than won seats. If we want to see just the victorious seats:

cargo run --release -- --party=grn | from json | where won | reverse
───┬───────────────────────┬─────────────┬───────────┬────────────┬───────┬────────┬──────
 # │        riding         │    party    │ last_name │ first_name │ votes │ ratio  │ won
───┼───────────────────────┼─────────────┼───────────┼────────────┼───────┼────────┼──────
 0 │ Saanich--Gulf Islands │ Green Party │ May       │ Elizabeth  │ 33454 │ 0.4908 │ true
 1 │ Nanaimo--Ladysmith    │ Green Party │ Manly     │ Paul       │ 24844 │ 0.3457 │ true
 2 │ Fredericton           │ Green Party │ Atwin     │ Jenica     │ 16640 │ 0.3367 │ true
───┴───────────────────────┴─────────────┴───────────┴────────────┴───────┴────────┴──────

Per-party Totals

Or perhaps we wish to see how every party did overall:

cargo run --release -- --total | from json | sort-by votes | reverse
────┬───────────────────────────────┬─────────┬────────┬───────
 #  │             party             │  votes  │ ratio  │ seats
────┼───────────────────────────────┼─────────┼────────┼───────
  0 │ Conservative                  │ 6239227 │ 0.3433 │  121
  1 │ Liberal                       │ 6018728 │ 0.3312 │  157
  2 │ New Democratic Party          │ 2903722 │ 0.1598 │  24
  3 │ Bloc Québécois                │ 1387030 │ 0.0763 │  32
  4 │ Green Party                   │ 1189607 │ 0.0654 │   3
  5 │ People's Party                │ 294092  │ 0.0161 │   0
  6 │ Independent                   │  72546  │ 0.0039 │   1
  7 │ Christian Heritage Party      │  18901  │ 0.0010 │   0
  8 │ Rhinoceros Party              │  9538   │ 0.0005 │   0
  9 │ Libertarian                   │  8367   │ 0.0004 │   0
 10 │ Veteran's Coalition           │  6300   │ 0.0003 │   0
 11 │ Animal Protection Party       │  4408   │ 0.0002 │   0
 12 │ Marxist-Leninist              │  4124   │ 0.0002 │   0
 13 │ Communist                     │  3905   │ 0.0002 │   0
 14 │ Pour l'Indépendance du Québec │  3815   │ 0.0002 │   0
 15 │ No Affiliation                │  1745   │ 0.0000 │   0
 16 │ PC Party                      │  1534   │ 0.0000 │   0
 17 │ Radical Marijuana             │   920   │ 0.0000 │   0
 18 │ Canada's Fourth Front         │   682   │ 0.0000 │   0
 19 │ United Party of Canada        │   602   │ 0.0000 │   0
 20 │ National Citizens Alliance    │   510   │ 0.0000 │   0
 21 │ Stop Climate Change           │   296   │ 0.0000 │   0
 22 │ Nationalist                   │   281   │ 0.0000 │   0
────┴───────────────────────────────┴─────────┴────────┴───────

Average Win Ratios

How good is each party at winning the seats they win? That is, regardless of how many seats they did actually win, in cases where they won, did they win by a large amount? We can find such average win ratios:

cargo run --release -- --party=blq | from json | where won | get ratio | math avg
0.448361485625
cargo run --release -- --party=ndp | from json | where won | get ratio | math avg
0.418011905
cargo run --release -- --party=grn | from json | where won | get ratio | math avg
0.39112569

So for the seats they won in 2019, the Bloc seemed to win by higher percentages than the NDP and the Greens.

You might also like...
A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, written in Rust

Datafuse Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture Datafuse is a Real-Time Data Processing & Analytics DBMS wit

ConnectorX - Fastest library to load data from DB to DataFrames in Rust and Python
ConnectorX - Fastest library to load data from DB to DataFrames in Rust and Python

ConnectorX enables you to load data from databases into Python in the fastest and most memory efficient way.

A highly efficient daemon for streaming data from Kafka into Delta Lake

A highly efficient daemon for streaming data from Kafka into Delta Lake

A cross-platform library to retrieve performance statistics data.

A toolkit designed to be a foundation for applications to monitor their performance.

TensorBase is a new big data warehousing with modern efforts.
TensorBase is a new big data warehousing with modern efforts.

TensorBase is a new big data warehousing with modern efforts.

Fill Apache Arrow record batches from an ODBC data source in Rust.

arrow-odbc Fill Apache Arrow arrays from ODBC data sources. This crate is build on top of the arrow and odbc-api crate and enables you to read the dat

📊  Cube.js — Open-Source Analytics API for Building Data Apps
📊 Cube.js — Open-Source Analytics API for Building Data Apps

📊 Cube.js — Open-Source Analytics API for Building Data Apps

Provides a way to use enums to describe and execute ordered data pipelines. 🦀🐾

enum_pipline Provides a way to use enums to describe and execute ordered data pipelines. 🦀 🐾 I needed a succinct way to describe 2d pixel map operat

Perhaps the fastest and most memory efficient way to pull data from PostgreSQL into pandas and numpy. 🚀

flaco Perhaps the fastest and most memory efficient way to pull data from PostgreSQL into pandas and numpy. 🚀 Have a gander at the initial benchmarks

Owner
Colin Woodbury
I write Rust, Haskell, and Go. 現在カナダで開発。
Colin Woodbury
A fast, powerful, flexible and easy to use open source data analysis and manipulation tool written in Rust

fisher-rs fisher-rs is a Rust library that brings powerful data manipulation and analysis capabilities to Rust developers, inspired by the popular pan

null 5 Sep 6, 2023
Yet Another Technical Analysis library [for Rust]

YATA Yet Another Technical Analysis library YaTa implements most common technical analysis methods and indicators. It also provides you an interface t

Dmitry 197 Dec 29, 2022
A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

Datafuse Labs 5k Jan 9, 2023
New generation decentralized data warehouse and streaming data pipeline

World's first decentralized real-time data warehouse, on your laptop Docs | Demo | Tutorials | Examples | FAQ | Chat Get Started Watch this introducto

kamu 184 Dec 22, 2022
This library provides a data view for reading and writing data in a byte array.

Docs This library provides a data view for reading and writing data in a byte array. This library requires feature(generic_const_exprs) to be enabled.

null 2 Nov 2, 2022
High-performance runtime for data analytics applications

Weld Documentation Weld is a language and runtime for improving the performance of data-intensive applications. It optimizes across libraries and func

Weld 2.9k Dec 28, 2022
A high-performance, high-reliability observability data pipeline.

Quickstart • Docs • Guides • Integrations • Chat • Download What is Vector? Vector is a high-performance, end-to-end (agent & aggregator) observabilit

Timber 12.1k Jan 2, 2023
Rayon: A data parallelism library for Rust

Rayon Rayon is a data-parallelism library for Rust. It is extremely lightweight and makes it easy to convert a sequential computation into a parallel

null 7.8k Jan 8, 2023
Quickwit is a big data search engine.

Quickwit This repository will host Quickwit, the big data search engine developed by Quickwit Inc. We will progressively polish and opensource our cod

Quickwit Inc. 2.9k Jan 7, 2023
DataFrame / Series data processing in Rust

black-jack While PRs are welcome, the approach taken only allows for concrete types (String, f64, i64, ...) I'm not sure this is the way to go. I want

Miles Granger 30 Dec 10, 2022