The “cluster of six”

[This article was first published on R – thinkr, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Unsupervised machine learning

Hansard reports what’s said in the UK Parliament, sets out details of divisions, and records decisions taken during a sitting. The hansard R package provides functions to import its data.

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.

Supervised machine learning makes predictions from labeled training data. The unsupervised flavour looks for hidden structure in “unlabeled” data, i.e. a classification or categorisation not included in the observations. Hierarchical clustering will identify a cluster of six MPs as the most “distant” from the wider party.

The full methodology, including the code, is published here. 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 Cook’s Distance.

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.

The “cluster of six”

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.

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.

Does this cluster make sense?

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.

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”.

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”.


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.

R toolkit

purrrmap_df; pmap; map_lgl; transpose; safely
hansardcommons_members; mp_vote_record
dplyrmutate; select; if_else; left_join; everything; summarise; group_by; arrange
tidyrspread; gather
stringrstr_replace; str_c
lubridatemonth; day; year
tibbleas_data_frame; data_frame
ggplot2geom_col; geom_text; ggtitle; coord_flip; geom_text_repel; geom_jitter
ggthemestheme_economist; economist_pal
factoextraget_clust_tendency; fviz_dist
dendextenddendlist; color_branches; assign_values_to_leaves_nodePar

View the code here.

Citations / Attributions

R Development Core Team (2008). R: A language and environment for
statistical computing. R Foundation for Statistical Computing,
Vienna, Austria. ISBN 3-900051-07-0, URL

Tal Galili (2015). dendextend: an R package for visualizing, adjusting, and comparing trees of hierarchical clustering. Bioinformatics  

The post The “cluster of six” appeared first on thinkr.

To leave a comment for the author, please follow the link and comment on their blog: R – thinkr. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)