<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/who-turned-out-to-vote-in-the-2014-new-zealand-general-election-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/07/13/voter-turnout"> 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>Motivation</h2>
<p>This <a rel="nofollow" href="https://twitter.com/ellis2013nz/status/884653816368422912" target="_blank">thread on Twitter</a> prompted some questions for me about who actually turns up to vote in New Zealand’s elections. With limited time, I can’t answer many of the important questions raised in that thread and the article it refers to critically. However, I can use the <a rel="nofollow" href="http://www.nzes.org/" target="_blank">New Zealand Election Study</a> to look into the specific question raised about the demographics of people on the electoral roll who fail to vote, and in particular if income is a significant factor.</p>
<p>To do this I adapted code from an <a rel="nofollow" href="http://ellisp.github.io/blog/2017/05/06/nz-first" target="_blank">earlier post</a> where I modelled party vote based on individual socio-economic characteristics. For the main analysis, I used “Voted” as the response variable in a regression model with about 20 possible explanatory variables; this lets us see the impact of each variable while simultaneously controlling for the others. Based on thinking that I was interested in behaviour rather than reasons, I wrapped up into one category those who claimed they “chose not to vote” and those who just “didn’t manage to vote”.</p>
<p>The NZES sample is drawn from the electoral roll, which means the important question of who gets enrolled in the first place can’t be analysed from it.</p>
<h2>Results</h2>
<p>To cut to the chase, it’s not <em>clear</em> that household income is a factor. However, there are so many people who did not tell the surveyors their income that in this analysis and in my earlier post I was obliged to code these people separately; in the chart below they show up as “HHIncome don’t know / NA”. Unlike the two “lower” and “higher” income groups they are contrasted to, this variable does show up as negatively related to likelihood to turn up to vote, even after controlling for all the other variables in the chart:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0104-model-results.svg"></p>
<p>To be clear on the interpretation of this chart, the following are characteristics for which there was significant evidence of a relationship with <em>more</em> likely to vote:</p>
<ul>
<li>Own their own house or flat</li>
<li>Someone in the household is a member of a professional association</li>
<li>Live in a city</li>
<li>Work part time (this is the only one that surprised me)</li>
<li>Has a university qualification</li>
<li>Older</li>
<li>Married or with a long term partner</li>
</ul>
<p>Generally speaking, these are mostly things associated with people who are doing well out of society.</p>
<p>The following factors are associated with being <em>less</em> likely to vote:</p>
<ul>
<li>Male</li>
<li>Young</li>
<li>Not European</li>
<li>Income not known (ie not told to the interviewer)</li>
</ul>
<p>I was interested that being Māori did <em>not</em> show up as significantly related to non-voting, above and beyond the general “non-European” factor (remembering that multiple ethnicities are usually allowed in New Zealand surveys and censuses). Assuming the chart in the Twitter thread referred to above is correct, this must mean that the Māori indicator is conflated with some of the other variables – such as being younger, not owning one’s own house or flat, not living in a city, not European, not having a university qualification, etc. I re-ran a regression, this time with “being Māori” as the response variable, to check that and found this conflation was indeed happening.</p>
<p>All up, this is pretty strong evidence for socio-economic disadvantage being a unifying factor in non-voting behaviour by people on the electoral roll. The fact that the “being Māori” non-voting effect disappears when we control for these other factors probably counts as a real finding of interest.</p>
<p>It’s a shame that we can’t see a clear income effect in itself (other than the people who don’t report income to surveyors), but income is notoriously difficult to measure in any social science context, so not that surprising.</p>
<h2>More exploration</h2>
<p>I poked around the data just a little more before writing up.</p>
<h3>More granular exploration of income</h3>
<p>Here is a mosaic plot of the original survey question on income matched to whether the respondent voted in the 2014 election:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0104-mosaic-dhhincome.svg"></p>
<p>Once one is familiar with these charts, they are a powerful way of visualising a two-way cross-tab. They are conceptually related to the basic Chi-square test used to test for independence of the two variables in a cross-tab like this. The cells coloured blue indicate a “surprisingly” high number of people in that cell, relative to the null hypothesis of no relationship between the two variables. Red means a surprisingly low number. The area of each box indicates the number of people in that particular cell of the table. For this “income by voting” plot we see:</p>
<ul>
<li>there are less people in the “don’t know income and did vote” category than would be expected if the two variables were unrelated</li>
<li>there are more people in the “don’t know income” and “chose not to vote” or “didn’t manage to vote” categories than would be expected</li>
<li>people with incomes between $31,000 and $55,000 turned up in “chose not to vote” more than would have been expected</li>
<li>people with incomes between $76,000 and $148,000 were less likely to be in “chose not to vote” than would have been expected</li>
</ul>
<p>All up, those exploratory findings broadly match an expectation that people with lower incomes didn’t vote, and those with higher incomes did; albeit with some complications in the detail.</p>
<p>This mosaic plot gives a more nuanced view of income than my regression, where I had to lump together categories from both variables. For example, a regression that differentiated between those who “chose not to vote” and those “didn’t manage to vote” would have been interesting but taken us into the world of multinomial responses which are extremely hard to explain visually, and which suck up more degrees of freedom from our fairly small sample size.</p>
<p>There’s not a lot of people in many of the cells in this table. With a bit more data, and a preparedness for some modelling complexity, I suspect we’d find an income effect somewhere. To tackle this seriously and with a big enough sample size I’d want to use all the election studies from previous years.</p>
<h3>Some attitudinal variables</h3>
<p>Here I present without comment some similar graphics comparing voting behaviour to some of the attitudinal questions in this survey:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0104-mosaic-dinterest.svg"><br>
<img width="100%" src="http://ellisp.github.io/img/0104-mosaic-ddiffvoting.svg"><br>
<img width="100%" src="http://ellisp.github.io/img/0104-mosaic-ddiffpower.svg"><br>
<img width="100%" src="http://ellisp.github.io/img/0104-mosaic-ddemo.svg"><br>
<img width="100%" src="http://ellisp.github.io/img/0104-mosaic-dbigbus.svg"></p>
<p>There’s lots to say here but to do it justice would require engaging much more with the political science literature than I have time for just now.</p>
<h2>Method</h2>
<p>Around a third of the 2,835 rows of data are missing at least one of the variables I wanted to include in my regression, so I needed to think carefully about my modelling strategy. Choosing a simpler variant of the different methods I tried in my <a rel="nofollow" href="http://ellisp.github.io/blog/2017/05/06/nz-first" target="_blank">earlier post on party vote</a>, I used:</p>
<ul>
<li>the survey weights provided by the NZES organizers</li>
<li>multiple imputations by chained equations (with the R <code>mice</code> package), imputing five alternative values for each missing value so we can fit five regressions and pool the results</li>
<li><code>glm</code> (with a quasi-binomial response to be safe) from the standard <code>stats</code> package with R, because it plays nicely with <code>mice</code> and my previous experience suggested there wasn’t much to gain by using <code>svyglm</code> from the <code>survey</code> package with this particular dataset.</li>
</ul>
<p>If I had more time and it was more important to me, I would have used <code>survey::svyglm</code> in combination with a bootstrap that encompasses the imputation process, as per the previous post. My experience suggests that this is unlikely to change the result materially.</p>
<h2>Code</h2>
<p>Here’s the R code that did the analysis. Two small points to note were that since my last post using this data, with the upgrade to R3.4.x,</p>
<ul>
<li>the <code>foreign</code> package seems to import the SPSS data slightly differently, which required a tweak to some of the code handling factors (on the plus side, I <em>think</em> it preserves more information from the SPSS version in doing so)</li>
<li>the <code>mice</code> package stores the contrasts for factors it used in imputation in a different spot</li>
</ul>
<p>Both these issues were food for thought and required a small amount of bug hunting.</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>foreign</span><span>)</span><span> </span><span># for importing SPSS data
</span><span>library</span><span>(</span><span>survey</span><span>)</span><span> </span><span># for survey weighting and analysis
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span> </span><span># for manipulating factor levels
</span><span>library</span><span>(</span><span>mice</span><span>)</span><span>
</span><span>#-------------convenience functions--------------
</span><span>camel_to_english</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>camelCase</span><span>){</span><span>
</span><span>return</span><span>(</span><span>gsub</span><span>(</span><span>&quot;([a-z])([A-Z])&quot;</span><span>,</span><span> </span><span>&quot;\\1 \\L\\2&quot;</span><span>,</span><span> </span><span>camelCase</span><span>,</span><span> </span><span>perl</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span><span>}</span><span>
</span><span># Convert five category membership question (for trade unions, business
# associations, etc) into Yes or No.
</span><span>membership</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>fct_recode</span><span>(</span><span>x</span><span>,</span><span>
</span><span>Yes</span><span> </span><span>=</span><span> </span><span>&quot;I belong, but no one else in the house does&quot;</span><span>,</span><span>
</span><span>Yes</span><span> </span><span>=</span><span> </span><span>&quot;I do, plus another in the house&quot;</span><span>,</span><span>
</span><span>Yes</span><span> </span><span>=</span><span> </span><span>&quot;Another person belongs, but not me&quot;</span><span>,</span><span>
</span><span>No</span><span> </span><span>=</span><span> </span><span>&quot;No, no one belongs&quot;</span><span>,</span><span>
</span><span>No</span><span> </span><span>=</span><span> </span><span>&quot;Don't know&quot;</span><span>)</span><span> </span><span>tmp</span><span> </span><span>&lt;-</span><span> </span><span>ifelse</span><span>(</span><span>tmp</span><span> </span><span>==</span><span> </span><span>&quot;Yes&quot;</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>0</span><span>)</span><span>
</span><span># Uncomment the next line if we want to turn NA into 0.
</span><span> </span><span># tmp &lt;-ifelse(is.na(tmp), 0, tmp)
</span><span> </span><span>return</span><span>(</span><span>tmp</span><span>)</span><span>
</span><span>}</span><span>
</span><span>#------------------data download-----------------
</span><span>
</span><span># Data downloaded from http://www.nzes.org/exec/show/data and because
# they want you to fill in a form to know who is using the data, I
# won't re-publish it myself
</span><span>unzip</span><span>(</span><span>&quot;D:/Downloads/NZES2014GeneralReleaseApril16.sav.zip&quot;</span><span>)</span><span>
</span><span>nzes_orig</span><span> </span><span>&lt;-</span><span> </span><span>read.spss</span><span>(</span><span>&quot;NZES2014GeneralReleaseApril16.sav&quot;</span><span>,</span><span> </span><span>to.data.frame</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>trim.factor.names</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>varlab</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>attributes</span><span>(</span><span>nzes_orig</span><span>)</span><span>$</span><span>variable.labels</span><span>)</span><span>
</span><span>#============rationalised version of feature creation===========
# This is a single longish command to aggregate various answers into
# simpler cateogrisations, because we don't have enough data to # analyse the original granular detail.
</span><span>nzes</span><span> </span><span>&lt;-</span><span> </span><span>nzes_orig</span><span> </span><span>%>%</span><span>
</span><span># Two degrees of freedom for ethnicity:
</span><span> </span><span># Oddly, this has changed since a few months ago. An upgrade to foreign (I think)
</span><span> </span><span># means that the SPSS data comes in with its labels. So this bit of code is slightly
</span><span> </span><span># different to some I'd previously blogged about:
</span><span> </span><span>mutate</span><span>(</span><span>NotEuropean</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>-</span><span> </span><span>(</span><span>dethnicity_e</span><span> </span><span>==</span><span> </span><span>&quot;NZ European&quot;</span><span>),</span><span>
</span><span>Maori</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>as.numeric</span><span>(</span><span>dethnicity_m</span><span>)</span><span> </span><span>==</span><span> </span><span>2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># Two degrees of freedom for income (lower, higher, don't know):
</span><span> </span><span>mutate</span><span>(</span><span>HHIncome</span><span> </span><span>=</span><span> </span><span>fct_recode</span><span>(</span><span>dhhincome</span><span>,</span><span>
</span><span>Lower</span><span> </span><span>=</span><span> </span><span>&quot;No income&quot;</span><span>,</span><span>
</span><span>Lower</span><span> </span><span>=</span><span> </span><span>&quot;$NZ23,000 or less&quot;</span><span>,</span><span>
</span><span>Lower</span><span> </span><span>=</span><span> </span><span>&quot;$NZ23,001-$NZ31,000&quot;</span><span>,</span><span>
</span><span>Lower</span><span> </span><span>=</span><span> </span><span>&quot;$NZ31,001-$NZ39,800&quot;</span><span>,</span><span>
</span><span>Lower</span><span> </span><span>=</span><span> </span><span>&quot;$NZ39,801-$NZ55,000&quot;</span><span>,</span><span>
</span><span>Higher</span><span> </span><span>=</span><span> </span><span>&quot;$NZ55,001-$NZ76,100&quot;</span><span>,</span><span>
</span><span>Higher</span><span> </span><span>=</span><span> </span><span>&quot;$NZ76,101-$NZ110,800&quot;</span><span>,</span><span>
</span><span>Higher</span><span> </span><span>=</span><span> </span><span>&quot;$NZ110,801-$NZ147,699&quot;</span><span>,</span><span>
</span><span>Higher</span><span> </span><span>=</span><span> </span><span>&quot;$NZ147,700 or over&quot;</span><span>,</span><span>
</span><span>`Don't know / NA`</span><span> </span><span>=</span><span> </span><span>&quot;Don't know&quot;</span><span>),</span><span>
</span><span>HHIncome</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>is.na</span><span>(</span><span>HHIncome</span><span>),</span><span> </span><span>&quot;Don't know / NA&quot;</span><span>,</span><span> </span><span>as.character</span><span>(</span><span>HHIncome</span><span>)),</span><span>
</span><span>HHIncome</span><span> </span><span>=</span><span> </span><span>fct_infreq</span><span>(</span><span>HHIncome</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>## Two - four degrees of freedom for associations?
</span><span> </span><span>mutate</span><span>(</span><span>HHMemberTradeUnion</span><span> </span><span>=</span><span> </span><span>membership</span><span>(</span><span>dtradeunion</span><span>),</span><span>
</span><span>HHMemberProfAssoc</span><span> </span><span>=</span><span> </span><span>membership</span><span>(</span><span>dprofassoc</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>## One degree of freedom for born in NZ
</span><span> </span><span>mutate</span><span>(</span><span>NZBorn</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>dnzborn</span><span> </span><span>==</span><span> </span><span>&quot;New Zealand&quot;</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>0</span><span>)</span><span>
</span><span># uncomment the next line if you want to turn NA into zero:
</span><span> </span><span># , NZBorn = ifelse(is.na(NZBorn), 0, NZBorn)
</span><span> </span><span>)</span><span> </span><span>%>%</span><span>
</span><span>## One for sex
</span><span> </span><span>mutate</span><span>(</span><span>Male</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>dsex</span><span> </span><span>==</span><span> </span><span>&quot;Male&quot;</span><span>),</span><span>
</span><span>Male</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>dsex</span><span> </span><span>==</span><span> </span><span>&quot;Transsexual or transgender&quot;</span><span>,</span><span> </span><span>NA</span><span>,</span><span> </span><span>Male</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>## Two-four for age
</span><span> </span><span>mutate</span><span>(</span><span>Age</span><span> </span><span>=</span><span> </span><span>fct_collapse</span><span>(</span><span>as.character</span><span>(</span><span>dage</span><span>),</span><span>
</span><span>`18-29`</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>18</span><span>:</span><span>29</span><span>),</span><span>
</span><span>`30-55`</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>30</span><span>:</span><span>55</span><span>),</span><span>
</span><span>`56+`</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>56</span><span>:</span><span>100</span><span>)),</span><span>
</span><span># Uncomment the next line if you want to explicitly code the NAs
</span><span> </span><span># Age = ifelse(is.na(dage), &quot;unknown&quot;, as.character(Age)),
</span><span> </span><span>Age</span><span> </span><span>=</span><span> </span><span>fct_relevel</span><span>(</span><span>Age</span><span>,</span><span> </span><span>&quot;30-55&quot;</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>## One for housing. Note there is also an alternative question &quot;do you or any family member own a residence&quot;
</span><span> </span><span>mutate</span><span>(</span><span>OwnHouseOrFlat</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>grepl</span><span>(</span><span>&quot;Own house or flat&quot;</span><span>,</span><span> </span><span>dhousing</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># Two for religion
</span><span> </span><span>mutate</span><span>(</span><span>Religion</span><span> </span><span>=</span><span> </span><span>fct_lump</span><span>(</span><span>dreligion</span><span>,</span><span> </span><span>2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># One for marital status
</span><span> </span><span>mutate</span><span>(</span><span>Marital</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>dmarital</span><span> </span><span>==</span><span> </span><span>&quot;Married, in a civil union, or living with a partner&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># One for self-identified class
</span><span> </span><span>mutate</span><span>(</span><span>IdentifyWorkingClass</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>dclass</span><span> </span><span>==</span><span> </span><span>&quot;Working class&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>## Two for education (University, None, Other)
</span><span> </span><span>mutate</span><span>(</span><span>HighestQual</span><span> </span><span>=</span><span> </span><span>fct_collapse</span><span>(</span><span>dedcons</span><span>,</span><span> </span><span>University</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Undergraduate Degree&quot;</span><span>,</span><span> </span><span>&quot;Postgraduate degree&quot;</span><span>,</span><span> </span><span>&quot;PhD&quot;</span><span>)),</span><span>
</span><span>HighestQual</span><span> </span><span>=</span><span> </span><span>fct_lump</span><span>(</span><span>HighestQual</span><span>,</span><span> </span><span>2</span><span>),</span><span>
</span><span>HighestQual</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>dedcons</span><span> </span><span>==</span><span> </span><span>&quot;Not known&quot;</span><span>,</span><span> </span><span>NA</span><span>,</span><span> </span><span>as.character</span><span>(</span><span>HighestQual</span><span>)),</span><span>
</span><span>HighestQual</span><span> </span><span>=</span><span> </span><span>fct_relevel</span><span>(</span><span>HighestQual</span><span>,</span><span> </span><span>&quot;Other&quot;</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>## Two for working status
</span><span> </span><span>mutate</span><span>(</span><span>WorkStatus</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>dwkpt</span><span>),</span><span> </span><span>&quot;Part time&quot;</span><span>,</span><span> </span><span>NA</span><span>),</span><span>
</span><span>WorkStatus</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>dwkft</span><span>),</span><span> </span><span>&quot;Full time&quot;</span><span>,</span><span> </span><span>WorkStatus</span><span>),</span><span>
</span><span>WorkStatus</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>is.na</span><span>(</span><span>WorkStatus</span><span>),</span><span> </span><span>&quot;Other or unknown&quot;</span><span>,</span><span> </span><span>WorkStatus</span><span>),</span><span>
</span><span>WorkStatus</span><span> </span><span>=</span><span> </span><span>fct_infreq</span><span>(</span><span>WorkStatus</span><span>),</span><span>
</span><span>Student</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>dwksch</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>## One for occupation
</span><span> </span><span>mutate</span><span>(</span><span>SuperviseAnyone</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>dsupervise</span><span> </span><span>==</span><span> </span><span>&quot;Yes&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># Note there is detailed occupation information (for respondent and partner)
</span><span> </span><span># but I don't think we hav eneough data to use this in the model.
</span><span> </span><span>## One degree of freedom for area lived in?
</span><span> </span><span># Five nice categories here, not enough data so we'll split into one
</span><span> </span><span>mutate</span><span>(</span><span>City</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>dregsize</span><span> </span><span>==</span><span> </span><span>&quot;A major city (over 100,000 population)&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Voted</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>*</span><span> </span><span>(</span><span>ddidvote</span><span> </span><span>==</span><span> </span><span>&quot;Did cast a vote&quot;</span><span>))</span><span>
</span><span># Mysterious Code 9 from outer space (in the original, see http://www.jackvowles.com/SectionD2014.htm)
</span><span>is.na</span><span>(</span><span>nzes</span><span>$</span><span>Voted</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>(</span><span>nzes</span><span>$</span><span>ddidvote</span><span> </span><span>==</span><span> </span><span>&quot;9&quot;</span><span>)</span><span>
</span><span>#==============descriptive stats==================
# create a survey design object that understand the weights:
</span><span>nzes_svy</span><span> </span><span>&lt;-</span><span> </span><span>svydesign</span><span>(</span><span>~</span><span>1</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>~</span><span>dwtfin</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>nzes</span><span>)</span><span>
</span><span>#' Function to estimate a survey cross tab and draw a mosaic plot, comparing
#' a given variable to whether the person voted (excluding those who said &quot;9&quot;
#' in response to the question on whether they voted)
</span><span>mp</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>variable</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>){</span><span>
</span><span>form</span><span> </span><span>&lt;-</span><span> </span><span>as.formula</span><span>(</span><span>paste</span><span>(</span><span>&quot;~&quot;</span><span>,</span><span> </span><span>variable</span><span>,</span><span> </span><span>&quot;+ ddidvote&quot;</span><span>))</span><span>
</span><span>tab</span><span> </span><span>&lt;-</span><span> </span><span>svytable</span><span>(</span><span>form</span><span>,</span><span> </span><span>nzes_svy</span><span>)</span><span>
</span><span>oldpar</span><span> </span><span>&lt;-</span><span> </span><span>par</span><span>(</span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>mosaicplot</span><span>(</span><span>t</span><span>(</span><span>tab</span><span>[</span><span> </span><span>,</span><span> </span><span>-4</span><span>]),</span><span> </span><span>shade</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>las</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;New Zealand Election Study 2014&quot;</span><span>,</span><span>
</span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;D2: Did you vote or not vote?&quot;</span><span>,</span><span>
</span><span>ylab</span><span> </span><span>=</span><span> </span><span>ylab</span><span>)</span><span>
</span><span>par</span><span>(</span><span>oldpar</span><span>)</span><span>
</span><span>}</span><span>
</span><span># Draw the mosaic plots used in the post:
</span><span>mp</span><span>(</span><span>&quot;dinterest&quot;</span><span>,</span><span> </span><span>&quot;A1: how interested in politics&quot;</span><span>)</span><span>
</span><span>mp</span><span>(</span><span>&quot;ddiffpower&quot;</span><span>,</span><span> </span><span>&quot;A10: does it make a difference who is in power&quot;</span><span>)</span><span>
</span><span>mp</span><span>(</span><span>&quot;ddemo&quot;</span><span>,</span><span> </span><span>&quot;A12: how satisfied with how democracy works in NZ&quot;</span><span>)</span><span>
</span><span>mp</span><span>(</span><span>&quot;ddiffvoting&quot;</span><span>,</span><span> </span><span>&quot;A11: does voting make any difference to what happens&quot;</span><span>)</span><span>
</span><span>mp</span><span>(</span><span>&quot;dbigbus&quot;</span><span>,</span><span> </span><span>&quot;C7m: big business in NZ has too much power&quot;</span><span>)</span><span>
</span><span>mp</span><span>(</span><span>&quot;dhhincome&quot;</span><span>,</span><span> </span><span>&quot;F26: household income between 1 April 2013 and 31 March 2014&quot;</span><span>)</span><span>
</span><span># too much missing data - nearly a third of rows missing a column - for this to be any use, but as a taster:
</span><span>mod1</span><span> </span><span>&lt;-</span><span> </span><span>svyglm</span><span>(</span><span>Voted</span><span> </span><span>~</span><span> </span><span>NotEuropean</span><span> </span><span>+</span><span> </span><span>Maori</span><span> </span><span>+</span><span> </span><span>HHIncome</span><span> </span><span>+</span><span> </span><span>HHMemberTradeUnion</span><span> </span><span>+</span><span> </span><span>HHMemberProfAssoc</span><span> </span><span>+</span><span>
</span><span>NZBorn</span><span> </span><span>+</span><span> </span><span>Male</span><span> </span><span>+</span><span> </span><span>Age</span><span> </span><span>+</span><span> </span><span>OwnHouseOrFlat</span><span> </span><span>+</span><span> </span><span>Religion</span><span> </span><span>+</span><span> </span><span>Marital</span><span> </span><span>+</span><span>
</span><span>IdentifyWorkingClass</span><span> </span><span>+</span><span> </span><span>HighestQual</span><span> </span><span>+</span><span> </span><span>WorkStatus</span><span> </span><span>+</span><span> </span><span>Student</span><span> </span><span>+</span><span> </span><span>SuperviseAnyone</span><span> </span><span>+</span><span> </span><span>City</span><span>,</span><span> </span><span>design</span><span> </span><span>=</span><span> </span><span>nzes_svy</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;quasibinomial&quot;</span><span>)</span><span>
</span><span>summary</span><span>(</span><span>mod1</span><span>)</span><span>
</span><span>anova</span><span>(</span><span>mod1</span><span>)</span><span>
</span><span>#============modelling with imputation===============
# create a dataset of just the variables we're using:
</span><span>nzes_subset</span><span> </span><span>&lt;-</span><span> </span><span>nzes</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>Voted</span><span>,</span><span> </span><span>NotEuropean</span><span>,</span><span> </span><span>Maori</span><span>,</span><span> </span><span>HHIncome</span><span>,</span><span> </span><span>HHMemberTradeUnion</span><span>,</span><span>
</span><span>HHMemberProfAssoc</span><span>,</span><span> </span><span>NZBorn</span><span>,</span><span> </span><span>Male</span><span>,</span><span> </span><span>Age</span><span>,</span><span> </span><span>OwnHouseOrFlat</span><span>,</span><span>
</span><span>Religion</span><span>,</span><span> </span><span>Marital</span><span>,</span><span> </span><span>IdentifyWorkingClass</span><span>,</span><span> </span><span>HighestQual</span><span>,</span><span> </span><span>WorkStatus</span><span>,</span><span>
</span><span>Student</span><span>,</span><span> </span><span>SuperviseAnyone</span><span>,</span><span> </span><span>City</span><span>,</span><span> </span><span>dwtfin</span><span>)</span><span>
</span><span># check how many complete cases there are:
</span><span>sum</span><span>(</span><span>complete.cases</span><span>(</span><span>nzes_subset</span><span>))</span><span>
</span><span>nrow</span><span>(</span><span>nzes_subset</span><span>)</span><span>
</span><span># create 5 different datasets with (different) imputed values where missing
</span><span>nzes_imp</span><span> </span><span>&lt;-</span><span> </span><span>mice</span><span>(</span><span>nzes_subset</span><span>)</span><span>
</span><span># remove contrasts to make it easier to interpret once the model is fit
# note that these contrasts are now in the $data object - some time in the # last year this changed, compared to what worked in my 6 May 2017 blog.
</span><span>attributes</span><span>(</span><span>nzes_imp</span><span>$</span><span>data</span><span>$</span><span>Religion</span><span>)</span><span>$</span><span>contrasts</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>attributes</span><span>(</span><span>nzes_imp</span><span>$</span><span>data</span><span>$</span><span>WorkStatus</span><span>)</span><span>$</span><span>contrasts</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>attributes</span><span>(</span><span>nzes_imp</span><span>$</span><span>data</span><span>$</span><span>HHIncome</span><span>)</span><span>$</span><span>contrasts</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>attributes</span><span>(</span><span>nzes_imp</span><span>$</span><span>data</span><span>$</span><span>HighestQual</span><span>)</span><span>$</span><span>contrasts</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>attributes</span><span>(</span><span>nzes_imp</span><span>$</span><span>data</span><span>$</span><span>Age</span><span>)</span><span>$</span><span>contrasts</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span># fit model
</span><span>mod2</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>nzes_imp</span><span>,</span><span> </span><span>glm</span><span>(</span><span>Voted</span><span> </span><span>~</span><span> </span><span>NotEuropean</span><span> </span><span>+</span><span> </span><span>Maori</span><span> </span><span>+</span><span> </span><span>HHIncome</span><span> </span><span>+</span><span> </span><span>HHMemberTradeUnion</span><span> </span><span>+</span><span> </span><span>HHMemberProfAssoc</span><span> </span><span>+</span><span>
</span><span>NZBorn</span><span> </span><span>+</span><span> </span><span>Male</span><span> </span><span>+</span><span> </span><span>Age</span><span> </span><span>+</span><span> </span><span>OwnHouseOrFlat</span><span> </span><span>+</span><span> </span><span>Religion</span><span> </span><span>+</span><span> </span><span>Marital</span><span> </span><span>+</span><span>
</span><span>IdentifyWorkingClass</span><span> </span><span>+</span><span> </span><span>HighestQual</span><span> </span><span>+</span><span> </span><span>WorkStatus</span><span> </span><span>+</span><span> </span><span>Student</span><span> </span><span>+</span><span> </span><span>SuperviseAnyone</span><span> </span><span>+</span><span> </span><span>City</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;quasibinomial&quot;</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>dwtfin</span><span>))</span><span>
</span><span># turn into a graphic:
</span><span>coefs</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>summary</span><span>(</span><span>pool</span><span>(</span><span>mod2</span><span>)))</span><span>
</span><span>coefs</span><span>$</span><span>variable</span><span> </span><span>&lt;-</span><span> </span><span>camel_to_english</span><span>(</span><span>row.names</span><span>(</span><span>coefs</span><span>))</span><span>
</span><span>coefs</span><span> </span><span>%>%</span><span>
</span><span># remove intercept as uninteresting:
</span><span> </span><span>slice</span><span>(</span><span>-1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># reorder:
</span><span> </span><span>mutate</span><span>(</span><span>variable</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>variable</span><span>,</span><span> </span><span>t</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># draw graphic:
</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>`lo 95`</span><span>,</span><span> </span><span>xend</span><span> </span><span>=</span><span> </span><span>`hi 95`</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>variable</span><span>,</span><span> </span><span>yend</span><span> </span><span>=</span><span> </span><span>variable</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_vline</span><span>(</span><span>xintercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2.5</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;orange&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_segment</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Who voted in New Zealand's Election 2014?&quot;</span><span>,</span><span>
</span><span>&quot;Of those enrolled to vote, when controlling for other variables, which variables were associated with voting.
Width of blue bars indicates uncertainty associated with sampling and imputation.&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;Less likely to vote ----------------- More likely to vote &quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Compared to enrollees of no religion, aged 30-55, high household income, school qualification, working full time&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: New Zealand Election Study; analysis at http://ellisp.github.io&quot;</span><span>)</span><span>
</span><span>#=================who is Maori=======================
</span><span>mod3</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>nzes_imp</span><span>,</span><span> </span><span>glm</span><span>(</span><span>Maori</span><span> </span><span>~</span><span> </span><span>NotEuropean</span><span> </span><span>+</span><span> </span><span>HHIncome</span><span> </span><span>+</span><span> </span><span>HHMemberTradeUnion</span><span> </span><span>+</span><span> </span><span>HHMemberProfAssoc</span><span> </span><span>+</span><span>
</span><span>NZBorn</span><span> </span><span>+</span><span> </span><span>Male</span><span> </span><span>+</span><span> </span><span>Age</span><span> </span><span>+</span><span> </span><span>OwnHouseOrFlat</span><span> </span><span>+</span><span> </span><span>Religion</span><span> </span><span>+</span><span> </span><span>Marital</span><span> </span><span>+</span><span>
</span><span>IdentifyWorkingClass</span><span> </span><span>+</span><span> </span><span>HighestQual</span><span> </span><span>+</span><span> </span><span>WorkStatus</span><span> </span><span>+</span><span> </span><span>Student</span><span> </span><span>+</span><span> </span><span>SuperviseAnyone</span><span> </span><span>+</span><span> </span><span>City</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;quasibinomial&quot;</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>dwtfin</span><span>))</span><span>
</span><span>coefs3</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>summary</span><span>(</span><span>pool</span><span>(</span><span>mod3</span><span>)))</span><span>
</span><span>coefs3</span><span>$</span><span>variable</span><span> </span><span>&lt;-</span><span> </span><span>camel_to_english</span><span>(</span><span>row.names</span><span>(</span><span>coefs3</span><span>))</span><span>
</span><span>coefs3</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>t</span><span>)</span></code></pre>
</figure>
	<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/07/13/voter-turnout"> 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/pemIswzGRGs" height="1"></a></div>