Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created April 29, 2024 10:56
Show Gist options
  • Save abikoushi/b8255514cf55ac614a7000ba496bdfb2 to your computer and use it in GitHub Desktop.
Save abikoushi/b8255514cf55ac614a7000ba496bdfb2 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:2]))
df <- data.frame(X)
d <- dist(X)
dm <- as.matrix(d)
hres <- lapply(x, function(m)hclust(d, method=m))
do <- lapply(1:length(x),
function(i){data.frame(
X,
cl2=cutree(hres[[i]],k=3),
cl3=cutree(hres[[i]],k=4),
method=x[i])})
df <-bind_rows(do) %>%
mutate(method=factor(method,levels=x))
head(df)
hull <- group_by(df, method, cl3) %>%
slice(chull(Sepal.Length, Sepal.Width))
ggplot(df,aes(x=Sepal.Length, y=Sepal.Width))+
geom_polygon(data=hull, aes(group=cl3),
colour="grey40",
linetype=1,
fill=NA)+
geom_point(aes(colour=factor(cl2)), alpha=0.5)+
facet_wrap(~method)+
theme_bw()+labs(colour="cluster")
ggsave("hull.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment