Exploring parliamentary voting patterns with hierarchical clustering
Author
Carl Goodwin
Published
January 29, 2018
Modified
January 20, 2023
Before each vote, the Speaker of the House yells “Division! Clear the Lobby”. I’d like to find which cluster of MPs (Members of Parliament) may be exiting the lobby and going their own way.
Hansard reports what’s said in the UK Parliament, sets out details of divisions, and records decisions taken during a sitting. The R package hansard package(Odell 2017) provides access to the data.
I’ll use it to extract the “aye” and “no” votes. Use of possibly prevents the code from stopping when it encounters former MPs for whom no data is returned.
Voting the opposite way to the majority of the party, as well as non-votes, will both be of interest when assessing which MPs are “most distant” from the wider party.
votes_df<-votes|>left_join(mps, by =join_by(mp==ID))|>select(about =about.x, title, date_value, lobby, mp, name =full_name_value)|>transmute( vote =if_else(lobby=="aye", 1, -1), mp =str_c(name, " (", mp, ")"), about =str_replace(about, "http://data.parliament.uk/resources/", ""), title =str_c(title, " (", about, ")"))|>select(-about)|>pivot_wider(names_from =title, values_from =vote, values_fill =0)
The data are standardised (i.e. scaled) to ensure comparability. This is verified by ensuring the mean and standard deviation are close to zero and one respectively.
Draft Local Authorities (Mayoral Elections) (England and Wales) (Amendment) Regulations 2017 (809953)_mean
Draft Local Authorities (Mayoral Elections) (England and Wales) (Amendment) Regulations 2017 (809953)_sd
Draft Combined Authorities (Mayoral Elections) (Amendment) Order 2017 (809954)_mean
Draft Combined Authorities (Mayoral Elections) (Amendment) Order 2017 (809954)_sd
European Union (Withdrawal) Bill: Committee of the whole House New Clause 13 (809988)_mean
European Union (Withdrawal) Bill: Committee of the whole House New Clause 13 (809988)_sd
Queen’s Speech: Economy and Jobs Amdt (g) (746518)_mean
Queen’s Speech: Economy and Jobs Amdt (g) (746518)_sd
EU (Withdrawal) Bill: Committee of the whole House Clause 1 (788773)_mean
EU (Withdrawal) Bill: Committee of the whole House Clause 1 (788773)_sd
Motion to sit in private (798541)_mean
Motion to sit in private (798541)_sd
European Union (Withdrawal) Bill: Committee of the whole House Amdt 120 (810046)_mean
European Union (Withdrawal) Bill: Committee of the whole House Amdt 120 (810046)_sd
EU (Withdrawal) Bill: Committee of the whole House Amdt 79 (788772)_mean
EU (Withdrawal) Bill: Committee of the whole House Amdt 79 (788772)_sd
sd_min
sd_max
mean_min
mean_max
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
1
1
0
0
I’d like to assess whether the data contain meaningful clusters rather than random noise. This is achieved quantitatively by calculating the Hopkins statistic, and visually by inspection.
If the Hopkins statistic is closer to 1 than 0, then we have data which may be clustered.
There are eight methods I could use for hierarchical clustering, and I’ll need to determine which will yield results that best fit the data.
The correlation plot below shows that the median and ward methods have a weaker correlation with the other five methods.
orig_dist<-scaled_df|>select(-mp)|>dist()dend_meths<-c("complete","average","single","ward.D","ward.D2","mcquitty","median","centroid")dend_list<-map(dend_meths, \(x){orig_dist|>hclust(x)|>as.dendrogram()})dend_list|>reduce(dendlist)|>set_names(dend_meths)|>cor.dendlist()|>corrplot("pie","lower", col =cols[1], mar =c(1, 0.5, 4, 0.5), order ="AOE", tl.cex =0.8, tl.col ="black", cl.cex =0.7)
The above plot does not tell us which method is optimal. For that, I’ll take each of the cluster agglomeration methods and calculate their cophenetic distances. I can then correlate these with the original distance to see which offers the best fit.
The plot below confirms the ward and median methods having a weaker fit. Average produces the strongest correlation coefficient of 0.98.
best_method|>ggplot(aes(reorder(method, correlation), correlation))+geom_col(fill =cols[1], width =0.8)+geom_text(aes(label =str_c(method, " ", round(correlation, 2))), hjust =1.3, colour ="white")+coord_flip()+labs( x ="Method", y ="Correlation", title ="Cluster Method Correlation Coefficients", caption ="Source: Hansard")
I can now plot the full Labour Party dendrogram using the average method. This shows a “cluster of six” MPs which is the last to merge with the rest of the party based on their voting pattern.
dend_avg<-orig_dist|>hclust("average")|>as.dendrogram()labels(dend_avg)<-scaled_df$mp[order.dendrogram(dend_avg)]dend<-dend_avg|>color_branches(k =2, col =cols[4])|>set("labels_cex", 0.4)start_formatted<-date_parse(start_date, format ="%Y-%m-%d")|>date_format(format ="%b %d, %Y")end_formatted<-date_parse(end_date, format ="%Y-%m-%d")|>date_format(format ="%b %d, %Y")ggplot(rev(dend), horiz =TRUE, offset_labels =-0.2)+labs( y ="\nDistance", title ="Hierarchical Clustering of Labour MPs", subtitle ="Based on House of Commons Divisions Since the 2017 Election", caption =glue("Source: Hansard ({start_formatted} to {end_formatted})"))+theme(panel.border =element_blank())
I’ll zoom in on the “cluster of six”.
dend_cuts<-dend|>assign_values_to_leaves_nodePar(19, "pch")|>assign_values_to_leaves_nodePar(5, "cex")|>assign_values_to_leaves_nodePar(cols[1], "col")|>set("labels_cex", 0.4)|>set("branches_lwd", 2.5)|>color_branches(k =2, col =cols[1])|>cut(h =50)ggplot(rev(dend_cuts$lower[[1]]), horiz =TRUE, nodePar =nodePar, offset_labels =-0.5)+labs( title ="Cluster of Six", subtitle ="MPs who Branch off First in the Dendrogram")+theme_void()+theme(plot.margin =unit(c(1, 1, 1, 1), "cm"))
Summarising and sorting the total votes by MP tells me that the “cluster of six” MPs are among the eight MPs voting the fewest times. And I can, for example, verify the record for Emma Reynolds directly via Hansard.
Non-voting will not be the only influencing factor. The “distant cluster” will be particularly influenced by a small minority of MPs voting in the opposite direction to the overwhelming majority.
Cook’s Distance visualises these influential outliers. This 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”.
Summarising below the packages and functions used in this post enables me to separately create a toolbox visualisation summarising the usage of packages and functions across all posts.