Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created April 29, 2024 09:25
Show Gist options
  • Save abikoushi/3ca19462a66a2d5b21df3f13fce4d699 to your computer and use it in GitHub Desktop.
Save abikoushi/3ca19462a66a2d5b21df3f13fce4d699 to your computer and use it in GitHub Desktop.
Comparison of the methods of `hclust`
library(dplyr)
library(ggplot2)
x <- c("ward.D","ward.D2", "single","complete",
"average","mcquitty","median","centroid")
X <- log(as.matrix(iris[,1:4]))
d <- dist(X)
dm <- as.matrix(d)
hres <- lapply(x, function(m)hclust(d,method=m))
do <- sapply(1:length(x),function(i)dm[hres[[i]]$order[1],hres[[i]]$order])
#s <- unname(dm[1,][order(dm[1,])])
colnames(do) <- x
row.names(do) <- NULL
df <- reshape2::melt(do, varnames=c("order","method")) %>%
mutate(method=factor(method,levels=x))
ggplot(df,aes(x=order,y=value))+
geom_line()+
facet_wrap(~method)+
theme_bw()+labs(y="dist")
ggsave("dist.png")
###
df <- vector("list", length(x))
for(i in 1:length(x)){
do <- dm[hres[[i]]$order,hres[[i]]$order]
colnames(do) <- NULL
rownames(do) <- NULL
do[lower.tri(do,diag = TRUE)] <-NA
df[[i]] <- mutate(reshape2::melt(do,varnames = c("row","col")),
method=x[i]) %>%
dplyr::filter(!is.na(value))
}
df <- bind_rows(df) %>%
mutate(method=factor(method,levels=x))
ggplot(df,aes(x=col,y=row,fill=value))+
geom_tile()+
scale_fill_viridis_c()+
facet_wrap(~method)+
scale_y_reverse()+labs(fill="dist")
ggsave("distmat.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment