<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/the-code-and-other-stuff/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://gianlubaio.blogspot.com/2017/06/the-code-and-other-stuff.html"> Gianluca Baio's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div><a rel="nofollow" href="https://i2.wp.com/2.bp.blogspot.com/-i5PnLOfMsiM/WTE9gejxSrI/AAAAAAAAHro/zrVGcRnXcIokpzLkjsd2TxHk7m71baU0ACLcB/s1600/download.jpg?ssl=1" target="_blank"><img border="0" data-original-height="182" src="https://i2.wp.com/2.bp.blogspot.com/-i5PnLOfMsiM/WTE9gejxSrI/AAAAAAAAHro/zrVGcRnXcIokpzLkjsd2TxHk7m71baU0ACLcB/s1600/download.jpg?resize=277%2C182&ssl=1" data-recalc-dims="1" data-original-width="277"></a></div>
<p><span>I’ve received a couple of emails or comments on one of the General Election posts to ask me to share the code I’ve used. </span><br><span><br></span><span>In general, I think this is a bit dirty and lots could be done in a more efficient way $-$ effectively, I’m doing this out of my own curiosity and while I think the model is sensible, it’s probably not “publication-standard” (in terms of annotation etc).</span><br><span><br></span><span>Anyway, I’ve created a (rather plain) <a rel="nofollow" href="https://github.com/giabaio/Election2017/tree/master" target="_blank">GitHub</a> repository, which contains the basic files (including R script, R functions, basic data and <a rel="nofollow" href="http://mcmc-jags.sourceforge.net/" target="_blank">JAGS</a> model). Given time (which I’m not given…), I’d like to put a lot more description and perhaps also write a <a rel="nofollow" href="http://mc-stan.org/interfaces/rstan" target="_blank">Stan</a> version of the model code. I could also write a more precise model description $-$ I’ll try to update the material on the GitHub.</span><br><span><br></span><span>On another note, the previous posts have been syndicated in a couple of places (<a rel="nofollow" href="http://www.ucl.ac.uk/general-election" target="_blank">here</a> and <a rel="nofollow" href="https://www.significancemagazine.com/politics/551-predicting-the-uk-s-snap-general-election" target="_blank">here</a>), which was nice. And finally, here’s a little update with the latest data. As of today, the model predicts the following seats distribution.</span><br><span><br></span><span>                mean        sd 2.5% median 97.5%</span><br><span>Conservative 352.124 3.8760350  345    352   359</span><br><span>Labour       216.615 3.8041091  211    217   224</span><br><span>UKIP           0.000 0.0000000    0      0     0</span><br><span>Lib Dem       12.084 1.8752228    8     12    16</span><br><span>SNP           49.844 1.8240041   45     51    52</span><br><span>Green          0.000 0.0000000    0      0     0</span><br><span>PCY            1.333 0.9513233    0      2     3</span><br><span>Other          0.000 0.0000000    0      0     0</span>
<div></div>
<p><span>Labour are still slowly but surely gaining some ground $-$ I’m not sure the effect of the <a rel="nofollow" href="http://www.bbc.co.uk/news/election-2017-40113827" target="_blank">debate</a> earlier this week (which was deserted by the PM) are visible yet as only a couple of the polls included were conducted after that.</span><br><span><br>Another interesting thing (following up on <a rel="nofollow" href="https://gianlubaio.blogspot.co.uk/2017/05/the-swingers.html" target="_blank">this</a> post) is the analysis of the marginal seats that the model predicts to swing from the 2015 Winners. I’ve updated the plot, which now looks as below.</span><br><span><br></span>
<div><a rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-veHe7Y4z2P8/WTFAn8m72tI/AAAAAAAAHr0/xidGjD8eW4EAtczzleQC8pq9wBnflAAkQCLcB/s1600/Swingers.png?ssl=1" target="_blank"><img border="0" data-original-height="800" src="https://i2.wp.com/3.bp.blogspot.com/-veHe7Y4z2P8/WTFAn8m72tI/AAAAAAAAHr0/xidGjD8eW4EAtczzleQC8pq9wBnflAAkQCLcB/s640/Swingers.png?resize=450%2C800&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p><span>Now there are 30 constituencies that are predicted to change hand, many still towards the Tories. I am not a political scientists, so I don’t really know all the ins and outs of these, but I think a couple of examples are quite interesting and I would venture some comment…</span><br><span><br></span><span>So, the model doesn’t know about the recent by-elections of <a rel="nofollow" href="http://www.bbc.co.uk/news/uk-politics-39064149" target="_blank">Copeland and Stoke-on-Trent South</a> and so still label these seats as “Labour” (as they were in 2015), although the Tories have actually now control of Copeland.
<p>In the prediction given the polls and the impact of the EU referendum (both were strong Leave areas with with 60% and 70% of the preference, respectively) and the Tories did well in 2015 (36% vs Labour’s 42% in Copeland and 33% to Labour’s 39% in 2015). So, the model is suggesting that both are likely to switch to the Tories this time around.</p>
<p>In fact, we know that at the time of the by-election, while Copeland (where the contest was mostly Labour v Tories) did go blue, Stoke didn’t. But there, the main battle was between the Labour’s and the UKIP’s candidate (UKIP had got 21% in 2015). And the by-election was fought last February, when the Tories lead was much more robust that it probably is now.<br><span><br>Another interesting area is Twickenham $-$ historically a constituency leaning to the Lib Dems, <a rel="nofollow" href="http://www.cityam.com/215381/general-election-2015-vince-cable-loses-twickenham-conservatives-lib-dem-bloodbath" target="_blank">which was captured by the Conservatives in 2015</a>. But since then, in another by-election the Tories have lost another similar area (<a rel="nofollow" href="http://www.bbc.co.uk/news/uk-politics-38178486" target="_blank">Richmond Park</a>,with a massive swing) and the model is suggesting that Twickenham could follow suit, come next Thursday. </span><br><span><br></span><span>Finally, Clapton was the only seat won by UKIP in 2015, but since then, the elected MP (a former Tory-turned-UKIP) <a rel="nofollow" href="https://www.theguardian.com/politics/2017/apr/20/former-ukip-mp-douglas-carswell-to-step-down-in-clacton" target="_blank">has defected the party</a> and is not contesting the seat. This, combined with the poor standing of UKIP in the polls produces the not surprisingly outcome that Clapton is predicted to go blue with basically no uncertainty…</span><br><span><br></span><span>These results look reasonable to me $-$ not sure how life will turn out of course. As many commentators <a rel="nofollow" href="https://www.theguardian.com/politics/2017/jun/01/how-pollsters-deal-with-turnout-rates-is-key-to-tories-reduced-lead" target="_blank">have noted</a> much may depend on the turn out among the younger. Or other factors. And probably there’ll be another instance of the “<a rel="nofollow" href="https://en.wikipedia.org/wiki/Shy_Tory_Factor" target="_blank">Shy-Tory effect</a>” (I’ll think about this if I get some time before the final prediction). But the model does seem to make some sense…</span></p></span>
	<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://gianlubaio.blogspot.com/2017/06/the-code-and-other-stuff.html"> Gianluca Baio's blog</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/CLlAT8Yj7n0" height="1"></a></div>