<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/nzelect-0-4-0-on-cran-with-results-from-2002-to-2014-and-polls-up-to-september-2017-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/10/05/nzelect-0-4-0"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>More nzelect New Zealand election data on CRAN</h2>
<p>Version 0.4.0 of my <code>nzelect</code> R package is now on CRAN. The key changes from version 0.3.0 are:</p>
<ul>
<li>election results by voting place are now available back to 2002 (was just 2014)</li>
<li>polling place locations, presented as consistent values of latitude and longitude, now available back to 2008 (was just 2014)</li>
<li>voting intention polls are now complete up to the 2017 general election (previously stopped about six months ago on CRAN, although the GitHub version was always kept up to date)</li>
<li>a few minor bug fixes eg <code>allocate_seats</code> now takes integer arguments, not just numeric.</li>
</ul>
<p>The definitive source of New Zealand election statistics is the <a rel="nofollow" href="http://www.electionresults.govt.nz/" target="_blank">Electoral Commission</a>. If there are any discrepencies between their results and those in <code>nzelect</code>, it’s a bug, and please file an <a rel="nofollow" href="https://github.com/ellisp/nzelect/issues" target="_blank">issue on GitHub</a>. The voting intention polls come from Wikipedia.</p>
<h2>Example – special and early votes</h2>
<p>Currently, while we wait for the counting of the “special” votes from the 2017 election, there’s renewed interest in the differences between special votes and those counted on election night. Special votes are those made by anyone outside their electorate, enrolled in the month before the election, or are in one of a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Special_vote" target="_blank">few other categories</a>. Most importantly, it’s people who are on the move or who are very recently enrolled, and obviously such people are different from the run of the mill voter.</p>
<p>Here’s a graphic created with the <code>nzelect</code> R package that shows how the “special votes” in the past have been disproportionately important for Greens and Labour:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0109-special.svg"></p>
<p>Here’s the code to create that graphic:</p>
<figure>
<pre><code data-lang="r"><span># get the latest version from CRAN:
</span><span>install.packages</span><span>(</span><span>&quot;nzelect&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>nzelect</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggrepel</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span># palette of colours for the next couple of charts: </span><span>palette</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>parties_v</span><span>,</span><span> </span><span>Other</span><span> </span><span>=</span><span> </span><span>&quot;pink2&quot;</span><span>,</span><span> </span><span>`Informal Votes`</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>)</span><span>
</span><span># special votes:
</span><span>nzge</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>voting_type</span><span> </span><span>==</span><span> </span><span>&quot;Party&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>party</span><span> </span><span>=</span><span> </span><span>fct_lump</span><span>(</span><span>party</span><span>,</span><span> </span><span>5</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>dummy</span><span> </span><span>=</span><span> </span><span>grepl</span><span>(</span><span>&quot;special&quot;</span><span>,</span><span> </span><span>voting_place</span><span>,</span><span> </span><span>ignore.case</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>electorate</span><span>,</span><span> </span><span>party</span><span>,</span><span> </span><span>election_year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>prop_before</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>votes</span><span>[</span><span>dummy</span><span>])</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>votes</span><span>),</span><span>
</span><span>total_votes</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>votes</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>party</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot; Party&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>party</span><span>),</span><span>
</span><span>party</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;ACT New Zealand&quot;</span><span>,</span><span> </span><span>&quot;ACT&quot;</span><span>,</span><span> </span><span>party</span><span>),</span><span>
</span><span>party</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;New Zealand First&quot;</span><span>,</span><span> </span><span>&quot;NZ First&quot;</span><span>,</span><span> </span><span>party</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>party</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>party</span><span>,</span><span> </span><span>prop_before</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>prop_before</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>party</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>total_votes</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>party</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>election_year</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.1</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;'Special' votes proportion by party, electorate and year&quot;</span><span>,</span><span>
</span><span>&quot;Each point represents the proportion of a party's vote in each electorate that came from special votes&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: www.electionresults.govt.nz, collated in the nzelect R package&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_size_area</span><span>(</span><span>&quot;Total party votes&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;\nPercentage of party's votes that were 'special'&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>palette</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span></code></pre>
</figure>
<p>Special votes are sometimes confused with advance voting in general. While many special votes are advance votes, the relationship is far from one to one. We see this particularly acutely by comparing the previous graphic to one that is identical except that it identifies all advance votes (those with the phrase “BEFORE” in the Electoral Commission’s description of polling place):</p>
<p><img width="100%" src="http://ellisp.github.io/img/0109-before.svg"></p>
<p>While New Zealand First are the party that gains least proportionately from special votes, they gain the most from advance votes, although the difference between parties is fairly marginal. New Zealand First voters are noticeably <a rel="nofollow" href="http://ellisp.github.io/blog/2017/05/06/nz-first" target="_blank">more likely to be in an older age bracket</a> than the voters for other parties. My speculation on their disproportionate share of advance voting is that it is related to that, although I’m not an expert in that area and am interested in alternative views.</p>
<p>This second graphic also shows nicely just how much the advance voting is becoming a feature of the electoral landscape. Unlike the proportion of votes that are “special” which has been fairly stable, the proportion of votes that are case in advance has increased very substantially over the past decade, and increased further in the 2017 election (for which final results come out on Saturday).</p>
<p>Here’s the code for the second graphic; it’s basically the same as the previous chunk of code, except filtering on a different character string in the voting place name:</p>
<figure>
<pre><code data-lang="r"><span>nzge</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>voting_type</span><span> </span><span>==</span><span> </span><span>&quot;Party&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>party</span><span> </span><span>=</span><span> </span><span>fct_lump</span><span>(</span><span>party</span><span>,</span><span> </span><span>5</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>dummy</span><span> </span><span>=</span><span> </span><span>grepl</span><span>(</span><span>&quot;before&quot;</span><span>,</span><span> </span><span>voting_place</span><span>,</span><span> </span><span>ignore.case</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>electorate</span><span>,</span><span> </span><span>party</span><span>,</span><span> </span><span>election_year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>prop_before</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>votes</span><span>[</span><span>dummy</span><span>])</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>votes</span><span>),</span><span>
</span><span>total_votes</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>votes</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>party</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot; Party&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>party</span><span>),</span><span>
</span><span>party</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;ACT New Zealand&quot;</span><span>,</span><span> </span><span>&quot;ACT&quot;</span><span>,</span><span> </span><span>party</span><span>),</span><span>
</span><span>party</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;New Zealand First&quot;</span><span>,</span><span> </span><span>&quot;NZ First&quot;</span><span>,</span><span> </span><span>party</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>party</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>party</span><span>,</span><span> </span><span>prop_before</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>prop_before</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>party</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>total_votes</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>party</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>election_year</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.1</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;'Before' votes proportion by party, electorate and year&quot;</span><span>,</span><span>
</span><span>&quot;Each point represents the proportion of a party's vote in each electorate that were cast before election day&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: www.electionresults.govt.nz, collated in the nzelect R package&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_size_area</span><span>(</span><span>&quot;Total party votes&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;\nPercentage of party's votes that were before election day&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>palette</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span></code></pre>
</figure>
<h2>‘Party’ compared to ‘Candidate’ vote</h2>
<p>Looking for something else to showcase, I thought it might be interesting to pool all five elections for which I have the detailed results and compare the party vote (ie the proportional representation choice out of the two votes New Zealanders get) to the candidate vote (ie the representative member choice). Here’s a graphic that does just that:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0109-party-cand.svg"></p>
<p>We see that New Zealand First and the Greens are the two parties that are most noticeably above the diagonal line indicating equality between party and candidate votes. This isn’t a surprise – these are minority parties that appeal to (different) demographic and issues-based communities that are dispersed across the country, and generally have little chance of winning individual electorates. Hence the practice of voters is often to split their votes. This is all perfectly fine and is exactly how mixed-member proportional voting systems are meant to work.</p>
<p>Here’s the code that produced the scatter plot:</p>
<figure>
<pre><code data-lang="r"><span>nzge</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>voting_type</span><span>,</span><span> </span><span>party</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>votes</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>votes</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>voting_type</span><span>,</span><span> </span><span>votes</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Candidate</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Party</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>party</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_abline</span><span>(</span><span>intercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>slope</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_text_repel</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_log10</span><span>(</span><span>&quot;Total 'candidate' votes&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>10</span><span>,</span><span> </span><span>100</span><span>,</span><span> </span><span>1000</span><span>)</span><span> </span><span>*</span><span> </span><span>1000</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_log10</span><span>(</span><span>&quot;Total 'party' votes&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>10</span><span>,</span><span> </span><span>100</span><span>,</span><span> </span><span>1000</span><span>)</span><span> </span><span>*</span><span> </span><span>1000</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Lots of political parties: total votes from 2002 to 2014&quot;</span><span>,</span><span>
</span><span>&quot;New Zealand general elections&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: www.electionresults.govt.nz, collated in the nzelect R package&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_equal</span><span>()</span></code></pre>
</figure>
<h2>What next?</h2>
<p>Obviously, the next big thing for <code>nzelect</code> is to get the 2017 results in once they are announced on Saturday. I should be able to do this for the GitHub version by early next week. I would have delayed the CRAN release until 2017 results were available, but unfortunately I had a bug in some of the examples in my helpfiles that stopped them working after the 23 September 2017, so I had to rush a fix and the latest enhancements to CRAN to avoid problems with the CRAN maintainers (which I fully endorse and thank by the way).</p>
<p>My other plans for nzelect over the next months to a year include:</p>
<ul>
<li>reliable point locations for voting places back to 2002</li>
<li>identify consistent/duplicate voting places over time to make it easier to analyse comparative change by micro location</li>
<li>add detailed election results for the 1999 and 1996 elections (these are saved under a different naming convention to those from 2002 onwards, which is why they need a bit more work)</li>
<li>add high level election results for prior to 1994</li>
</ul>
<p>The source code for cleaning the election data and packaging it into <code>nzelect</code> is <a rel="nofollow" href="https://github.com/ellisp/nzelect" target="_blank">on GitHub</a>. The package itself is <a rel="nofollow" href="https://cran.r-project.org/package=nzelect" target="_blank">on CRAN</a> and installable in the usual way.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ellisp.github.io/blog/2017/10/05/nzelect-0-4-0"> Peter's stats stuff - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hafqD26jxrE" height="1"></a></div>