<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/more-things-with-the-new-zealand-election-study-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/08/20/nzes-so-far"> 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>A new cross tab tool</h2>
<p>I recently put up a simple web app, built with R Shiny, to let users explore the relationship between party vote in the 2014 New Zealand general election and a range of demographic and attitudinal questions in the <a rel="nofollow" href="http://www.nzes.org/" target="_blank">New Zealand Election Study</a>. The image below is a link to the web app:</p>
<p><a rel="nofollow" href="https://ellisp.shinyapps.io/nzes2014_x_by_party/" target="_blank"><img src="https://i1.wp.com/ellisp.github.io/img/0108-screenshot-crosstabs-nzes.png?w=100%25" data-recalc-dims="1"></a></p>
<p>The original motivation was to answer a <a rel="nofollow" href="https://twitter.com/FoxyLustyGrover/status/892263331343384576" target="_blank">question on Twitter</a> for a breakdown of National party vote by gender. I was surprised how interesting I found the resulting tool though. Without a fancy graphic, just a table of numbers, there’s a lot to play around with here. I deliberately kept the functionality narrow, because I wanted to avoid a bewildering array of choices and confusing user interface, so it tries to do only one thing and does it well. The thing it does is show cross tabs of party vote with other variables from the study.</p>
<p>The <a rel="nofollow" href="https://github.com/ellisp/ellisp.github.io/tree/source/_working/0108" target="_blank">source code of the Shiny app</a> is available as is the <a rel="nofollow" href="https://github.com/ellisp/ellisp.github.io/blob/source/_working/0108-nzes-prep.R" target="_blank">preparation script</a> but they’re quite unremarkable so I won’t reproduce them here; follow the links and read them on GitHub in their natural habitat.</p>
<p>A few interesting statistical points to note:</p>
<ul>
<li>I produced a new set of weights so population totals would match the actual party vote. Even after the NZES team did their weighting, the sample wasn’t representative of the population of people on the electoral roll in terms of actual party vote. In fact, people who did <em>not</em> vote were particularly over-represented. This isn’t that surprising – people who don’t vote for whatever reason (whether it is apathy or being out of the country and busy with other things) are probably also disproportionately likely not to respond to surveys. The web app gives the user the choice of the original NZES weights or my re-calibrated ones, defaulting to the latter. I think that’s useful because people might use the app to say “X thousand voters for party Y have Z attitude”, so adding up to the right number of voters by party is important.</li>
<li>I included an option to see the Pearson residuals, which compare the observed cell count with what would have been expected in the (nearly always implausible) null hypothesis of no relationship at all between the two variables making up the cross tab. I think this is by far the best way to look at which cells of the table are unusual. For example, in the screenshot above, it is highlighted clearly that National voters had unusually strong levels of agreement with the statement “with lower welfare benefits people would learn to stand on their own two feet”, whereas voters for Labour and the Greens were unusually <em>unlikely</em> to agree with that statement (and likely to disagree). This won’t be a surprise for any watchers of New Zealand politics.</li>
<li>One version I tested had a little Chi square test of the null hypothesis of no relationship between the two. But it was nearly always returning a p value of zero, because of course there is a relationship between these variables. I decided it was uninteresting, and didn’t want to focus people on null hypothesis testing anyway, so left it out.</li>
<li>I resisted the urge to put multiple numbers in each cell of the table, as is done in some stats package output (eg SPSS). I think tables like these work as visualisations if the eye can sweep across, knowing that every number in the table is somehow comparable. This isn’t possible when you combine values in each cell (eg include both row-wise and column-wise percentages).</li>
<li>It was interesting to think through what should be the default way of calculating percentages in a table like this. I decided in the end to default to row-wise, which means the user is reading (for example) “Of the people who voted X, what percentage thought Y?” I don’t think there’s a right or wrong, just a contingent guess that this is most likely to be the first want of people.</li>
<li>An early version of the tool had an option for “margin of error” for each cell and this drew my attention to the difficult of conceptualising the margin of error in a single cell of a contingency table. I’m going to think more about this one.</li>
<li>Adding the heatmap colour was a late addition, made easy by the wonder of the easy combination of <code>datatable</code> JavaScript with R via the <code>DT</code> package.</li>
</ul>
<h2>More stuff using the New Zealand Election Study</h2>
<p>So I now have two web apps with this data:</p>
<ul>
<li><a rel="nofollow" href="https://ellisp.shinyapps.io/individual-vote-nzes/" target="_blank">Predict party vote given a combination of demographic characteristics</a></li>
<li><a rel="nofollow" href="https://ellisp.shinyapps.io/nzes2014_x_by_party/" target="_blank">Cross tab of your choice of variable with party vote</a></li>
</ul>
<p>… and six blog posts. To recap, here’s all the blog posts I’ve done so far with this data:</p>
<h3><a rel="nofollow" href="http://ellisp.github.io/blog/2016/09/18/nzes1" target="_blank">1. Attitudes to the “Dirty Politics” book</a></h3>
<p>In my first post on the data, I did quick demo analysis of what the attitudes were of voters for various parties to Nicky Hagar’s book “Dirty Politics”</p>
<p><img width="100%" src="http://ellisp.github.io/img/0057-hagar.svg"></p>
<h3><a rel="nofollow" href="http://ellisp.github.io/blog/2017/05/06/nz-first" target="_blank">2. Modelling individual level party vote</a></h3>
<p>I did some reasonably comprehensive modelling of who votes for whom. The main work here was deciding how many degrees of freedom could be spared for the various demographic variables, and clumping/tidying them up into analysis-ready form. This was also a good opportunity for some thinking about modelling strategy, the role of the bootstrap, and multiple imputation which is essential with this sort of problem.</p>
<p><img width="100%" src="http://ellisp.github.io/img/0096-all-parties.svg"></p>
<h3><a rel="nofollow" href="http://ellisp.github.io/blog/2017/05/14/nzes-app" target="_blank">3. Web app for individual vote</a></h3>
<p>This led to my first web app with the New Zealand Election Study data, which lets you explore the predicted probability of different types of people voting for different parties.</p>
<p><a rel="nofollow" href="https://ellisp.shinyapps.io/individual-vote-nzes/" target="_blank"><img src="https://i1.wp.com/ellisp.github.io/img/0097-nzes3.gif?w=100%25" data-recalc-dims="1"></a></p>
<h3><a rel="nofollow" href="http://ellisp.github.io/blog/2017/05/21/nzes-sankey" target="_blank">4. Sankey chart of ‘transitions’ from 2011 vote to 2014</a></h3>
<p>This was an interesting experiment in looking at what one survey can tell us about people swapping from party to party:</p>
<p><iframe width="450" src="http://ellisp.github.io/img/0098-sankey.html"></iframe></p>
<h3><a rel="nofollow" href="http://ellisp.github.io/blog/2017/07/13/voter-turnout" target="_blank">5. Modelling voter turnout</a></h3>
<p>I adapted my approach of modelling party vote to the perhaps even more important question of who turns out to vote at all.</p>
<p><img width="100%" src="http://ellisp.github.io/img/0104-model-results.svg"></p>
<h3><a rel="nofollow" href="http://ellisp.github.io/blog/2017/08/20/nzes-so-far" target="_blank">6. Cross tab tool</a></h3>
<p>The sixth blog post is today’s.</p>
<p><a rel="nofollow" href="https://ellisp.shinyapps.io/nzes2014_x_by_party/" target="_blank"><img src="https://i1.wp.com/ellisp.github.io/img/0108-screenshot-crosstabs-nzes.png?w=100%25" data-recalc-dims="1"></a></p>
<p>For New Zealand readers, have a good final five weeks up to the 2017 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/08/20/nzes-so-far"> 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/YV1jn8Aaz98" height="1"></a></div>