<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/new-data-and-functions-in-nzelect-0-3-0-r-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/03/11/nzelect-0.3.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>Polling data and other goodies ready for download</h2>
<p>A new version, 0.3.0, of the <code>nzelect</code> R package is <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=nzelect">now available on CRAN</a>.</p>
<ul>
<li>historical polling data from 2002 to February 2017, sourced from Wikipedia</li>
<li>some small functions to help convert voting numbers into seats in a New Zealand or similar proportional representation system; and to weight polling numbers in the way done by two of New Zealand’s polling aggregator websites.</li>
<li>some small bits of metadata on political parties, most notably named vectors of their colours for use in graphics.</li>
</ul>
<p><code>nzelect</code> was originally developed in response to <a target="_blank" rel="nofollow" href="http://www.arilamstein.com/blog/2016/04/18/r-election-analysis-contest-results/">Ari Lamstein’s R election analysis contest</a>, and my series of blog posts drawing on its data won me that competition. The first major version included polling-place election results from the 2014 election. The main purpose of the package remains to make these data available in tidier, more analysis-ready format than the <a target="_blank" rel="nofollow" href="http://archive.electionresults.govt.nz/">Electoral Commission’s official election results site</a>.</p>
<p>I still have plans to add the results from earlier elections, but with a New Zealand general election now scheduled for 23 September 2017 I thought I’d prioritise some polling data. I’m hoping to up the level of sophistication of at least a corner of the debate in the leadup to the election. I’ve got some blog posts on things like house effects (eg historical biases of different polling firms when confronted with actual election results) and probabilistic prediction coming up, and needed a clean and tidy set of historical data to do this.</p>
<h2>Historical polling data</h2>
<p>The polling data was the main addition to this version of <code>nzelect</code>. The data have been scraped from a range of Wikipedia pages and subjected to some cleaning. With this somewhat sketchy provenance, they can’t be guaranteed but they look very plausible. All the data have been combined in a single data frame <code>polls</code> which looks like this:</p>
<div>
<pre><code>           Pollster      WikipediaDates  StartDate    EndDate    MidDate         Party VotingIntention   Client ElectionYear
5965 Colmar Brunton 11–15 February 2017 2017-02-11 2017-02-15 2017-02-13 United Future            0.00 One News         2017
5966 Colmar Brunton 11–15 February 2017 2017-02-11 2017-02-15 2017-02-13         Maori            0.01 One News         2017
5967 Colmar Brunton 11–15 February 2017 2017-02-11 2017-02-15 2017-02-13       Destiny              NA One News         2017
5968 Colmar Brunton 11–15 February 2017 2017-02-11 2017-02-15 2017-02-13   Progressive              NA One News         2017
5969 Colmar Brunton 11–15 February 2017 2017-02-11 2017-02-15 2017-02-13          Mana            0.01 One News         2017
5970 Colmar Brunton 11–15 February 2017 2017-02-11 2017-02-15 2017-02-13  Conservative            0.00 One News         2017
</code></pre>
</div>
<p>Election results (actual total party vote by party) are also included for convenience. This doesn’t remove the need to bring in the detailed historical election results by polling place in future versions of <code>nzelect</code>, but gives a start on the historical perspective.</p>
<p>Combining multiple election cycles of polling data together makes it possible to see the longer game in party political change in New Zealand:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0082-polls.svg"></p>
<p>Here’s the code behind that graphic.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>nzelect</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rvest</span><span>)</span><span>
</span><span>library</span><span>(</span><span>magrittr</span><span>)</span><span>
</span><span>#=========polls demo=========
</span><span>election_dates</span><span> </span><span>&lt;-</span><span> </span><span>polls</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Pollster</span><span> </span><span>==</span><span> </span><span>&quot;Election result&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>MidDate</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>()</span><span>
</span><span>polls</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>Party</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;Destiny&quot;</span><span>,</span><span> </span><span>&quot;Progressive&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>gsub</span><span>(</span><span>&quot;M.ori&quot;</span><span>,</span><span> </span><span>&quot;Maori&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>VotingIntention</span><span>,</span><span> </span><span>.desc</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span>
</span><span>Pollster</span><span> </span><span>=</span><span> </span><span>fct_relevel</span><span>(</span><span>Pollster</span><span>,</span><span> </span><span>&quot;Election result&quot;</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>MidDate</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>VotingIntention</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>Pollster</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>0.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>aes</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>Party</span><span>),</span><span> </span><span>se</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey15&quot;</span><span>,</span><span> </span><span>span</span><span> </span><span>=</span><span> </span><span>.20</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Voting intention&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_x_date</span><span>(</span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>Party</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_vline</span><span>(</span><span>xintercept</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>election_dates</span><span>$</span><span>MidDate</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey80&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;15 years of voting intention opinion polls in New Zealand&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: nzelect #Rstats package on CRAN&quot;</span><span>)</span></code></pre>
</figure>
<p>The CRAN version of an R package isn’t the appropriate way to make day to day updates available – upgrades on CRAN should only be every three months or so at the most. I will probably keep the GitHub version up to date as more polling data comes in, but I’m not in the position to give a service level commitment on timeliness.</p>
<h2>Converting voting results to seats</h2>
<p>One thing we need to be able to do efficiently if we’re going to facilitate polling punditry is convert election results – real or hypothetical – into actual seats in Parliament. Since 1996, New Zealand has 120 or more seats in its single house of parliament to be allocated by a system known as “mixed-member proportional”. Each elector has two votes – an electorate vote and a party vote. <a target="_blank" rel="nofollow" href="http://www.elections.org.nz/news-media/new-zealand-2014-general-election-official-results">71 of the seats (at the time of writing)</a> are “first past the post” electorates. However, these electorate votes have very little (not quite none) impact on the total make-up of Parliament, because the remaining seats are allocated to “lists” provided by the parties in such a way as to be in proportion to the electors’ party votes.</p>
<p>Only parties that received five percent of the party vote, or that have won at least one electorate, are counted in the proportional representation part.</p>
<p>The reason why there are 120 “or more” seats is the same as the reason why electorate votes have not quite exactly zero impact on the overall makeup. If a party wins more electorates than they would be entitled to from their party vote, the difference is translated into <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Overhang_seat">“overhang seats”</a>. After the 2014 election there was one such seat; the leader of the United Future party won the Ōhariu electorate, but the party received only 0.22% of the party vote (much less than 1/120th), so he holds the 121st seat in Parliament.</p>
<p>The method of allocating the list seats is known as the <a target="_blank" rel="nofollow" href="http://www.elections.org.nz/voting-system/mmp-voting-system/sainte-lague-allocation-formula">Sainte-Laguë</a> or <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Webster/Sainte-Lagu%C3%AB_method">Webster/Sainte-Laguë</a> method. It’s well explained on <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Webster/Sainte-Lagu%C3%AB_method">Wikipedia</a>. It’s now available in <code>nzelect</code> via the <code>allocate_seats()</code> function.</p>
<p><code>allocate_seats</code> defaults to New Zealand settings (5% threshold, 120 seats if no overhang seats) but these can be set to other values for use in other electoral systems or conducting thought experiments about New Zealand. For example, the 5% threshhold acts as a barrier to small parties getting representation in Parliament proportionate to their support. A <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Electoral_reform_in_New_Zealand">2012 review</a> recommended amongst other things reducing this to 4%, although this wasn’t adopted by Parliament. Other countries with similar systems have lower thresholds; for example, Israel has had no less than four different threshold figures in the past thirty years (1%, 1.5%, 2%, and the current value of 3.25%).</p>
<p>Here’s how the New Zealand Parliament would have looked with different values of the threshold for getting access to the proportional representation part of the system:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0082-scenarios1.svg"></p>
<p>We can see that the Conservative party were the big losers from the 5% threshold rule; with 3.97% of the party vote and no seats in Parliament under current rules (or indeed the 4% threshold proposed and rejected in 2012), but five seats if using the Israeli threshold.</p>
<p>Using electoral results data and functions from the <code>nzelect</code> and other packages, here’s how that analysis was done:</p>
<figure>
<pre><code data-lang="r"><span>#================2014 theshold scenarios=========
# Party vote results from the 2014 election
</span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>GE2014</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>VotingType</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>VotingType</span><span>,</span><span> </span><span>&quot;Vote&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>Party</span><span>,</span><span> </span><span>VotingType</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>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>VotingType</span><span>,</span><span> </span><span>Votes</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>Party</span><span>,</span><span> </span><span>PartyVote</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>PartyVote</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Party</span><span> </span><span>!=</span><span> </span><span>&quot;Informal Party Votes&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>PartyVote</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;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>Party</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;Internet MANA&quot;</span><span>,</span><span> </span><span>&quot;Mana&quot;</span><span>,</span><span> </span><span>Party</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>parties_df</span><span>[,</span><span> </span><span>c</span><span>(</span><span>&quot;Shortname&quot;</span><span>,</span><span> </span><span>&quot;Colour&quot;</span><span>)],</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Party&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Shortname&quot;</span><span>))</span><span>
</span><span># Electorate seats from the 2014 election
</span><span>electorate</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>41</span><span>,</span><span> </span><span>27</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>0</span><span>,</span><span>
</span><span>1</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>0</span><span>,</span><span>
</span><span>0</span><span>,</span><span>0</span><span>,</span><span>0</span><span>)</span><span>
</span><span># Seats awarded under different scenarios:
</span><span>res2014</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>party</span><span> </span><span>=</span><span> </span><span>results</span><span>$</span><span>Party</span><span>,</span><span>
</span><span>`5% threshold\n(actual result)`</span><span> </span><span>=</span><span> </span><span>allocate_seats</span><span>(</span><span>results</span><span>$</span><span>PartyVote</span><span>,</span><span> </span><span>electorate</span><span> </span><span>=</span><span> </span><span>electorate</span><span>)</span><span>$</span><span>seats_v</span><span>,</span><span>
</span><span>`3.25% threshold like Israel\n(hypothetical result)`</span><span> </span><span>=</span><span> </span><span>allocate_seats</span><span>(</span><span>results</span><span>$</span><span>PartyVote</span><span>,</span><span> </span><span>electorate</span><span> </span><span>=</span><span> </span><span>electorate</span><span>,</span><span> </span><span>threshold</span><span> </span><span>=</span><span> </span><span>0.0325</span><span>)</span><span>$</span><span>seats_v</span><span>,</span><span>
</span><span>`No threshold\n(hypothetical result)`</span><span> </span><span>=</span><span> </span><span>allocate_seats</span><span>(</span><span>results</span><span>$</span><span>PartyVote</span><span>,</span><span> </span><span>electorate</span><span> </span><span>=</span><span> </span><span>electorate</span><span>,</span><span> </span><span>threshold</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span>$</span><span>seats_v</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>scenario</span><span>,</span><span> </span><span>seats</span><span>,</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>str_wrap</span><span>(</span><span>party</span><span>,</span><span> </span><span>15</span><span>),</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>-</span><span>seats</span><span>,</span><span> </span><span>fun</span><span> </span><span>=</span><span> </span><span>mean</span><span>),</span><span>
</span><span>scenario</span><span> </span><span>=</span><span> </span><span>fct_relevel</span><span>(</span><span>scenario</span><span>,</span><span> </span><span>&quot;5% threshold\n(actual result)&quot;</span><span>),</span><span>
</span><span>party</span><span> </span><span>=</span><span> </span><span>fct_other</span><span>(</span><span>party</span><span>,</span><span> </span><span>keep</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>party</span><span>)[</span><span>1</span><span>:</span><span>10</span><span>]))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>party</span><span>,</span><span> </span><span>scenario</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>seats</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>seats</span><span>))</span><span> </span><span>res2014</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>seats</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>scenario</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>seats</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>party</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_segment</span><span>(</span><span>xend</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>aes</span><span>(</span><span>yend</span><span> </span><span>=</span><span> </span><span>scenario</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>party</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</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>parties_v</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_discrete</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>res2014</span><span>$</span><span>scenario</span><span>)[</span><span>3</span><span>:</span><span>1</span><span>])</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Impact of changing minimum threshold for seats in Parliament&quot;</span><span>,</span><span>
</span><span>&quot;New Zealand 2014 election seat allocation&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Seats&quot;</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Analysis using the `nzelect` R package&quot;</span><span>)</span></code></pre>
</figure>
<p>For comparison, here are the same scenarios with the 2011 election results:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0082-scenarios2.svg"></p>
<p>Because the full 2011 election results aren’t yet available in <code>nzelect</code>, the code below needs to scrape them from a HTML table on the Electoral Commission’s site, using the very user-friendly <code>rvest</code> R package:</p>
<figure>
<pre><code data-lang="r"><span>#================2011 theshold scenarios=========
</span><span>res2011</span><span> </span><span>&lt;-</span><span> </span><span>&quot;http://archive.electionresults.govt.nz/electionresults_2011/partystatus.html&quot;</span><span> </span><span>%>%</span><span>
</span><span>read_html</span><span>()</span><span> </span><span>%>%</span><span>
</span><span># extract all the tables:
</span><span> </span><span>html_nodes</span><span>(</span><span>css</span><span> </span><span>=</span><span> </span><span>&quot;table&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># we only want the fourth table:
</span><span> </span><span>extract2</span><span>(</span><span>4</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># turn it into a data frame:
</span><span> </span><span>html_table</span><span>()</span><span> </span><span>%>%</span><span>
</span><span># drop the first four rows:
</span><span> </span><span>slice</span><span>(</span><span>-</span><span>(</span><span>1</span><span>:</span><span>4</span><span>))</span><span>
</span><span># give it some real names:
</span><span>names</span><span>(</span><span>res2011</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Party&quot;</span><span>,</span><span> </span><span>&quot;PartyVote&quot;</span><span>,</span><span> </span><span>&quot;PropVote&quot;</span><span>,</span><span> </span><span>&quot;ElectorateSeats&quot;</span><span>,</span><span> </span><span>&quot;ListSeats&quot;</span><span>,</span><span> </span><span>&quot;TotalSeats&quot;</span><span>)</span><span>
</span><span>res2011</span><span> </span><span>&lt;-</span><span> </span><span>res2011</span><span> </span><span>%>%</span><span>
</span><span># turn into numbers:
</span><span> </span><span>mutate</span><span>(</span><span>PartyVote</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>gsub</span><span>(</span><span>&quot;,&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>PartyVote</span><span>)),</span><span>
</span><span>ElectorateSeats</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>ElectorateSeats</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># remove the &quot;total seats&quot; row:
</span><span> </span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>PartyVote</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># estimate seats
</span><span> </span><span>mutate</span><span>(</span><span>
</span><span>`5% threshold\n(actual result)`</span><span> </span><span>=</span><span> </span><span>allocate_seats</span><span>(</span><span>PartyVote</span><span>,</span><span> </span><span>electorate</span><span> </span><span>=</span><span> </span><span>ElectorateSeats</span><span>)</span><span>$</span><span>seats_v</span><span>,</span><span>
</span><span>`3.25% threshold like Israel\n(hypothetical result)`</span><span> </span><span>=</span><span> </span><span>allocate_seats</span><span>(</span><span>PartyVote</span><span>,</span><span> </span><span>electorate</span><span> </span><span>=</span><span> </span><span>ElectorateSeats</span><span>,</span><span> </span><span>threshold</span><span> </span><span>=</span><span> </span><span>0.0325</span><span>)</span><span>$</span><span>seats_v</span><span>,</span><span>
</span><span>`No threshold\n(hypothetical result)`</span><span> </span><span>=</span><span> </span><span>allocate_seats</span><span>(</span><span>PartyVote</span><span>,</span><span> </span><span>electorate</span><span> </span><span>=</span><span> </span><span>ElectorateSeats</span><span>,</span><span> </span><span>threshold</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span>$</span><span>seats_v</span><span>
</span><span>)</span><span>
</span><span>res2011</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>(</span><span>PartyVote</span><span>:</span><span>TotalSeats</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>scenario</span><span>,</span><span> </span><span>seats</span><span>,</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>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;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>left_join</span><span>(</span><span>parties_df</span><span>[,</span><span> </span><span>c</span><span>(</span><span>&quot;Shortname&quot;</span><span>,</span><span> </span><span>&quot;Colour&quot;</span><span>)],</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Party&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Shortname&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_reorder</span><span>(</span><span>Party</span><span>,</span><span> </span><span>-</span><span>seats</span><span>),</span><span>
</span><span>Party</span><span> </span><span>=</span><span> </span><span>fct_other</span><span>(</span><span>Party</span><span>,</span><span> </span><span>keep</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>Party</span><span>)[</span><span>1</span><span>:</span><span>11</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>seats</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>scenario</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>seats</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>Party</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_segment</span><span>(</span><span>xend</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>aes</span><span>(</span><span>yend</span><span> </span><span>=</span><span> </span><span>scenario</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>Party</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.3</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>parties_v</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_discrete</span><span>(</span><span>&quot;&quot;</span><span>,</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>res2014</span><span>$</span><span>scenario</span><span>)[</span><span>3</span><span>:</span><span>1</span><span>])</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;none&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Impact of changing minimum threshold for seats in Parliament&quot;</span><span>,</span><span>
</span><span>&quot;New Zealand 2011 election seat allocation&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Seats&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Analysis using the `nzelect` R package&quot;</span><span>)</span></code></pre>
</figure>
<p>New Zealand has a Westminster-like rather than USA-like relation of parliament to the executive, in that the government needs to command a majority in Parliament (indicated in budget or confidence votes) or resign. In practice, this generally leads to government by a coalition of parties, although this is not inevitable; in the 2014 election the National Party came within a whisker of being able to govern by itself (although political logic suggests they would have kept at least some junior coalition parties anyway).</p>
<p><code>nzelect</code> is available for installation from CRAN the usual way (<code>install.packages(&quot;nzelect&quot;)</code>). Report any bugs, enhancement requests or issues on <a target="_blank" rel="nofollow" href="https://github.com/ellisp/nzelect/issues">GitHub</a> please. I already know about the wrong vignette title on CRAN :(… will fix it next release.</p>
<p><a target="_blank" rel="nofollow" href="http://www.r-pkg.org/pkg/nzelect"><img alt="CRAN version" src="http://www.r-pkg.org/badges/version/nzelect"></a><br>
<a target="_blank" rel="nofollow" href="http://www.r-pkg.org/pkg/nzelect"><img alt="CRAN RStudio mirror downloads" src="http://cranlogs.r-pkg.org/badges/nzelect"></a></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/03/11/nzelect-0.3.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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/5gRijI8_E74"></a></div>