กลุ่มที่แบ่งตามความสูง
ในแบบฝึกหัดก่อนหน้า คุณได้จัดกลุ่มข้อมูลโดยกำหนดจำนวนกลุ่ม (k) ไว้ล่วงหน้า ในแบบฝึกหัดนี้ จะใช้การแสดงผลด้วย dendrogram เพื่อจัดกลุ่มข้อมูลโดยอิงจากความสูงสูงสุด (h) ซึ่งกลุ่มจะก่อตัวขึ้นเมื่ออยู่ต่ำกว่าค่านี้
จะใช้ฟังก์ชัน color_branches() จากไลบรารี dendextend เพื่อตรวจสอบด้วยสายตาว่ากลุ่มใดก่อตัวขึ้นที่ความสูงต่าง ๆ บน dendrogram
hc_players ถูกนำมาจากงานที่ผ่านมาในชุดข้อมูลตำแหน่งนักฟุตบอล
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์กลุ่มข้อมูลใน R
คำแนะนำการฝึกหัด
- สร้าง dendrogram object ชื่อ
dend_playersจากผลลัพธ์ของhclustโดยใช้ฟังก์ชันas.dendrogram() - พล็อต dendrogram
- ใช้ฟังก์ชัน
color_branches()สร้างและพล็อต dendrogram ใหม่โดยให้กลุ่มแสดงด้วยสีตามความสูงที่ตัดที่ 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