<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/web-app-for-individual-party-vote-from-the-2014-new-zealand-election-study/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/05/14/nzes-app"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://ellisp.github.io/blog/2017/05/06/nz-first">Last week</a> I posted some analysis of individual voting behaviour in New Zealand’s 2014 general election. In that post, I used logistic regression in four different models to predict the probability of an individual giving party vote to each of the four largest parties – National, Labour, Green and New Zealand First. That let the user compare the people voting for each of those parties, one at a time, to the wider population.</p>
<p>A logical extension of this is to model party vote for those four categories, plus “other” and “did not vote”, simultaneously as a multinomial response. I tried this out with several different methods: a deep learning neural network (from <a target="_blank" rel="nofollow" href="https://www.h2o.ai/"><code>H2O</code></a>, random forest (trying out both the <code>H2O</code> version and <a target="_blank" rel="nofollow" href="https://arxiv.org/pdf/1508.04409.pdf"><code>ranger</code>, a fast R/C++ implementation</a>), and multinomial log-linear regression (from <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=nnet"><code>nnet</code></a>). The aim was to produce an interactive web tool that lets people see the impact of changing one variable at a time on predicted voting probabilities:</p>
<p><a target="_blank" rel="nofollow" href="https://ellisp.shinyapps.io/individual-vote-nzes/"><img src="https://i1.wp.com/ellisp.github.io/img/0097-nzes3.gif?w=100%25" data-recalc-dims="1"></a></p>
<p>As per last week’s approach, I use about 20 explanatory variables in total with 2,835 observations. As my purpose was predictive analytics rather than structural inference, I dealt with the survey weighting by the brute force method of creating replicates of each row with the number of rows proportionate to their calibrated survey weight (on average 10 rows each). I added some noise to the data (as extra missing values for one variable per person) in the interest of regularising the predicted probabilities and used a variant of multiple imputation to deal with the missing data.</p>
<p>After playing around with tuning via the very convenient <code>h2o.grid</code> function, the best performing model was the neural network with two hidden layers of 60 neurons each and a high dropout rate between each layer. However, this was a bit slow for the end user when implemented in Shiny for the web app, and I anticipated some further problems in deploying an H2O model to <code>shinyapps.io</code> – problems I’ll address at some point, but not today. So in the end I used an average of the <code>ranger</code> random forest and the <code>nnet::multinom</code> multinomial regression models, which is nice and fast and gives very plausible results.</p>
<p>See:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="https://ellisp.shinyapps.io/individual-vote-nzes/">The web tool itself</a>;</li>
<li><a target="_blank" rel="nofollow" href="https://github.com/ellisp/ellisp.github.io/blob/source/_working/0097-predicting-vote.R">Source code for the preparation</a> for the Shiny app (I always separate out as much prep as possible from a Shiny app deployment, for ease of maintenance as well as faster user experience) and the various experiments in different models;</li>
<li><a target="_blank" rel="nofollow" href="https://github.com/ellisp/ellisp.github.io/tree/source/_working/0097">Source code for the app itself</a>.</li>
</ul>
<p>As usual, comments, suggestions and corrections are welcomed.</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/05/14/nzes-app"> Peter's stats stuff - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/2umxD412-a0"></a></div>