Skip to content

[Improvement] Improvements to n_clusters() - #1295

Open
DominiqueMakowski wants to merge 5 commits into
mainfrom
fix-n-clusters-mclust
Open

DominiqueMakowski wants to merge 5 commits into
mainfrom
fix-n-clusters-mclust

Conversation

@DominiqueMakowski

Copy link
Copy Markdown
Member

see NEWS

Copilot AI balanced review requested due to automatic review settings November 5, 2026 14:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new hkmeans class assignment drops kmeans inheritance, breaking prediction because the package defines predict.kmeans() but no predict.hkmeans() method.

4 open issues
2 files not reviewed
  • man/cluster_analysis.Rd: Generated file
  • man/n_clusters.Rd: Generated file
What changed in this PR

This PR improves cluster-count selection and removes the factoextra dependency from the package’s clustering tools.

Changes:

  • Computes clustering diagnostics and hierarchical k-means directly using base R and cluster.
  • Corrects voting, argument handling, and random-seed preservation in n_clusters(), and enables the M3C branch.
  • Adds regression tests and updates documentation, examples, and release notes.
File Description
vignettes/​clustering.Rmd Removes factoextra setup requirements.
tests/​testthat/​test-n_clusters.R Adds coverage for voting, seed preservation, and diagnostic calculations.
tests/​testthat/​test-cluster_analysis.R Tests hierarchical k-means initialization and stored predictions.
R/​n_clusters.R Updates method voting, backend handling, and BIC metadata.
R/​n_clusters_easystats.R Replaces factoextra calls with direct diagnostic calculations.
R/​cluster_analysis.R Implements hierarchical k-means locally.
NEWS.md Documents clustering improvements and fixes.
man/​n_clusters.Rd Updates scaling guidance and example dependencies.
man/​cluster_analysis.Rd Updates hierarchical k-means documentation and scaling guidance.
DESCRIPTION Removes factoextra from suggested dependencies.

Review effort: Balanced

Comment thread R/cluster_analysis.R Outdated
iter.max = iterations,
algorithm = kmeans_method
)
class(model) <- "hkmeans"
Comment thread NEWS.md Outdated
Comment on lines +14 to +18
* *factoextra* is no longer needed. `n_clusters_elbow()`,
`n_clusters_silhouette()`, `n_clusters_gap()` and `n_clusters()` now compute
the within-cluster sum of squares, the silhouette width and the gap statistic
directly (with *cluster*). `cluster_analysis(method = "hkmeans")` runs the
hierarchical k-means itself. The results are the same as before. The
Comment thread R/cluster_analysis.R Outdated
iter.max = iterations, hc.method = hclust_method, ...
# Hierarchical k-means, as in factoextra::hkmeans(): the centers of the
# clusters from a hierarchical clustering are the initial centers of k-means
hc <- stats::hclust(stats::dist(cluster_data), method = hclust_method)
Comment thread R/n_clusters.R

if ("M3C" %in% tolower(package)) {
if ("m3c" %in% tolower(package)) {
out <- rbind(out, .n_clusters_M3C(x, n_max = n_max, fast = fast))
DominiqueMakowski and others added 3 commits November 5, 2026 15:04
- Apply the air formatting suggestions.
- hkmeans: honour `hc.metric` (as factoextra::hkmeans() did), and keep the
  "kmeans" class so that predict() with newdata works.
- Restore the user's random seed after M3C::M3C(), which calls set.seed(),
  with a helper shared with the NbClust methods.
- Bump the development version to 0.30.0.17.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
predict() for cluster_meta() gets a `method` argument passed to hclust(),
with "average" as the default (instead of "complete"), the usual choice for
consensus matrices (Monti et al., 2003). With few solutions, the matrix has
many ties, and complete linkage cut the tree arbitrarily. The tree is
returned in the "tree" attribute. predict() is now documented on the
cluster_meta() page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@strengejacke

Copy link
Copy Markdown
Member

See copilot comments. Seem reasonable to me.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants