election
is a small Rust program for processing vote data from Canadian Federal Elections.
After building, see election --help
for the available options.
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
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
All results are output as JSON. I use another tool, Nushell, to format the results into nice tables.
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 ───┴────────────────────────────────────┴───────┴─────────────────────┴───────────────┴───────┴────────┴───────
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 ───┴───────────────────────┴─────────────┴───────────┴────────────┴───────┴────────┴──────
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 ────┴───────────────────────────────┴─────────┴────────┴───────
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.