R/reduce_dist.R

Defines functions reduce_dist

reduce_dist <-
function(ddist, K = 500, given = NULL) {
  
  if(is.null(given)) {
    hhc <- hclust(ddist, method = "complete")
    clusters <- as.factor(cutree(hhc, k = K)) # get the clusters
    namesOfClusters <- levels(clusters) <- paste0("c", levels(clusters))
  } else {
    clusters <- as.factor(given) # get the clusters
    namesOfClusters <- levels(clusters)
    K <- length(unique(given))
  }
  
  ddist <- as.matrix(ddist)
  rdist<- matrix(0, ncol = K, nrow = K)
  colnames(rdist) <- rownames(rdist) <- levels(clusters)
  
  for(j in 1:K) for(i in j:K) {
    rrow <- names(clusters)[which(clusters == namesOfClusters[i])]
    ccol <- names(clusters)[which(clusters == namesOfClusters[j])]
    rdist[i, j] <- mean(ddist[rrow, ccol])
  }
  rdist <- as.dist(tmp <- rdist)
  rdist <- (rdist - min(rdist))/(max(rdist)-min(rdist))
  attr(rdist, "clustering") <- clusters
  invisible(rdist)
}

Try the yaConsensus package in your browser

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

yaConsensus documentation built on April 3, 2025, 10:12 p.m.