始める無料で始める

デンドログラムから切り取った枝の探索

cutree() 関数は、演習 5 と 6 で使用しましたが、h パラメーターを指定することで、特定の高さでツリーを切ることもできます。前の演習で生成したクラスターを、高さ 20 と 40 のそれぞれについて確認してみましょう。

この演習はコースの一部です

Rによるクラスター分析

コースを見る

演習の手順

  • cutree()h = 20 を指定して、クラスター割り当てベクトル clusters_h20 を作成してください。
  • クラスターの割り当てを cluster 列として lineup データフレームに追加し、結果を lineup_h20_complete という新しいデータフレームに保存してください。
  • 上記の 2 つのステップを高さ 40 に対して繰り返し、変数 clusters_h40lineup_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

コードを編集して実行