開始使用免費開始

探索從樹上切下的分枝

你在練習題 5 與 6 使用過的 cutree(),也可以透過參數 h 在指定高度把樹切開。請依據先前練習所產生的結果,分別以高度 20 與 40 來看看你得到的叢集。

本練習屬於課程

R 的叢集分析

檢視課程

練習說明

  • 使用 cutree() 並設定 h = 20,建立叢集指定向量 clusters_h20
  • 將叢集指定結果以欄位 cluster 附加到資料框 lineup,並將結果存成新的資料框 lineup_h20_complete
  • 針對高度 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

編輯並執行程式碼