สำรวจกิ่งที่ตัดออกจากต้นไม้
ฟังก์ชัน cutree() ที่ใช้ในแบบฝึกหัดที่ 5 และ 6 สามารถตัดต้นไม้ที่ความสูงที่กำหนดได้ด้วยพารามิเตอร์ h ลองสำรวจคลัสเตอร์ที่สร้างขึ้นจากแบบฝึกหัดก่อนหน้าโดยใช้ความสูง 20 และ 40
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์กลุ่มข้อมูลใน R
คำแนะนำการฝึกหัด
- สร้างเวกเตอร์การกำหนดคลัสเตอร์
clusters_h20โดยใช้cutree()กับh = 20 - เพิ่มการกำหนดคลัสเตอร์เป็นคอลัมน์
clusterใน data framelineupและบันทึกผลลัพธ์ลงใน data frame ใหม่ชื่อlineup_h20_complete - ทำซ้ำสองขั้นตอนข้างต้นสำหรับความสูง 40 โดยสร้างตัวแปร
clusters_h40และlineup_h40_complete - ใช้ ggplot2 สร้าง scatter plot โดยระบายสีตามการกำหนดคลัสเตอร์สำหรับทั้งสองความสูง
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
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