高さに基づくクラスター
これまでの演習では、クラスター数(k)をあらかじめ指定して観測値をグループ化してきました。この演習では、デンドログラムの視覚的な表現を活用し、最大高さ(h)を基準にクラスターを形成します。指定した高さより下でクラスターがまとまる仕組みです。
dendextend ライブラリの color_branches() 関数を使い、デンドログラム上の任意の高さで形成されるクラスターを色分けして確認しましょう。
hc_players は、前の演習で使用したサッカーのラインアップデータから引き継がれています。
この演習はコースの一部です
Rによるクラスター分析
演習の手順
as.dendrogram()関数を使って、hclustの結果から dendrogram オブジェクトdend_playersを作成してください。- デンドログラムを描画してください。
color_branches()関数を使い、カット高さ 20 でクラスターを色分けした新しいデンドログラムを作成・描画してください。- 同じ手順を高さ 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