<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-cluster-of-six/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://thinkr.biz/2018/01/29/cluster-of-six/"> R – thinkr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Unsupervised machine learning</h2>
<p><a rel="nofollow" href="https://hansard.parliament.uk/" target="_blank">Hansard</a> reports what’s said in the UK Parliament, sets out details of divisions, and records decisions taken during a sitting. The <a rel="nofollow" href="https://cran.r-project.org/web/packages/hansard/index.html" target="_blank">hansard R package</a> provides functions to import its data.</p>
<p>Using the Hansard API (Application Programming Interface), we’ll apply unsupervised machine learning to analyze the voting patterns of 219 Labour Members of Parliament (MPs). We’ll consider all divisions (results of the votes) in the UK House of Commons since the 2017 general election.<span></span></p>
<p><i>Supervised</i> machine learning makes predictions from labeled training data. The <i>unsupervised</i> flavour looks for hidden structure in “unlabeled” data, i.e. a classification or categorisation not included in the observations. <a rel="nofollow" href="https://en.wikipedia.org/wiki/Hierarchical_clustering" target="_blank">Hierarchical clustering</a> will identify a cluster of six MPs as the most “distant” from the wider party.</p>
<p>The full methodology, including the code, is <span><strong><a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/01/Labour_voting_html.html" target="_blank">published here</a></strong></span>. This extended narrative confirms the suitability of the data for clustering; reviews eight clustering methods for optimal fit; plots the full dendrogram of 219 Labour MPs; and rationalises the outcome in more detail, for example, using <a rel="nofollow" href="https://en.wikipedia.org/wiki/Cook%27s_distance" target="_blank">Cook’s Distance</a>.</p>
<p>We’ll set a vote of “aye” to 1, and “no” to -1. And we’ll treat non-votes as 0. Voting the opposite way to the majority of the party, as well as non-votes, will be of interest when assessing which MPs are “most distant” from the majority.</p>
<h2>The “cluster of six”</h2>
<p>We’ll apply a “bottom up” clustering approach. Each MP starts in their own cluster, and pairs of clusters are progressively combined until none remains.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2018/01/Plot-cluster-1.png?w=450&ssl=1" alt="" class="wp-image-552" data-recalc-dims="1"></p>
<p>What we find is the above cluster of six MPs who, based on their voting patterns, are the last to merge with the wider party.</p>
<h2>Does this cluster make sense?</h2>
<p>Can we rationalize why machine learning has isolated this cluster? If we inspect the ten MPs recording the fewest votes since June 8th, 2017, it does include all six.</p>
<p><img alt="" src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2018/01/fewest10.png?w=450&ssl=1" class="wp-image-544" data-recalc-dims="1"></p>
<p>Nonetheless, non-voting will not be the only influencing factor. A small minority of MPs voting the opposite way to the overwhelming majority will influence the “distant cluster”.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2018/01/Outliers-1.png?w=450&ssl=1" alt="" class="wp-image-547" data-recalc-dims="1"></p>
<p>Cook’s Distance visualizes these influential outliers. It shows the voting of three MPs, all on the European Union Withdrawal Bill readings, to be particular outliers. All three MPs are in the “cluster of six”.</p>
<h2>Conclusion</h2>
<p>So, in summary, we established that the data is suitable for hierarchical clustering and selected the clustering method that best fits the data. We identified a “cluster of six” MPs who are the last to combine with the rest of the party. And, by inspecting the non-votes and most influential outliers, we can rationalize why unsupervised machine learning merged these MPs last.</p>
<h2>R toolkit</h2>
<div>
<table>
<thead>
<tr>
<th> </th>
<th><strong>Packages</strong></th>
<th><strong>Functions</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2017/10/Program-e1509478672361.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-135" data-recalc-dims="1" height="100"></td>
<td>purrr</td>
<td>map_df; pmap; map_lgl; transpose; safely</td>
</tr>
<tr>
<td><img src="https://i0.wp.com/thinkr.biz/wp-content/uploads/2017/10/Import-e1509478685299.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-134" data-recalc-dims="1" height="100"></td>
<td>hansard</td>
<td>commons_members; mp_vote_record</td>
</tr>
<tr>
<td rowspan="7"><img src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2017/10/Transform-e1509478657749.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-136" data-recalc-dims="1" height="100"></td>
<td>dplyr</td>
<td>mutate; select; if_else; left_join; everything; summarise; group_by; arrange</td>
</tr>
<tr>
<td>tidyr</td>
<td>spread; gather</td>
</tr>
<tr>
<td>broom</td>
<td>augment</td>
</tr>
<tr>
<td>stringr</td>
<td>str_replace; str_c</td>
</tr>
<tr>
<td>lubridate</td>
<td>month; day; year</td>
</tr>
<tr>
<td>tibble</td>
<td>as_data_frame; data_frame</td>
</tr>
<tr>
<td>magrittr</td>
<td>extract</td>
</tr>
<tr>
<td rowspan="5"><img src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2017/10/Visualize-e1509478640644.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-137" data-recalc-dims="1" height="100"></td>
<td>ggplot2</td>
<td>geom_col; geom_text; ggtitle; coord_flip; geom_text_repel; geom_jitter</td>
</tr>
<tr>
<td>ggthemes</td>
<td>theme_economist; economist_pal</td>
</tr>
<tr>
<td>factoextra</td>
<td>get_clust_tendency; fviz_dist</td>
</tr>
<tr>
<td>dendextend</td>
<td>dendlist; color_branches; assign_values_to_leaves_nodePar</td>
</tr>
<tr>
<td>corrplot</td>
<td>corrplot</td>
</tr>
</tbody>
</table>
</div>
<p></p>
<p><a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/01/Labour_voting_html.html" target="_blank">View the code here</a>.</p>
<h3>Citations / Attributions</h3>
<p>R Development Core Team (2008). R: A language and environment for<br>
statistical computing. R Foundation for Statistical Computing,<br>
Vienna, Austria. ISBN 3-900051-07-0, URL <a rel="nofollow" href="http://www.r-project.org/" target="_blank">http://www.R-project.org</a>.</p>
<p>Tal Galili (2015). dendextend: an R package for visualizing, adjusting, and comparing trees of hierarchical clustering. Bioinformatics <a rel="nofollow" href="https://academic.oup.com/bioinformatics/article/31/22/3718/240978" target="_blank"> &lt;doi:10.1093/bioinformratics/btv428></a></p>
<p>The post <a rel="nofollow" href="https://thinkr.biz/2018/01/29/cluster-of-six/" target="_blank">The “cluster of six”</a> appeared first on <a rel="nofollow" href="https://thinkr.biz/" target="_blank">thinkr</a>.</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="https://thinkr.biz/2018/01/29/cluster-of-six/"> R – thinkr</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/dmw7HlpZQBU" height="1"></a></div>