R/ClusterAUCC.R

Defines functions ClusterAUCC

Documented in ClusterAUCC

ClusterAUCC = function(Cls, DataOrDistances, DistanceMethod = "euclidean",
                       Silent = TRUE, ...) {
  # V=ClusterAUCC(Cls,Data)
  #
  # Area under the ROC Curve for Clustering [Jaskowiak et al., 2022]
  #
  # INPUT
  # Cls[1:n]                 Numeric, character or factor cluster labels; no missing labels
  # DataOrDistances          Data[1:n,1:d], dissimilarities[1:n,1:n] or a dist object
  #
  # OPTIONAL
  # DistanceMethod           Distance method; default "euclidean"; ignored for distances
  # Silent                   TRUE suppresses distance progress, not warnings/messages
  # ...                      Further arguments for parallelDist::parDist()
  #                          or stats::dist() if parallelDist is unavailable
  #
  # OUTPUT
  # List V with
  #   AUCC                     Area under the clustering ROC curve in [0,1]; larger is better
  #   Object                   List of WithinPairCount and BetweenPairCount
  #
  # DETAILS
  # AUCC = P(Dwithin < Dbetween) + 0.5 * P(Dwithin == Dbetween).
  # Average ranks implement the tie rule without comparing every pair
  # of distances. Each unordered object pair occurs once, with no self
  # pairs. Zero is an ordinary cluster label. Absence of either type
  # of pair yields NaN with a warning.
  #
  # Author: Michael Thrun, 11/2026
  #
  # [Jaskowiak et al., 2022] Jaskowiak, P. A., Costa, I. G., & Campello, R. J. G. B.: The area under the ROC curve as a measure of clustering quality, Data Mining and Knowledge Discovery, Vol. 36, pp. 1219-1245, DOI:10.1007/s10618-022-00829-0, 2022.
  #
  Input = internalClusterValidation(Cls, DataOrDistances, "ClusterAUCC",
                                    TRUE, DistanceMethod, Silent, ...)
  DataDists = Input$Data
  Cls = Input$Cls
  Pairs = lower.tri(DataDists)
  Distances = DataDists[Pairs]
  Within = outer(Cls, Cls, "==")[Pairs]
  # Double counts prevent integer overflow in the product of pair counts.
  WithinCount = as.double(sum(Within))
  BetweenCount = length(Distances) - WithinCount
  if (WithinCount == 0 || BetweenCount == 0) {
    warning("ClusterAUCC: requires both within-cluster and between-cluster pairs")
    return(list(AUCC = NaN, Object = NULL))
  }
  Ranks = rank(-Distances, ties.method = "average")
  Score = (sum(Ranks[Within]) - WithinCount * (WithinCount + 1) / 2) /
    (WithinCount * BetweenCount)
  return(list(AUCC = Score,
              Object = list(WithinPairCount = WithinCount,
                            BetweenPairCount = BetweenCount)))
}

Try the FCPS package in your browser

Any scripts or data that you put into this service are public.

FCPS documentation built on Nov. 28, 2026, 9:08 a.m.