'tree' 的 'height' 组件未排序 cutree 中的错误

The 'height' component of 'tree' is not sorted Error in cutree

我正在尝试进行一些分组,但遇到了这个错误。

Evaluation error: the 'height' component of 'tree' is not sorted (increasingly).

我的输入是:

library(stringdist)
name <- c("luke,abcdef","luke,abcdeh","luke,abcdeg")
a<-stringdistmatrix(name, method="jw")
clusts <- hclust(a, method="ward.D2")

但是当我尝试剪切它时,它给了我一个错误:

> cutree(clusts, h = 0.155)
Error in cutree(clusts, h = 0.155) : 
  the 'height' component of 'tree' is not sorted (increasingly)

但是如果我使用

a<-stringdistmatrix(name, method="jw", p=0.05)

一切正常。

我一直在寻找解决方案,但找不到。 我应该怎么做才能防止这种情况发生并使其继续工作?

我也注意到,如果我有相同的距离矩阵,但是是手工生成的(所以集群中没有距离参数。

如果比较这两个例子的 diff(clusts$height),第一个结果是一个很小的负数,第二个结果正好是零。因此,问题是由本应相等的值的二进制表示舍入差异引起的。

如果在计算 clusts...

后对高度进行四舍五入,它应该可以工作
clusts$height <- round(clusts$height, 6)