探索从树上切下的分支
您在练习 5 和 6 中使用过的 cutree() 函数,也可以通过参数 h 在给定高度处切割树。请基于高度 20 和 40,花点时间探索您在前面练习中生成的簇。
本练习是课程的一部分
R 中的聚类分析
练习说明
- 使用
cutree()并设定h = 20,构建簇分配向量clusters_h20。 - 将簇分配作为名为
cluster的列追加到数据框lineup中,并将结果保存为新的数据框lineup_h20_complete。 - 对高度 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