<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/state-space-modelling-of-the-australian-2007-federal-election/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/06/24/oz-polls-statespace"> 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>Pooling the polls with Bayesian statistics</h2>
<p>In <a rel="nofollow" href="http://eppsac.utdallas.edu/files/jackman/CAJP%2040-4%20Jackman.pdf" target="_blank">an important 2005 article in the Australian Journal of Political Science</a>, Simon Jackman set out a statistically-based approach to pooling polls in an election campaign. He describes the sensible intuitive approach of modelling a latent, unobserved voting intention (unobserved except on the day of the actual election) and treats each poll as a random observation based on that latent state space. Uncertainty associated with each measurement comes from sample size and bias coming from the average effect of the firm conducting the poll, as well as of course uncertainty about the state of the unobserved voting intention. This approach allows house effects and the latent state space to be estimated simultaneously, quantifies the uncertainty associated with both, and in general gives a much more satisfying method of pooling polls than any kind of weighted average.</p>
<p>Jackman gives a worked example of the approach in his excellent book <a rel="nofollow" href="http://au.wiley.com/WileyCDA/WileyTitle/productCd-0470011548.html" target="_blank">Bayesian Analysis for the Social Sciences</a>, using voting intention for the Australian Labor Party (ALP) in the 2007 Australian federal election for data. He provides <code>JAGS</code> code for fitting the model, but notes that with over 1,000 parameters to estimate (most of those parameters are the estimated voting intention for each day between the 2004 and 2007 elections) it is painfully slow to fit in general purpose MCMC-based Bayesian tools such as <code>WinBUGS</code> or <code>JAGS</code> – several days of CPU time on a fast computer in 2009. Jackman estimated his model with Gibbs sampling implemented directly in R.</p>
<p>Down the track, I want to implement Jackman’s method of polling aggregation myself, to estimate latent voting intention for New Zealand to provide an alternative method for my <a rel="nofollow" href="http://ellisp.github.io/elections/elections.html" target="_blank">election forecasts</a>. I set myself the familiarisation task of reproducing his results for the Australian 2007 election. New Zealand’s elections are a little complex to model because of the multiple parties in the proportional representation system, so I wanted to use a general Bayesian tool for the purpose to simplify my model specification when I came to it. I use <a rel="nofollow" href="http://mc-stan.org/" target="_blank">Stan</a> because its <a rel="nofollow" href="https://en.wikipedia.org/wiki/Hybrid_Monte_Carlo" target="_blank">Hamiltonian Monte Carlo</a> method of exploring the parameter space works well when there are many parameters – as in this case, with well over 1,000 parameters to estimate.</p>
<p>Stan describes itself as “a state-of-the-art platform for statistical modeling and high-performance statistical computation. Thousands of users rely on Stan for statistical modeling, data analysis, and prediction in the social, biological, and physical sciences, engineering, and business.” It lets the programmer specify a complex statistical model, and given a set of data will return a range of parameter estimates that were most likely to produce the observed data. Stan isn’t something you use as an end-to-end workbench – it’s assumed that data manipulation and presentation is done with another tool such as R, Matlab or Python. Stan focuses on doing one thing well – using Hamiltonian Monte Carlo to estimate complex statistical models, potentially with many thousands of hierarchical parameters, with arbitrarily set prior distributions.</p>
<p><em>Caveat! – I’m fairly new to Stan and I’m pretty sure my Stan programs that follow aren’t best practice, even though I am confident they work. Use at your own risk!</em></p>
<h2>Basic approach – estimated voting intention in the absence of polls</h2>
<p>I approached the problem in stages, gradually making my model more realistic. First, I set myself the task of modelling latent first-preference support for the ALP in the absence of polling data. If all we had were the 2004 and 2007 election results, where might we have thought ALP support went between those two points? Here’s my results:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0102-no-polls.svg"></p>
<p>For this first analysis, I specified that support for the ALP had to be a random walk that changed by a normally distributed variable with standard deviation of 0.25 percentage points for each daily change. Why 0.25? Just because Jim Savage used it in his <a rel="nofollow" href="http://andrewgelman.com/2016/08/06/state-space-poll-averaging-model/" target="_blank">rough application of this approach</a> to the US Presidential election in 2016. I’ll be relaxing this assumption later.</p>
<p>Here’s the R code that sets up the session, brings in the data from Jackman’s <code>pscl</code> R package, and defines a graphics function that I’ll be using for each model I create.</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>pscl</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rstan</span><span>)</span><span>
</span><span>rstan_options</span><span>(</span><span>auto_write</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>options</span><span>(</span><span>mc.cores</span><span> </span><span>=</span><span> </span><span>7</span><span>)</span><span>
</span><span>#=========2004 election to 2007 election==============
</span><span>data</span><span>(</span><span>AustralianElectionPolling</span><span>)</span><span>
</span><span>data</span><span>(</span><span>AustralianElections</span><span>)</span><span>
</span><span>days_between_elections</span><span> </span><span>&lt;-</span><span> </span><span>as.integer</span><span>(</span><span>diff</span><span>(</span><span>as.Date</span><span>(</span><span>c</span><span>(</span><span>&quot;2004-10-09&quot;</span><span>,</span><span> </span><span>&quot;2007-11-24&quot;</span><span>))))</span><span> </span><span>+</span><span> </span><span>1</span><span>
</span><span>#' Function to plot time series extracted from a stan fit of latent state space model of 2007 Australian election
</span><span>plot_results</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>stan_m</span><span>){</span><span>
</span><span>if</span><span>(</span><span>class</span><span>(</span><span>stan_m</span><span>)</span><span> </span><span>!=</span><span> </span><span>&quot;stanfit&quot;</span><span>){</span><span>
</span><span>stop</span><span>(</span><span>&quot;stan_m must be an object of class stanfit, with parameters mu representing latent vote at a point in time&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>ex</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>rstan</span><span>::</span><span>extract</span><span>(</span><span>stan_m</span><span>,</span><span> </span><span>&quot;mu&quot;</span><span>))</span><span>
</span><span>names</span><span>(</span><span>ex</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>:</span><span>d</span><span>1</span><span>$</span><span>n_days</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ex</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>day</span><span>,</span><span> </span><span>value</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>day</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>day</span><span>),</span><span>
</span><span>day</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>day</span><span>,</span><span> </span><span>origin</span><span> </span><span>=</span><span> </span><span>&quot;2004-10-08&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>day</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>mean</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>value</span><span>),</span><span>
</span><span>upper</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>value</span><span>,</span><span> </span><span>0.975</span><span>),</span><span>
</span><span>lower</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>value</span><span>,</span><span> </span><span>0.025</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>day</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;Shaded region shows a pointwise 95% credible interval.&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Voting intention for the ALP (%)&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: Jackman's pscl R package; analysis at https://ellisp.github.io&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_ribbon</span><span>(</span><span>aes</span><span>(</span><span>ymin</span><span> </span><span>=</span><span> </span><span>lower</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>upper</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>geom_line</span><span>(</span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>mean</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>breaks</span><span> </span><span>=</span><span> </span><span>31</span><span>:</span><span>54</span><span>,</span><span> </span><span>sec.axis</span><span> </span><span>=</span><span> </span><span>dup_axis</span><span>(</span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span>
</span><span>return</span><span>(</span><span>p</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p>Here’s the Stan program that specifies this super simple model of changing ALP support from 2004 to 2007:</p>
<figure>
<pre><code data-lang="stan">// oz-polls-1.stan

data {
  int&lt;lower=1> n_days;           // number of days
  real mu_start;                 // value at starting election
  real mu_finish;                // value at final election
}
parameters {
  real&lt;lower=0,upper=100> mu[n_days];               // underlying state of vote intention
}

model {
  
  // state model
  mu[1] ~ normal(mu_start, 0.01);
  for (i in 2:n_days) 
      mu[i] ~ normal(mu[i - 1], 0.25);
      
  // measurement model
  // 1. Election result
  mu[n_days] ~ normal(mu_finish, 0.01);
  
}</code></pre>
</figure>
<p>And here’s the R code that calls that Stan program and draws the resulting summary graphic. Stan works by compiling a program in C++ that is based on the statistical model specified in the <code>*.stan</code> file. Then the C++ program zooms around the high-dimensional parameter space, moving slower around the combinations of parameters that seem more likely given the data and the specified prior distributions. It can use multiple processors on your machine and works super fast given the complexity of what it’s doing.</p>
<figure>
<pre><code data-lang="r"><span>#----------------no polls inbetween the elections------------
</span><span>d</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>mu_start</span><span> </span><span>=</span><span> </span><span>37.64</span><span>,</span><span> </span><span>mu_finish</span><span> </span><span>=</span><span> </span><span>43.38</span><span>,</span><span> </span><span>n_days</span><span> </span><span>=</span><span> </span><span>days_between_elections</span><span>)</span><span>
</span><span># returns some warnings first time it compiles; see
# http://mc-stan.org/misc/warnings.html suggests most compiler
# warnings can be just ignored.
</span><span>system.time</span><span>({</span><span>
</span><span>stan_mod1</span><span> </span><span>&lt;-</span><span> </span><span>stan</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>'oz-polls-1.stan'</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>1</span><span>,</span><span>
</span><span>control</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>max_treedepth</span><span> </span><span>=</span><span> </span><span>20</span><span>))</span><span>
</span><span>})</span><span> </span><span># 1800 seconds
</span><span>
</span><span>plot_results</span><span>(</span><span>stan_mod1</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Voting intention for the ALP between the 2004 and 2007 Australian elections&quot;</span><span>,</span><span>
</span><span>&quot;Latent variable estimated with no use of polling data&quot;</span><span>)</span></code></pre>
</figure>
<h2>Adding in one polling firm</h2>
<p>Next I wanted to add a single polling firm. I chose Nielsen’s 42 polls because Jackman found they had a fairly low bias, which removed one complication for me as I built up my familiarity with the approach. Here’s the result:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0102-one-poll.svg"></p>
<p>That model was specified in Stan as set out below. The Stan program is more complex now; I’ve had to specify how many polls I have (<code>y_n</code>), the values for each poll (<code>y_values</code>), and the days since the last election each poll was taken (<code>y_days</code>). This way I only have to specify 42 measurement errors as part of the probability model – other implementations I’ve seen of this approach ask for an estimate of measurement error for each poll on each day, treating the days with no polls as missing values to be estimated. That obviously adds a huge computational load I wanted to avoid.</p>
<p>In this program, I haven’t yet added in the notion of a house effect for Nielsen. Each measurement Nielsen made is assumed to have been an unbiased one. Again, I’ll be relaxing this later. The state model is also the same as before ie standard deviation of the day to day innovations is still hard coded as 0.25 percentage points.</p>
<figure>
<pre><code data-lang="stan">// oz-polls-2.stan

