R/internalClusterValidation.R

Defines functions internalClusterValidation

# Input preparation for the standalone internal cluster-quality measures.
# Existing FCPS quality functions retain their own input handling.
internalClusterValidation = function(Cls, Data, FunctionName,
                                     Distances = FALSE,
                                     DistanceMethod = "euclidean",
                                     Silent = TRUE, ...) {
  # V=internalClusterValidation(Cls,Data,"ClusterCIndex",Distances=TRUE)
  #
  # Validate and prepare inputs shared by the internal quality measures.
  #
  # INPUT
  # Cls[1:n]          Numeric, character or factor labels; no missing values
  # Data              Finite numeric matrix/data.frame, or distances if enabled
  # FunctionName      Calling function's name for errors and messages
  #
  # OPTIONAL
  # Distances         FALSE: require feature data; TRUE: prepare dissimilarities
  # DistanceMethod    Method for parallelDist::parDist() or stats::dist()
  # Silent            TRUE suppresses distance calculation progress messages
  # ...               Further arguments for the selected distance function
  #
  # OUTPUT
  # List V with
  #   Data             Validated feature matrix or full dissimilarity matrix
  #   Cls[1:n]         Consecutive cluster codes in order of first appearance
  #   Labels[1:k]      Original labels corresponding to those codes
  #   Dimension        Number of feature columns; NULL for supplied distances
  #
  # Author: Michael Thrun, 11/2026
  #
  if (missing(Cls)) stop(FunctionName, ": Cls is missing")
  if (missing(Data)) stop(FunctionName, ": data is missing")
  if (!(is.numeric(Cls) || is.character(Cls) || is.factor(Cls)) ||
      !is.null(dim(Cls)) || anyNA(Cls) ||
      (is.numeric(Cls) && any(!is.finite(Cls))))
    stop(FunctionName, ": Cls must be a vector of non-missing cluster labels")
  if (!is.logical(Silent) || length(Silent) != 1L || is.na(Silent))
    stop(FunctionName, ": Silent must be TRUE or FALSE")

  IsDistance = inherits(Data, "dist") || (Distances && IsDissimilarity(Data))
  if (IsDistance && !Distances)
    stop(FunctionName, ": a data matrix is required, not a dist object")
  if (!(is.matrix(Data) || is.data.frame(Data) || inherits(Data, "dist")))
    stop(FunctionName, ": data must be a numeric matrix or data.frame")
  Data = as.matrix(Data)
  if (!is.numeric(Data) || nrow(Data) < 2L || ncol(Data) < 1L ||
      any(!is.finite(Data)))
    stop(FunctionName, ": data must contain finite numeric values and at least two rows")
  if (length(Cls) != nrow(Data))
    stop(FunctionName, ": number of rows does not equal length of Cls")
  Dimension = if (IsDistance) NULL else ncol(Data)

  if (Distances) {
    if (!IsDistance) {
      if (!Silent) message(FunctionName, ": computing pairwise distances")
      if (requireNamespace("parallelDist", quietly = TRUE)) {
        Data = as.matrix(parallelDist::parDist(Data, method = DistanceMethod, ...))
      } else {
        message("Subordinate parallelDist package is missing. stats::dist() is used.")
        Data = as.matrix(stats::dist(Data, method = DistanceMethod, ...))
      }
    }
    if (!IsDissimilarity(Data))
      stop(FunctionName, ": distances must be finite, non-negative, symmetric, with a zero diagonal")
    # Remove only round-off deviations accepted by IsDissimilarity().
    Data = Data / 2 + t(Data) / 2
    Data[Data < 0] = 0
    diag(Data) = 0
  }
  Labels = unique(Cls)
  return(list(Data = Data, Cls = match(Cls, Labels), Labels = Labels,
              Dimension = Dimension))
}

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.