Skip to content

Instantly share code, notes, and snippets.

@Btibert3
Created October 16, 2019 14:05
Show Gist options
  • Select an option

  • Save Btibert3/441822ea54d7fa5e17c4772ffe70dfb1 to your computer and use it in GitHub Desktop.

Select an option

Save Btibert3/441822ea54d7fa5e17c4772ffe70dfb1 to your computer and use it in GitHub Desktop.
A simple function to explore hclust in R to look at distance/cluster tradeoff
## function to take an dendrogram, and heights for each iteration
## pull out the # of clusters and height
hclust_eval = function(c) {
# extract the height, and the dendrogram
h = c$height
d = as.dendrogram(c)
# the container for the stats
s = list()
# for each entry in h, get the # of clusters
# TODO: improve this code
for (i in 1:length(h)) {
tmp_h = h[i]
tmp = length(unique(cutree(d, h=tmp_h)))
s[[i]] = list(iteration = i,
n_clust = tmp,
h_dist = tmp_h)
rm(tmp_h, tmp)
}
# return s
return(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment