我正在做一个聚类分析,我想计算某个变量在修剪树的叶子中出现的次数。下面是一个简化的示例,其中修剪的树只有三个分支。我现在想知道三个不同分支/叶子中的 As 和 B 的数量。我怎样才能得到那些?
rm(list=ls(all=TRUE))
mylabels <- matrix(nrow=1, ncol = 20)
mylabels[1,1:10] <- ("A")
mylabels[1,11:20] <- ("B")
myclusterdata <- matrix(rexp(100, rate=.1), ncol=100, nrow=20)
rownames(myclusterdata)<-mylabels
hc <- hclust(dist(myclusterdata), "ave")
memb <- cutree(hc, k = 3)
cent <- NULL
for(k in 1:3){
cent <- rbind(cent, colMeans(myclusterdata[memb == k, , drop = FALSE]))
}
hc1 <- hclust(dist(cent)^2, method = "cen", members = table(memb))
# whole tree
plot(as.dendrogram(hc),horiz=T)
# pruned tree (only 3 branches)
plot(as.dendrogram(hc1),horiz=T)