基于高度的聚类
在之前的练习中,您使用预先设定的聚类数(k)来将观测分组。在本练习中,您将利用树状图的可视化表示,通过指定最大高度(h)来分组;低于该高度的分支将形成聚类。
您将使用 dendextend 库中的 color_branches() 函数,直观检查树状图上任意高度处形成的聚类。
对象 hc_players 已从您之前对足球阵型数据的工作中延续而来。
本练习是课程的一部分
R 中的聚类分析
练习说明
- 使用函数
as.dendrogram(),将您的hclust结果创建为树状图对象dend_players。 - 绘制该树状图。
- 使用
color_branches()函数,基于切割高度 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