Repository navigation
Fix self-distance and sampling bugs and vectorize .clusterstructure_hopkins() - #1027
Merged
strengejacke merged 2 commits intoNov 24, 2026
Merged
strengejacke merged 2 commits into
strengejacke merged 2 commits into
Conversation
strengejacke
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
.clusterstructure_hopkins()(R/check_clusterstructure.R:107-144) uses a hand-rolled nested R loop (inherited fromclustertend) with two correctness bugs and ank[i] == 1):minqiis unconditionally initialized tostats::dist(rbind(qq[i, ], x[1, ]))before checking whetherqq[i, ]equalsx[1, ]. Wheneverk[i] == 1(orqq[i, ]matchesx[1, ]),minqistarts at0and cannot be improved by any positive distance, leavingminq[i] = 0and biasing Hopkins'k <- round(stats::runif(n, 1, nrow(x)))samples boundary indices1andnrow(x)with half the probability of interior indices2:(nrow(x) - 1)instead of sampling uniformly.stats::dist(rbind(...))calls inside nested R loops: Callingstats::dist()on 2-row matrices inside a double loop incurs massive R-level overhead on moderate datasets.Hand-rolling pairwise distance loops and discrete uniform sampling in R is error-prone around boundary/self-match cases (like
j = 1, duplicate rows, and 2-row inputs wheredrop = FALSEmatters). Base R'ssample.int()and vectorizedstats::dist()handle all supported distance metrics in compiled C without per-pair loop edge cases.Minimal repro
Fix
round(stats::runif(n, 1, nrow(x)))withsample.int(nr, n)for uniform sampling without replacement.for (i in 1:n) for (j in 2:nrow(x))stats::dist(rbind(...))loops with a single vectorizedstats::dist(rbind(p, x), method = distance)call, extracting thep-to-xandx-to-xblocks withdrop = FALSEand excluding zero distances (dist_xx[dist_xx == 0] <- Inf) uniformly across all rows including row 1 (with a finite guard if all rows are identical).tests/testthat/test-checks.Rand add regression tests for row-1 self-distance exclusion and non-Euclidean ("manhattan") distances.Paired timings (median of 3 runs)
main)iris[, 1:4](Verification
test-checks.R: PASS (all assertions includingcheck_factorstructureandcheck_clusterstructure).R/check_clusterstructure.R): FAIL on the new row-1 self-distance test and updated expectations; restoringR/check_clusterstructure.R: PASS.