data {
  int&lt;lower=1> n_days;            // number of days
  real mu_start;                  // value at starting election
  real mu_finish;                 // value at final election
  int&lt;lower=1> y_n;               // number of polls
  real y_values[y_n];             // actual values in polls
  int&lt;lower=0> y_days[y_n];       // the number of days since starting election each poll was taken
  real y_se[y_n];
}
parameters {
  real&lt;lower=0,upper=100> mu[n_days];               // underlying state of vote intention
}

model {
  
  // state model
  mu[1] ~ normal(mu_start, 0.01);
  for (i in 2:n_days) 
      mu[i] ~ normal(mu[i - 1], 0.25);
      
  // measurement model
  // 1. Election result
  mu[n_days] ~ normal(mu_finish, 0.01);
  
  // 2. Polls
  for(t in 1:y_n)
      y_values[t] ~ normal(mu[y_days[t]], y_se[t]);
  
}</code></pre>
</figure>
<p>Here’s the R code to prepare the data and pass it to Stan. Interestingly, fitting this model is noticeably faster than the one with no polling data at all. My intuition for this is that now the state space is constrained to being reasonably close to some actually observed measurements, it’s an easier job for Stan to know where is good to explore.</p>
<figure>
<pre><code data-lang="r"><span>#--------------------AC Nielson-------------------
</span><span>ac</span><span> </span><span>&lt;-</span><span> </span><span>AustralianElectionPolling</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>org</span><span> </span><span>==</span><span> </span><span>&quot;Nielsen&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>MidDate</span><span> </span><span>=</span><span> </span><span>startDate</span><span> </span><span>+</span><span> </span><span>(</span><span>endDate</span><span> </span><span>-</span><span> </span><span>startDate</span><span>)</span><span> </span><span>/</span><span> </span><span>2</span><span>,</span><span>
</span><span>MidDateNum</span><span> </span><span>=</span><span> </span><span>as.integer</span><span>(</span><span>MidDate</span><span> </span><span>-</span><span> </span><span>as.Date</span><span>(</span><span>&quot;2004-10-08&quot;</span><span>)),</span><span> </span><span># ie number of days since first election; last election (9 October 2004) is day 1
</span><span> </span><span>p</span><span> </span><span>=</span><span> </span><span>ALP</span><span> </span><span>/</span><span> </span><span>100</span><span>,</span><span>
</span><span>se_alp</span><span> </span><span>=</span><span> </span><span>sqrt</span><span>(</span><span>p</span><span> </span><span>*</span><span> </span><span>(</span><span>1</span><span>-</span><span> </span><span>p</span><span>)</span><span> </span><span>/</span><span> </span><span>sampleSize</span><span>)</span><span> </span><span>*</span><span> </span><span>100</span><span>)</span><span>
</span><span>d</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>
</span><span>mu_start</span><span> </span><span>=</span><span> </span><span>37.64</span><span>,</span><span>
</span><span>mu_finish</span><span> </span><span>=</span><span> </span><span>43.38</span><span>,</span><span>
</span><span>n_days</span><span> </span><span>=</span><span> </span><span>days_between_elections</span><span>,</span><span>
</span><span>y_values</span><span> </span><span>=</span><span> </span><span>ac</span><span>$</span><span>ALP</span><span>,</span><span>
</span><span>y_days</span><span> </span><span>=</span><span> </span><span>ac</span><span>$</span><span>MidDateNum</span><span>,</span><span>
</span><span>y_n</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>ac</span><span>),</span><span>
</span><span>y_se</span><span> </span><span>=</span><span> </span><span>ac</span><span>$</span><span>se_alp</span><span>
</span><span>)</span><span>
</span><span>system.time</span><span>({</span><span>
</span><span>stan_mod2</span><span> </span><span>&lt;-</span><span> </span><span>stan</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>'oz-polls-2.stan'</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>2</span><span>,</span><span>
</span><span>control</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>max_treedepth</span><span> </span><span>=</span><span> </span><span>20</span><span>))</span><span>
</span><span>})</span><span> </span><span># 510 seconds
</span><span>
</span><span>plot_results</span><span>(</span><span>stan_mod2</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>ac</span><span>,</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>ALP</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Voting intention for the ALP between the 2004 and 2007 Australian elections&quot;</span><span>,</span><span>
</span><span>&quot;Latent variable estimated with use of just one firm's polling data (Nielsen)&quot;</span><span>)</span></code></pre>
</figure>
<h2>Including all five polling houses</h2>
<p>Finally, the complete model replicating Jackman’s work:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0102-all-polls.svg"></p>
<p>As well as adding the other four sets of polls, I’ve introduced five house effects that need to be estimated (ie the bias for each polling firm/mode); and I’ve told Stan to estimate the standard deviation of the day-to-day innovations in the latent support for ALP rather than hard-coding it as 0.25. Jackman specified a uniform prior on <code>[0, 1]</code> for that parameter, but I found this led to lots of estimation problems for Stan. The Stan developers give some <a rel="nofollow" href="https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations" target="_blank">great practical advice</a> on this sort of issue and I adapted some of that to specify the prior distribution for the standard deviation of day to day innovation as <code>N(0.5, 0.5)</code>, constrained to be positive.</p>
<p>Here’s the Stan program:</p>
<figure>
<pre><code data-lang="stan">// oz-polls-3.stan

data {
  int&lt;lower=1> n_days;            // number of days
  real mu_start;                  // value at starting election
  real mu_finish;                 // value at final election
  
  // change the below into 5 matrixes with 3 columns each for values, days, standard error
  int y1_n;                     // number of polls
  int y2_n;
  int y3_n;
  int y4_n;
  int y5_n;
  real y1_values[y1_n];       // actual values in polls
  real y2_values[y2_n];       
  real y3_values[y3_n];       
  real y4_values[y4_n];       
  real y5_values[y5_n];       
  int y1_days[y1_n];          // the number of days since starting election each poll was taken
  int y2_days[y2_n]; 
  int y3_days[y3_n]; 
  int y4_days[y4_n]; 
  int y5_days[y5_n]; 
  real y1_se[y1_n];             // the standard errors of the polls
  real y2_se[y2_n];           
  real y3_se[y3_n];           
  real y4_se[y4_n];           
  real y5_se[y5_n];           
}
parameters {
  real&lt;lower=0,upper=100> mu[n_days];               // underlying state of vote intention
  real d[5];                                        // polling effects
  real&lt;lower=0> sigma;                              // sd of innovations
}

model {
  
  // state model
  mu[1] ~ normal(mu_start, 0.01); // starting point
  
  // Jackman used uniform(0, 1) for sigma, but this seems to be the cause
  // of a lot of problems with the estimation process.
  // https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations
  // recommends not using a uniform, but constraining sigma to be positive
  // and using an open ended prior instead.  So:
  sigma ~ normal(0.5, 0.5);              // prior for innovation sd.  
  
  for (i in 2:n_days) 
      mu[i] ~ normal(mu[i - 1], sigma);
      
  // measurement model
  // 1. Election result
  mu[n_days] ~ normal(mu_finish, 0.01);
  
  // 2. Polls
  d ~ normal(0, 7.5); // ie a fairly loose prior for house effects
  
  for(t in 1:y1_n)
      y1_values[t] ~ normal(mu[y1_days[t]] + d[1], y1_se[t]);
  for(t in 1:y2_n)
      y2_values[t] ~ normal(mu[y2_days[t]] + d[2], y2_se[t]);
  for(t in 1:y3_n)
      y3_values[t] ~ normal(mu[y3_days[t]] + d[3], y3_se[t]);
  for(t in 1:y4_n)
      y4_values[t] ~ normal(mu[y4_days[t]] + d[4], y4_se[t]);
  for(t in 1:y5_n)
      y5_values[t] ~ normal(mu[y5_days[t]] + d[5], y5_se[t]);
}</code></pre>
</figure>
<p>Building the fact there are 5 polling firms (or firm-mode combinations, as Morgan is in there twice) directly into the program must be bad practice, but seeing as there are different numbers of polls taken by each firm and on different days I couldn’t work out a better way to do it. Stan doesn’t support ragged arrays, or objects like R’s lists, or (I think) convenient subsetting of tables, which would be the three ways I’d normally try to do that in another language. So I settled for the approach above, even though it has some ugly bits of repetition.</p>
<p>Here’s the R code that sorts the data and passes it to Stan</p>
<figure>
<pre><code data-lang="r"><span>#-------------------all 5 polls--------------------
</span><span>all_polls</span><span> </span><span>&lt;-</span><span> </span><span>AustralianElectionPolling</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>MidDate</span><span> </span><span>=</span><span> </span><span>startDate</span><span> </span><span>+</span><span> </span><span>(</span><span>endDate</span><span> </span><span>-</span><span> </span><span>startDate</span><span>)</span><span> </span><span>/</span><span> </span><span>2</span><span>,</span><span>
</span><span>MidDateNum</span><span> </span><span>=</span><span> </span><span>as.integer</span><span>(</span><span>MidDate</span><span> </span><span>-</span><span> </span><span>as.Date</span><span>(</span><span>&quot;2004-10-08&quot;</span><span>)),</span><span> </span><span># ie number of days since starting election
</span><span> </span><span>p</span><span> </span><span>=</span><span> </span><span>ALP</span><span> </span><span>/</span><span> </span><span>100</span><span>,</span><span>
</span><span>se_alp</span><span> </span><span>=</span><span> </span><span>sqrt</span><span>(</span><span>p</span><span> </span><span>*</span><span> </span><span>(</span><span>1</span><span>-</span><span> </span><span>p</span><span>)</span><span> </span><span>/</span><span> </span><span>sampleSize</span><span>)</span><span> </span><span>*</span><span> </span><span>100</span><span>,</span><span>
</span><span>org</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>org</span><span>,</span><span> </span><span>ALP</span><span>))</span><span>
</span><span>poll_orgs</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>unique</span><span>(</span><span>all_polls</span><span>$</span><span>org</span><span>))</span><span>
</span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>all_polls</span><span>,</span><span> </span><span>org</span><span> </span><span>==</span><span> </span><span>poll_orgs</span><span>[[</span><span>1</span><span>]])</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>all_polls</span><span>,</span><span> </span><span>org</span><span> </span><span>==</span><span> </span><span>poll_orgs</span><span>[[</span><span>2</span><span>]])</span><span>
</span><span>p</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>all_polls</span><span>,</span><span> </span><span>org</span><span> </span><span>==</span><span> </span><span>poll_orgs</span><span>[[</span><span>3</span><span>]])</span><span>
</span><span>p</span><span>4</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>all_polls</span><span>,</span><span> </span><span>org</span><span> </span><span>==</span><span> </span><span>poll_orgs</span><span>[[</span><span>4</span><span>]])</span><span>
</span><span>p</span><span>5</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>all_polls</span><span>,</span><span> </span><span>org</span><span> </span><span>==</span><span> </span><span>poll_orgs</span><span>[[</span><span>5</span><span>]])</span><span>
</span><span>d</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>
</span><span>mu_start</span><span> </span><span>=</span><span> </span><span>37.64</span><span>,</span><span>
</span><span>mu_finish</span><span> </span><span>=</span><span> </span><span>43.38</span><span>,</span><span>
</span><span>n_days</span><span> </span><span>=</span><span> </span><span>days_between_elections</span><span>,</span><span>
</span><span>y</span><span>1</span><span>_</span><span>values</span><span> </span><span>=</span><span> </span><span>p</span><span>1</span><span>$</span><span>ALP</span><span>,</span><span>
</span><span>y</span><span>1</span><span>_</span><span>days</span><span> </span><span>=</span><span> </span><span>p</span><span>1</span><span>$</span><span>MidDateNum</span><span>,</span><span>
</span><span>y</span><span>1</span><span>_</span><span>n</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>p</span><span>1</span><span>),</span><span>
</span><span>y</span><span>1</span><span>_</span><span>se</span><span> </span><span>=</span><span> </span><span>p</span><span>1</span><span>$</span><span>se_alp</span><span>,</span><span>
</span><span>y</span><span>2</span><span>_</span><span>values</span><span> </span><span>=</span><span> </span><span>p</span><span>2</span><span>$</span><span>ALP</span><span>,</span><span>
</span><span>y</span><span>2</span><span>_</span><span>days</span><span> </span><span>=</span><span> </span><span>p</span><span>2</span><span>$</span><span>MidDateNum</span><span>,</span><span>
</span><span>y</span><span>2</span><span>_</span><span>n</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>p</span><span>2</span><span>),</span><span>
</span><span>y</span><span>2</span><span>_</span><span>se</span><span> </span><span>=</span><span> </span><span>p</span><span>2</span><span>$</span><span>se_alp</span><span>,</span><span>
</span><span>y</span><span>3</span><span>_</span><span>values</span><span> </span><span>=</span><span> </span><span>p</span><span>3</span><span>$</span><span>ALP</span><span>,</span><span>
</span><span>y</span><span>3</span><span>_</span><span>days</span><span> </span><span>=</span><span> </span><span>p</span><span>3</span><span>$</span><span>MidDateNum</span><span>,</span><span>
</span><span>y</span><span>3</span><span>_</span><span>n</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>p</span><span>3</span><span>),</span><span>
</span><span>y</span><span>3</span><span>_</span><span>se</span><span> </span><span>=</span><span> </span><span>p</span><span>3</span><span>$</span><span>se_alp</span><span>,</span><span>
</span><span>y</span><span>4</span><span>_</span><span>values</span><span> </span><span>=</span><span> </span><span>p</span><span>4</span><span>$</span><span>ALP</span><span>,</span><span>
</span><span>y</span><span>4</span><span>_</span><span>days</span><span> </span><span>=</span><span> </span><span>p</span><span>4</span><span>$</span><span>MidDateNum</span><span>,</span><span>
</span><span>y</span><span>4</span><span>_</span><span>n</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>p</span><span>4</span><span>),</span><span>
</span><span>y</span><span>4</span><span>_</span><span>se</span><span> </span><span>=</span><span> </span><span>p</span><span>4</span><span>$</span><span>se_alp</span><span>,</span><span>
</span><span>y</span><span>5</span><span>_</span><span>values</span><span> </span><span>=</span><span> </span><span>p</span><span>5</span><span>$</span><span>ALP</span><span>,</span><span>
</span><span>y</span><span>5</span><span>_</span><span>days</span><span> </span><span>=</span><span> </span><span>p</span><span>5</span><span>$</span><span>MidDateNum</span><span>,</span><span>
</span><span>y</span><span>5</span><span>_</span><span>n</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>p</span><span>5</span><span>),</span><span>
</span><span>y</span><span>5</span><span>_</span><span>se</span><span> </span><span>=</span><span> </span><span>p</span><span>5</span><span>$</span><span>se_alp</span><span>
</span><span>)</span><span>
</span><span>system.time</span><span>({</span><span>
</span><span>stan_mod3</span><span> </span><span>&lt;-</span><span> </span><span>stan</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>'oz-polls-3.stan'</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>3</span><span>,</span><span>
</span><span>control</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>max_treedepth</span><span> </span><span>=</span><span> </span><span>15</span><span>,</span><span>
</span><span>adapt_delta</span><span> </span><span>=</span><span> </span><span>0.8</span><span>),</span><span>
</span><span>iter</span><span> </span><span>=</span><span> </span><span>4000</span><span>)</span><span>
</span><span>})</span><span> </span><span># about 600 seconds
</span><span>
</span><span>plot_results</span><span>(</span><span>stan_mod3</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>all_polls</span><span>,</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>ALP</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>org</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>geom_line</span><span>(</span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>mean</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</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>ggtitle</span><span>(</span><span>&quot;Voting intention for the ALP between the 2004 and 2007 Australian elections&quot;</span><span>,</span><span>
</span><span>&quot;Latent variable estimated with use of all major firms' polling data&quot;</span><span>)</span></code></pre>
</figure>
<h2>Estimates of polling house effects</h2>
<p>Here’s the house effects estimated by me with Stan, compared to those in Jackman’s 2009 book:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0102-compare-house-effects.svg"></p>
<p>Basically we got the same results – certainly close enough anyway. Jackman writes:</p>
<blockquote>
<p>“The largest effect is for the face-to-face polls conducted by Morgan; the point estimate of the house effect is 2.7 percentage points, which is very large relative to the classical sampling error accompanhying these polls.”</p>
</blockquote>
<p>Interestingly, Morgan’s phone polls did much better.</p>
<p>Here’s the code that did that comparison:</p>
<figure>
<pre><code data-lang="r"><span>house_effects</span><span> </span><span>&lt;-</span><span> </span><span>summary</span><span>(</span><span>stan_mod3</span><span>,</span><span> </span><span>pars</span><span> </span><span>=</span><span> </span><span>&quot;d&quot;</span><span>)</span><span>$</span><span>summary</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>round</span><span>(</span><span>2</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>org</span><span> </span><span>=</span><span> </span><span>poll_orgs</span><span>,</span><span>
</span><span>source</span><span> </span><span>=</span><span> </span><span>&quot;Stan&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>org</span><span>,</span><span> </span><span>mean</span><span>,</span><span> </span><span>`2.5%`</span><span>,</span><span> </span><span>`97.5%`</span><span>,</span><span> </span><span>source</span><span>)</span><span>
</span><span>jackman</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>org</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Galaxy&quot;</span><span>,</span><span> </span><span>&quot;Morgan, F2F&quot;</span><span>,</span><span> </span><span>&quot;Newspoll&quot;</span><span>,</span><span> </span><span>&quot;Nielsen&quot;</span><span>,</span><span> </span><span>&quot;Morgan, Phone&quot;</span><span>),</span><span>
</span><span>mean</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-1.2</span><span>,</span><span> </span><span>2.7</span><span>,</span><span> </span><span>1.2</span><span>,</span><span> </span><span>0.9</span><span>,</span><span> </span><span>0.8</span><span>),</span><span>
</span><span>`2.5%`</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-3.1</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>-0.5</span><span>,</span><span> </span><span>-0.8</span><span>,</span><span> </span><span>-1</span><span>),</span><span>
</span><span>`97.5%`</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.6</span><span>,</span><span> </span><span>4.3</span><span>,</span><span> </span><span>2.8</span><span>,</span><span> </span><span>2.5</span><span>,</span><span> </span><span>2.3</span><span>),</span><span>
</span><span>source</span><span> </span><span>=</span><span> </span><span>&quot;Jackman&quot;</span><span>
</span><span>)</span><span>
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>rbind</span><span>(</span><span>house_effects</span><span>,</span><span> </span><span>jackman</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>org</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>org</span><span>,</span><span> </span><span>mean</span><span>),</span><span>
</span><span>ypos</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>org</span><span>)</span><span> </span><span>+</span><span> </span><span>0.1</span><span> </span><span>-</span><span> </span><span>0.2</span><span> </span><span>*</span><span> </span><span>(</span><span>source</span><span> </span><span>==</span><span> </span><span>&quot;Stan&quot;</span><span>))</span><span> </span><span>d</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>ypos</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>source</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_segment</span><span>(</span><span>aes</span><span>(</span><span>yend</span><span> </span><span>=</span><span> </span><span>ypos</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>`2.5%`</span><span>,</span><span> </span><span>xend</span><span> </span><span>=</span><span> </span><span>`97.5%`</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>mean</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>5</span><span>,</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>d</span><span>$</span><span>org</span><span>),</span><span>
</span><span>minor_breaks</span><span> </span><span>=</span><span> </span><span>NULL</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.grid.major.y</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&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;House effect for polling firms, 95% credibility intervals\n(percentage points over-estimate of ALP vote)&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>colour</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;Source: Jackman's pscl R package; analysis at https://ellisp.github.io&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Polling 'house effects' in the leadup to the 2007 Australian election&quot;</span><span>,</span><span>
</span><span>&quot;Basically the same results in new analysis with Stan as in the original Jackman (2009)&quot;</span><span>)</span></code></pre>
</figure>
<p>So there we go – state space modelling of voting intention, with variable house effects, in the Australian 2007 federal election.</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/06/24/oz-polls-statespace"> 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/7Z33TnNz8bA" height="1"></a></div>