Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
Created June 29, 2018 06:16
Show Gist options
  • Save benjamin-chan/ff2adddb2cbcdc044038d6f7525cf769 to your computer and use it in GitHub Desktop.
Save benjamin-chan/ff2adddb2cbcdc044038d6f7525cf769 to your computer and use it in GitHub Desktop.
install.packages("cluster")
install.packages("ggdendro")
install.packages("factoextra")
library(cluster)
library(ggdendro)
library(factoextra)
hc <-
df %>%
select(-id) %>%
select(-starts_with("PCB")) %>%
agnes(method= "ward")
hc
hc %>% ggdendrogram()
df %>%
select(-id) %>%
select(-starts_with("PCB")) %>%
fviz_nbclust(FUN = hcut, method = "wss")
df %>%
select(-id) %>%
select(-starts_with("PCB")) %>%
fviz_nbclust(FUN = hcut, method = "silhouette")
df %>%
select(-id) %>%
select(-starts_with("PCB")) %>%
clusGap(FUN = hcut, nstart = 25, K.max = 10, B = 50) %>%
fviz_gap_stat()
clusterID <- hc %>% cutree(k = 4)
table(clusterID)
hc
plot(hc, ask = FALSE, which.plots = 2)
rect.hclust(hc, k = 4, border = 2:8)
df0 <-
df %>%
mutate(clusterID = hc %>% cutree(k = 4))
hc_a <- agnes(df, method = "ward")
cutree(as.hclust(hc_a), k = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment