This Computational Provenance Record documents the statistical computing environment, software dependencies, computational provenance, and bibliographic references associated with the RAISINS Cluster Analysis module. It is intended to support computational reproducibility and software transparency. Detailed statistical methodology, mathematical derivations, and user guidance are provided separately in the official module documentation.
Any issues or updates required please comment here
Z-score standardisation (mean 0, SD 1) applied to every selected clustering variable before analysis
Alternatives
Centering only, min-max (0-1), unit-length, robust (median / IQR), or none, selectable per run
Rationale
Distance-based clustering is dominated by variables with the largest numeric range; scaling puts all clustering variables on a comparable footing
Distance
Measure
(dist())
Default
euclidean
Alternatives
manhattan, maximum, canberra, minkowski
Linkage
Method
(hclust())
Default
complete
Alternatives
average, single, ward.D2, mcquitty, median, centroid
Number of
Clusters (k)
Initial value
Pre-filled from the elbow-method suggestion below; user-adjustable, must satisfy 2 ≤ k < n
Elbow method
factoextra::fviz_nbclust(Xs, FUNcluster = kmeans, method = "wss") on the scaled data; suggested k is taken at the point of maximum decrease in the WSS curve (which.min(diff(wss)) + 1). Used as a fast heuristic even though the reported clustering itself is hierarchical, not k-means
Silhouette method
factoextra::fviz_nbclust(X, FUN = hcut, method = "silhouette"); suggested k maximises the average silhouette width. Note: hcut() uses its own internal default linkage/distance (Ward / Euclidean) independent of the linkage and distance the user selected elsewhere in the module
Gap statistic
cluster::clusGap(Xs, FUN = hcut, nstart = 25, K.max = 10, B = 50) with cluster::maxSE(..., method = "firstSEmax"); suggested k is the smallest k within one standard error of the maximum gap. Visualised via factoextra::fviz_gap_stat()
Dendrogram
Fit
Cophenetic correlation
cor(original distance, cophenetic(hc)); values closer to 1 indicate the dendrogram faithfully represents the original distance structure
PCA
Diagnostics
Eigenvalues & variance explained
res.pca$eig - eigenvalue, percentage of variance, and cumulative percentage of variance explained, one row per principal component
Variable contributions
res.pca$var$contrib - percentage contribution of each clustering variable to each principal component (columns sum to 100% per component)
Variable correlations
res.pca$var$cor - correlation of each clustering variable with each principal component, range -1 to +1
HCPC
(HCPC())
Basis
FactoMineR::PCA() is run on the scaled data first; FactoMineR::HCPC() then clusters in principal-component space, consolidating via Ward linkage plus a k-means pass. This is independent of the linkage/distance chosen for the main Analysis Results tab
Cluster quality
Total / Within / Between sum-of-squares computed in PCA space; Between/Total ratio reported as a separation indicator
Top variables per cluster
res.hcpc$desc.var$quanti - one row per (cluster, variable) pair, columns: v.test, Mean in category, Overall mean, sd in category, Overall sd, p.value, and n (number of observations in that cluster). Ranked by absolute v.test; larger magnitude indicates the variable more strongly characterises that cluster relative to the overall mean
Cluster-wise
Means
Raw units
aggregate(X, by = list(cluster = clusters), FUN = mean), grouped by the cutree()-based cluster assignment - not the HCPC assignment used for the rows above
Standardized scale ("Clustering Summary")
Mean of each PCA input variable per HCPC cluster, computed on the same scaled data PCA was run on; since the data are z-scored, positive values are above the overall (zero) average and negative values are below it
Intra-/Inter-Cluster
Distances
Intra-cluster
Mean pairwise distance among all members of the same cluster (upper triangle of that cluster's distance submatrix), using the cutree()-based assignment; only computed when distance is euclidean/manhattan and linkage is complete/average/single/ward.D2
Inter-cluster
Mean pairwise distance between every pair of distinct clusters' members; one row per cluster pair
Dendrogram
Plot
(fviz_dend())
Rendering
factoextra::fviz_dend() draws the cluster dendrogram with branches and tip labels colour-coded by cluster assignment (k colours), optionally with rectangles drawn around each cluster
Cluster Comparisons
(Tanglegram)
Alignment
Two dendrograms - built from independently selectable linkage/distance pairs - are aligned with dendextend::untangle(method = "step1side") to minimise crossing before comparison
Agreement metric
dendextend::entanglement(); 0 = the two dendrograms align perfectly, 1 = maximal tangling
Linkage Method
Correlation Matrix
Comparison
dendextend::cor.dendlist(method = "cophenetic") across Single / Complete / Average / Centroid / Median / Ward linkage on one distance metric
5 R Code for Key Analytical Steps
The code blocks below demonstrate the exact computation behind each reported result using the USArrests dataset (Murder, Assault, UrbanPop, Rape; 50 U.S. states) built into the datasets package - the same dataset shipped as this module’s own demo data.
5.1 Scaling, Distance, and Hierarchical Clustering
5.5 HCPC (Hierarchical Clustering on Principal Components)
library(FactoMineR)res.pca <-PCA(Xs, graph =FALSE) # PCA on the scaled datares.hcpc <-HCPC(res.pca, nb.clust = k, graph =FALSE) # Ward + k-means consolidationres.hcpc$data.clust$clust # cluster membershipres.hcpc$desc.var$quanti # top variables per cluster - v.test, Mean/sd in category,# Overall mean/sd, p.value, and n (observations per cluster)# cluster quality: Total / Within / Between sum-of-squares (PCA space)total_ss <-sum(scale(res.pca$ind$coord, scale =FALSE)^2)between_ss <-sum(sapply(split(as.data.frame(res.pca$ind$coord), res.hcpc$data.clust$clust),function(g) nrow(g) *sum((colMeans(g))^2)))within_ss <- total_ss - between_sscluster_seperation_ss <- between_ss / total_ss # Between/Total ratio (separation indicator)
5.6 PCA Diagnostics, Cluster Means, and Intra-/Inter-Cluster Distances
# PCA eigenvalues, variable contributions, and variable correlations with componentsres.pca$eig # eigenvalue / % variance / cumulative %res.pca$var$contrib # % contribution of each variable per componentres.pca$var$cor # correlation of each variable with each component# Cluster-wise means - raw units (uses the cutree()-based clusters, not HCPC)cluster_means <-aggregate(X, by =list(cluster = clusters), FUN = mean)# Cluster-wise means - standardized scale ("Clustering Summary", uses HCPC clusters)hcpc_cluster <- res.hcpc$data.clust$clusttab_means <-aggregate(as.data.frame(Xs), by =list(cluster = hcpc_cluster), FUN = mean)# Intra-/inter-cluster distances (uses the cutree()-based clusters + distance matrix)dmat <-as.matrix(dist(Xs, method ="euclidean"))cluster_ids <-sort(unique(clusters))intra <-sapply(cluster_ids, function(cl) { idx <-which(clusters == cl) m <- dmat[idx, idx]mean(m[upper.tri(m)]) # mean pairwise distance within the cluster})names(intra) <- cluster_idsinter <-combn(cluster_ids, 2, function(pair) {mean(dmat[clusters == pair[1], clusters == pair[2]]) # mean pairwise distance between the pair})names(inter) <-combn(cluster_ids, 2, paste, collapse ="-")
Explore the entire Cluster Analysis module in preview mode using our demo datasets. To submit suggestions or report a workflow issue, please use the discussion section below, or visit the official RAISINS website.
6 RAISINS Native Statistical Framework
RAISINS uses R for all its statistical computations. Every package used to generate major results is listed and demonstrated with examples, so results can be reproduced independently. These results are then organized and formatted on the RAISINS website along with visualisation to make them easier to use and interpret. RAISINS also has its own custom-built statistical tools for managing workflows, validating results, and generating reports. Details of these are not fully covered here, they’re shared with outside researchers only on request, and are subject to licensing terms.
7 Package References
R Core Team. (2025). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/
Le, S., Josse, J., & Husson, F. (2025). FactoMineR: Multivariate Exploratory Data Analysis and Data Mining (R package version 2.16). https://doi.org/10.32614/CRAN.package.FactoMineR
Kassambara, A., & Mundt, F. (2020). factoextra: Extract and Visualize the Results of Multivariate Data Analyses (R package version 2.1.0). https://doi.org/10.32614/CRAN.package.factoextra
Maechler, M., Rousseeuw, P., Struyf, A., Hubert, M., & Hornik, K. (2025). cluster: “Finding Groups in Data”: Cluster Analysis Extended Rousseeuw et al. (R package version 2.1.8.2). https://doi.org/10.32614/CRAN.package.cluster
Galili, T. (2015). dendextend: an R package for visualizing, adjusting, and comparing trees of hierarchical clustering (R package version 1.19.1). https://doi.org/10.32614/CRAN.package.dendextend
Allaire, J. J., Xie, Y., Dervieux, C., McPherson, J., Luraschi, J., Ushey, K., Atkins, A., Wickham, H., Cheng, J., Chang, W., & Iannone, R. (2026). rmarkdown: Dynamic Documents for R (R package version 2.31). https://github.com/rstudio/rmarkdown