<p>This post is one of a series leading up to a purely data-driven probabilistic prediction model for the New Zealand general election in 2017. No punditry will be indulged in (if only to avoid complications with my weekday role as an apolitical public servant)! This is straight statistics, if there is such a thing…</p>
<h2>There are important sources of uncertainty in political polling other than sampling error</h2>
<p>An important question in using polling/survey data to predict election results is how to quantify the “house effects” of the different polling companies conducting surveys of potential voters. This is important for establishing which of the various pollsters is best at predicting the overall result which has obvious commercial implications for them; and also for predicting future election results for polling aggregators.</p>
<p>The naive approach is the one taken in <a target="_blank" rel="nofollow" href="http://m.nzherald.co.nz/nz/news/article.cfm?c_id=1&objectid=11328981">this article on polling leading up to the 2014 New Zealand general election</a>, which takes polling results as predictions of the eventual result. A more sophisticated approach is to treat polling numbers as data in an a predictive model in which election vote is a time-bound result. Basically, voters get to change their mind between a poll and the election – the more time between the two, the more randomness there is, and the uncertainty from this can certainly swamp the often-quoted margins of error.</p>
<p>If we look at the data from three major pollsters that have both a track record and continue to poll today (March 2017) for New Zealand intended party vote, we see that discrepancies between election results and voting intention estimated from opinion polls are not purely a matter of pollsters’ uncertainty or error:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0084-straight-polls-1.svg"></p>
<p>The case of the Labour Party in the period between the 2011 and 2014 elections is a particular case in point. All three pollsters in the chart had, on average, higher intended party vote for Labour during the period than eventually transpired. But the consistency of their patterns strongly suggests that there was a genuine trend over time, best captured by Reid Research and Roy Morgan, with a decrease in support in the months leading up to the actual election.</p>
<p>In contrast, in the 2011 and 2014 (and perhaps 2008) elections, all three pollsters do indeed seem to systematically underestimate the vote for New Zealand First.</p>
<h2>There are several things to take account of</h2>
<p>A well-regarded approach to studying the relationship between opinion polls and electoral results has been based on <a target="_blank" rel="nofollow" href="http://eppsac.utdallas.edu/files/jackman/CAJP%2040-4%20Jackman.pdf">a seminal 2005 article by Simon Jackman, “Pooling the Polls Over an Election Campaign”</a>:</p>
<blockquote>
<p>“Poll results vary over the course of a campaign election and across polling organisations, making it difficult to track genuine changes in voter support. I present a statistical model that tracks changes in voter support over time by pooling the polls, and corrects for variation across polling organisations due to biases known as ‘house effects’. The result is a less biased and more precise estimate of vote intentions than is possible from any one poll alone.”</p>
</blockquote>
<p><em>Simon Jackman</em></p>
<p>The method is also described in his classic text, <a target="_blank" rel="nofollow" href="http://au.wiley.com/WileyCDA/WileyTitle/productCd-0470011548.html">Bayesian Analysis for the Social Sciences</a>. The statistical method theorises a latent, largely unobserved (ie except on election day) state space of the voting intention each day, and that polls are various dirty variables linked to the state of that latent variable, but not direct observations of it. The state space changes at random due to unknown forces over time, and the various opinion polls are grossly imperfect glimpses of its reality. The method is state of the art but very computationally intensive – it requires estimating the state of the voting intention for each party on each day since observations began, resulting in many thousands of parameters if the model is fit over multiple election cycles. Computational methods exist for fitting such models and I intend to do so at some point, but I also wanted a quicker look at the data that doesn’t take hours or days to fit fit.</p>
<p>It’s worth noting at this point that there’s good reason to believe that the latent, unobserved voting intention isn’t as volatile as polls indicate – ie an endorsement of Jackman’s method or a similar smoothing approach to aggregate polls, with a conservative approach to change in the underlying intention compared even to the common weighted rolling average method of aggregation provides. A <a target="_blank" rel="nofollow" href="http://www.slate.com/articles/news_and_politics/politics/2016/08/don_t_be_fooled_by_clinton_trump_polling_bounces.html">brilliant study during the 2012 USA Presidential Election by Andrew Gelman and Andrew Rothschild</a> showed compellingly that much of the fluctuation in voting intention comes from bias in responses:</p>
<blockquote>
<p>“When there was good news for Mitt Romney, more Republicans opted to respond to the poll; when Obama was riding high, Democrats were more likely to respond. The result was that large and systematic changes in nonresponse had the effect of amplifying small changes in actual voter intention.”</p>
</blockquote>
<p>These results are compelling evidence for some kind of smoothing of polling results, that not only provides a weighted average of recent polls, but in addition some healthy statistical skepticism (or regularisation to shrink towards zero) to the apparent rapid moves up and down in voting intention.</p>
<h2>Generalized additive models give a cheap and quick solution to a broad range of smoothing challenges</h2>
<p>To get a glance at house effects in New Zealand polling, I decided to use generalized additive models for the polls leading up to individual elections, for one party at a time, to produce a predicted election result for each pollster-party-election combination. Most studies I’ve seen in this space have applied Jackman’s method to a single election cycle; the computational problems magnify with longer periods as do the data management challenges. My <code>nzelect</code> R package makes available multiple years of polling data sourced from Wikipedia (albeit unfortunately without sample size and margin of error information); using a GAM rather than a state space model massively reduces the computational challenges.</p>
<p>I limit myself to the seven parties with a party vote likely to influence the 2017 election and who also were in previous elections; and the three major pollsters who both have a track record and an intention of polling leading up to the 2017 election (my understanding is that Digipoll do not have such intention; otherwise they would be a fourth pollster to include in the analysis).</p>
<p>For each combination of party and election year I used the polling data from all pollsters to predict election result, allowing an absolute level of bias for each pollster in the model. For two of the pollsters I have four complete election cycles of data and I obtained these results:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0084-house1.svg"></p>
<p>Roy Morgan stands out as particularly inclined to over-estimate the vote for the Greens; and Colmar Brunton to under-estimate the vote for New Zealand First.</p>
<p>For the third pollster I only have two election cycles of data and I get these results:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0084-house2.svg"></p>
<p>Here is a table summarising the amount each pollster appears to over/under estimate voting intention, comparing the prediction for election day based on patterns to date with the actual result:</p>
<table>
<thead>
<tr>
<th>Party</th>
<th>Colmar Brunton</th>
<th>Reid Research</th>
<th>Roy Morgan</th>
</tr>
</thead>
<tbody>
<tr>
<td>ACT</td>
<td>-0.2%</td>
<td>-0.1%</td>
<td>0.2%</td>
</tr>
<tr>
<td>Green</td>
<td>0.6%</td>
<td>2%</td>
<td>2.6%</td>
</tr>
<tr>
<td>Labour</td>
<td>-0.1%</td>
<td>-0.9%</td>
<td>-1%</td>
</tr>
<tr>
<td>Maori</td>
<td>-0.2%</td>
<td>-0.2%</td>
<td>-0.1%</td>
</tr>
<tr>
<td>National</td>
<td>3.2%</td>
<td>3.2%</td>
<td>0.1%</td>
</tr>
<tr>
<td>NZ First</td>
<td>-1.8%</td>
<td>-2.5%</td>
<td>0%</td>
</tr>
<tr>
<td>United Future</td>
<td>-0.4%</td>
<td>-0.3%</td>
<td>0%</td>
</tr>
</tbody>
</table>
<h2>Number of polls</h2>
<p>For those who are interested, here is an exploratory chart on the number of actual polls available by pollster and election cycle which led me to my conclusion to fit models just to polls from Colmar Brunton, Reid Research and Roy Morgan:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0084-polls-year.svg"></p>
<h2>R code</h2>
<p>Here is the code in R for all the above. Caveat – this is very much a spare time project for me, and none of this has been peer reviewed. I’d never allow this situation in my professional life, but here I am publishing results on a political subject this way… Use at your own risk, and if you spot something wrong or an idea for improvement, please let me know.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>nzelect</span><span>)</span><span>
</span><span>library</span><span>(</span><span>mgcv</span><span>)</span><span>
</span><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>magrittr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RColorBrewer</span><span>)</span><span>
</span><span>#=====================data prep=======================
</span><span>
</span><span># vector of colours to use in graphics
</span><span>house_colours</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;black&quot;</span><span>,</span><span> </span><span>brewer.pal</span><span>(</span><span>3</span><span>,</span><span> </span><span>&quot;Set1&quot;</span><span>))</span><span>
</span><span>names</span><span>(</span><span>house_colours</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Election result&quot;</span><span>,</span><span> </span><span>&quot;Reid Research&quot;</span><span>,</span><span> </span><span>&quot;Colmar Brunton&quot;</span><span>,</span><span> </span><span>&quot;Roy Morgan&quot;</span><span>)</span><span>
</span><span># vector of just the seven main parties to use
</span><span>parties</span><span> </span><span>&lt;-</span><span> </span><span>polls</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>ElectionYear</span><span> </span><span>==</span><span> </span><span>2017</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>(</span><span>Party</span><span>)</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>&quot;Mana&quot;</span><span>,</span><span> </span><span>&quot;Conservative&quot;</span><span>))</span><span> </span><span>%$%</span><span>
</span><span>Party</span><span>
</span><span>#===============introductory graphic========================
</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>d</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>polls</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Party</span><span> </span><span>%in%</span><span> </span><span>parties</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Pollster</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;Reid Research&quot;</span><span>,</span><span> </span><span>&quot;Colmar Brunton&quot;</span><span>,</span><span> </span><span>&quot;Roy Morgan&quot;</span><span>,</span><span> </span><span>&quot;Election result&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>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>d</span><span>1</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_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;orange&quot;</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.4</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>filter</span><span>(</span><span>d</span><span>1</span><span>,</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><span>=</span><span> </span><span>.3</span><span>,</span><span> </span><span>se</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>filter</span><span>(</span><span>d</span><span>1</span><span>,</span><span> </span><span>Pollster</span><span> </span><span>==</span><span> </span><span>&quot;Election result&quot;</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>,</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>data</span><span> </span><span>=</span><span> </span><span>filter</span><span>(</span><span>d</span><span>1</span><span>,</span><span> </span><span>Pollster</span><span> </span><span>==</span><span> </span><span>&quot;Election result&quot;</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2</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>labs</span><span>(</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</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>house_colours</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Survey versus actual performance in New Zealand voting behaviour&quot;</span><span>,</span><span>
</span><span>&quot;New Zealand First seems systematically underestimated; Greens perhaps overestimated.&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: polls data collected by Wikipedia, available in the {nzelect} R package&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span> </span><span>~</span><span> </span><span>Party</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free&quot;</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>c</span><span>(</span><span>0.7</span><span>,</span><span> </span><span>0.15</span><span>))</span><span> </span><span>#=============estimate and present house &quot;bias&quot;=============
</span><span>house_bias</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>elect_years</span><span>,</span><span> </span><span>pollsters</span><span>){</span><span>
</span><span># depends on these objects being in environmenet:
</span><span> </span><span># polls, house_colours, parties
</span><span> </span><span>houses</span><span> </span><span>&lt;-</span><span> </span><span>expand.grid</span><span>(</span><span>elect_years</span><span>,</span><span> </span><span>pollsters</span><span>)</span><span>
</span><span>names</span><span>(</span><span>houses</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;ElectionYear&quot;</span><span>,</span><span> </span><span>&quot;Pollster&quot;</span><span>)</span><span>
</span><span>for</span><span>(</span><span>j</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>parties</span><span>)){</span><span>
</span><span>the_party</span><span> </span><span>=</span><span> </span><span>parties</span><span>[</span><span>j</span><span>]</span><span>
</span><span># election results:
</span><span> </span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>polls</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>ElectionYear</span><span> </span><span>%in%</span><span> </span><span>elect_years</span><span> </span><span>&amp;</span><span> </span><span>ElectionYear</span><span> </span><span>!=</span><span> </span><span>2002</span><span>)</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>filter</span><span>(</span><span>Party</span><span> </span><span>==</span><span> </span><span>the_party</span><span>)</span><span> </span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>length</span><span>(</span><span>elect_years</span><span>)){</span><span>
</span><span># Note we include *all* pollsters in the data for fitting the model
</span><span> </span><span>thedata</span><span> </span><span>&lt;-</span><span> </span><span>polls</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>ElectionYear</span><span> </span><span>==</span><span> </span><span>elect_years</span><span>[</span><span>i</span><span>]</span><span> </span><span>&amp;</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>filter</span><span>(</span><span>Party</span><span> </span><span>==</span><span> </span><span>the_party</span><span>)</span><span>
</span><span>mod</span><span> </span><span>&lt;-</span><span> </span><span>gam</span><span>(</span><span>VotingIntention</span><span> </span><span>~</span><span> </span><span>s</span><span>(</span><span>as.numeric</span><span>(</span><span>MidDate</span><span>))</span><span> </span><span>+</span><span> </span><span>Pollster</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;quasibinomial&quot;</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>thedata</span><span>)</span><span>
</span><span># for predicting values, we only take the pollsters we have an interest in:
</span><span> </span><span>preddata</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>MidDate</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>results</span><span>[</span><span>i</span><span>,</span><span> </span><span>&quot;MidDate&quot;</span><span>]),</span><span> </span><span>Pollster</span><span> </span><span>=</span><span> </span><span>pollsters</span><span>)</span><span>
</span><span># house effect is shown by the amount the predicted value from polling
</span><span> </span><span># is *more* than the actual vote. So a positive score means the poll
</span><span> </span><span># overestimated the actual vote:
</span><span> </span><span>houseeffects</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>mod</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>preddata</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;response&quot;</span><span>)</span><span> </span><span>-</span><span>
</span><span>results</span><span>[</span><span>i</span><span>,</span><span> </span><span>&quot;VotingIntention&quot;</span><span>]</span><span>
</span><span>houses</span><span>[</span><span>houses</span><span>$</span><span>ElectionYear</span><span> </span><span>==</span><span> </span><span>elect_years</span><span>[</span><span>i</span><span>],</span><span> </span><span>the_party</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>houseeffects</span><span>
</span><span>}</span><span>
</span><span>}</span><span> </span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>houses</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Party</span><span>,</span><span> </span><span>`Polling overestimate`</span><span>,</span><span> </span><span>-</span><span>ElectionYear</span><span>,</span><span> </span><span>-</span><span>Pollster</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>ElectionYear</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>`Polling overestimate`</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_hline</span><span>(</span><span>yintercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_line</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>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>house_colours</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;Election year&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2005</span><span>,</span><span> </span><span>2008</span><span>,</span><span> </span><span>2011</span><span>,</span><span> </span><span>2014</span><span>),</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2004</span><span>,</span><span> </span><span>2015</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</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>c</span><span>(</span><span>0.9</span><span>,</span><span> </span><span>0.18</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Statistical forecast of election compared to actual result&quot;</span><span>,</span><span>
</span><span>&quot;Forecasts use time series methods based on pollsters' results, are not actual pollsters' forecasts&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: polls data collected by Wikipedia, available in the {nzelect} R package&quot;</span><span>)</span><span>
</span><span>print</span><span>(</span><span>p</span><span>)</span><span>
</span><span>houses_av</span><span> </span><span>&lt;-</span><span> </span><span>houses</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Party</span><span>,</span><span> </span><span>Bias</span><span>,</span><span> </span><span>-</span><span>ElectionYear</span><span>,</span><span> </span><span>-</span><span>Pollster</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>Party</span><span>,</span><span> </span><span>Pollster</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Bias</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>Bias</span><span>))</span><span>
</span><span>return</span><span>(</span><span>houses_av</span><span>)</span><span>
</span><span>}</span><span>
</span><span>hb1</span><span> </span><span>&lt;-</span><span> </span><span>house_bias</span><span>(</span><span>elect_years</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2005</span><span>,</span><span> </span><span>2008</span><span>,</span><span> </span><span>2011</span><span>,</span><span> </span><span>2014</span><span>),</span><span>
</span><span>pollsters</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Colmar Brunton&quot;</span><span>,</span><span> </span><span>&quot;Roy Morgan&quot;</span><span>))</span><span> </span><span>hb2</span><span> </span><span>&lt;-</span><span> </span><span>house_bias</span><span>(</span><span>elect_years</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2011</span><span>,</span><span> </span><span>2014</span><span>),</span><span>
</span><span>pollsters</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Reid Research&quot;</span><span>,</span><span> </span><span>&quot;Colmar Brunton&quot;</span><span>,</span><span> </span><span>&quot;Roy Morgan&quot;</span><span>))</span><span> </span><span># table for blog post:
</span><span>hb2</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>Pollster</span><span> </span><span>==</span><span> </span><span>&quot;Reid Research&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rbind</span><span>(</span><span>hb1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>Party</span><span>,</span><span> </span><span>Pollster</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>`Average bias`</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>round</span><span>(</span><span>Bias</span><span> </span><span>*</span><span> </span><span>100</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>&quot;%&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>Bias</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>Pollster</span><span>,</span><span> </span><span>`Average bias`</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>align</span><span> </span><span>=</span><span> </span><span>&quot;lrrr&quot;</span><span>)</span><span>
</span><span>#===================how many polls per year==========================
</span><span>polls</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>ElectionYear</span><span>,</span><span> </span><span>Pollster</span><span>,</span><span> </span><span>MidDate</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>ElectionYear</span><span>,</span><span> </span><span>Pollster</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Polls</span><span> </span><span>=</span><span> </span><span>n</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>Pollster</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>Pollster</span><span>,</span><span> </span><span>Polls</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>Polls</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Pollster</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>ElectionYear</span><span>)))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>ElectionYear</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></code></pre>
</figure>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/1Fui4mBB6tI">