デンドログラムから切り取った枝の探索
cutree() 関数は、演習 5 と 6 で使用しましたが、h パラメーターを指定することで、特定の高さでツリーを切ることもできます。前の演習で生成したクラスターを、高さ 20 と 40 のそれぞれについて確認してみましょう。
この演習はコースの一部です
Rによるクラスター分析
演習の手順
cutree()にh = 20を指定して、クラスター割り当てベクトルclusters_h20を作成してください。- クラスターの割り当てを
cluster列としてlineupデータフレームに追加し、結果をlineup_h20_completeという新しいデータフレームに保存してください。 - 上記の 2 つのステップを高さ 40 に対して繰り返し、変数
clusters_h40とlineup_h40_completeを生成してください。 - ggplot2 を使って、両方の高さについてクラスターの割り当てで色分けした散布図を作成してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
dist_players <- dist(lineup, method = 'euclidean')
hc_players <- hclust(dist_players, method = "complete")
# Calculate the assignment vector with a h of 20
clusters_h20 <- ___
# Create a new data frame storing these results
lineup_h20_complete <- mutate(lineup, cluster = ___)
# Calculate the assignment vector with a h of 40
clusters_h40 <- ___
# Create a new data frame storing these results
lineup_h40_complete <- ___
# Plot the positions of the players and color them using their cluster for height = 20
ggplot(___, aes(x = ___, y = ___, color = factor(___))) +
geom_point()
# Plot the positions of the players and color them using their cluster for height = 40