以高度為基準的叢集
在前面的練習中,你是用預先指定的叢集數(k)來把觀測值分群。這個練習中,你會利用樹狀圖(dendrogram)的視覺化,改用最大高度(h)作為門檻,在該高度以下形成叢集,來將觀測值分群。
你會使用 dendextend 函式庫的 color_branches() 函式,視覺化檢視在樹狀圖上任何高度所形成的叢集。
物件 hc_players 已從你先前處理足球陣容資料的步驟延續而來。
本練習屬於課程
R 的叢集分析
練習說明
- 使用
as.dendrogram()將你的hclust結果建立為 dendrogram 物件dend_players。 - 繪製該 dendrogram。
- 使用
color_branches()以切割高度 20 產生並繪製新的 dendrogram,讓叢集以此高度著色。 - 以高度 40 重複上一個步驟。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
library(dendextend)
dist_players <- dist(lineup, method = 'euclidean')
hc_players <- hclust(dist_players, method = "complete")
# Create a dendrogram object from the hclust variable
dend_players <- as.dendrogram(___)
# Plot the dendrogram
# Color branches by cluster formed from the cut at a height of 20 & plot
dend_20 <- color_branches(___, h = ___)
# Plot the dendrogram with clusters colored below height 20
# Color branches by cluster formed from the cut at a height of 40 & plot
dend_40 <- ___
# Plot the dendrogram with clusters colored below height 40