How to line (cut) a dendrogram at the best K

僤鯓⒐⒋嵵緔 提交于 2020-08-08 05:05:09

问题


How do I draw a line in a dendrogram that corresponds the best K for a given criteria?

Like this:

Lets suppose that this is my dendrogram, and the best K is 4.

data("mtcars")
myDend <-  as.dendrogram(hclust(dist(mtcars))) 
plot(myDend)

I know that abline function is able to draw lines in graphs similarly to the one showed above. However, I don't know how could I calculate the height, so the function is used as abline(h = myHeight)


回答1:


The information that you need to get the heights came with hclust. It has a variable containing the heights. To get the 4 clusters, you want to draw your line between the 3rd biggest and 4th biggest height.

HC = hclust(dist(mtcars))
myDend <-  as.dendrogram(HC) 

par(mar=c(7.5,4,2,2))
plot(myDend)

k = 4
n = nrow(mtcars)
MidPoint = (HC$height[n-k] + HC$height[n-k+1]) / 2
abline(h = MidPoint, lty=2)



来源:https://stackoverflow.com/questions/49091292/how-to-line-cut-a-dendrogram-at-the-best-k

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!