**Peter's stats stuff - R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Pooling the polls with Bayesian statistics

In an important 2005 article in the Australian Journal of Political Science, 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.

Jackman gives a worked example of the approach in his excellent book Bayesian Analysis for the Social Sciences, using voting intention for the Australian Labor Party (ALP) in the 2007 Australian federal election for data. He provides `JAGS`

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 `WinBUGS`

or `JAGS`

ā several days of CPU time on a fast computer in 2009. Jackman estimated his model with Gibbs sampling implemented directly in R.

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 election forecasts. 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 Stan because its Hamiltonian Monte Carlo 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.

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.

*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!*

## Basic approach ā estimated voting intention in the absence of polls

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:

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 rough application of this approach to the US Presidential election in 2016. Iāll be relaxing this assumption later.

Hereās the R code that sets up the session, brings in the data from Jackmanās `pscl`

R package, and defines a graphics function that Iāll be using for each model I create.

Hereās the Stan program that specifies this super simple model of changing ALP support from 2004 to 2007:

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 `*.stan`

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.

## Adding in one polling firm

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:

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 (`y_n`

), the values for each poll (`y_values`

), and the days since the last election each poll was taken (`y_days`

). 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.

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.

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.

## Including all five polling houses

Finally, the complete model replicating Jackmanās work:

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 `[0, 1]`

for that parameter, but I found this led to lots of estimation problems for Stan. The Stan developers give some great practical advice 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 `N(0.5, 0.5)`

, constrained to be positive.

Hereās the Stan program:

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.

Hereās the R code that sorts the data and passes it to Stan

## Estimates of polling house effects

Hereās the house effects estimated by me with Stan, compared to those in Jackmanās 2009 book:

Basically we got the same results ā certainly close enough anyway. Jackman writes:

ā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.ā

Interestingly, Morganās phone polls did much better.

Hereās the code that did that comparison:

So there we go ā state space modelling of voting intention, with variable house effects, in the Australian 2007 federal election.

**leave a comment**for the author, please follow the link and comment on their blog:

**Peter's stats stuff - R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.