Using clValid in R getting Error in cutree.default(clusterObj, nc) : Function cutree is only available for hclust/dendrogram/phylo objects only

Viewed 331

Using the default data set from Brock et al., 2008

install.packages("clValid", dependencies = TRUE)
library(clValid)
data("mouse")
express <- mouse[, c("M1", "M2", "M3", "NC1", "NC2", "NC3")]
rownames(express) <- mouse$ID
intern <- clValid(express, 2:6, clMethods = c("hierarchical", "kmeans", "diana", "fanny", 
"som", "pam", "sota", "clara", "model"), validation = "internal")

and getting the following:

Error in cutree.default(clusterObj, nc) :
Function cutree is only available for hclust/dendrogram/phylo objects only.

1 Answers

The problem seems to come from the fact that stats::cutree is masked by dendextend::cutree once the package dendextendis loaded and that dendextend::cutree does not work for objects generated with some functions of the cluster package like diana and agnes (while stats::cutree works fine).

EDIT : this problem was observed with dendextend 1.9.0 but has been very quickly fixed by the package author after bug report. The new version 1.10.0 works perfectly fine with cluster and the clValid version from the CRAN. It can be installed via : devtools::install_github("talgalili/dendextend")

This is the best solution now. I let here the rest of my initial answer below for the archives only.


Some code to test that (everything works fine without dendextend but loading dendextend triggers the error message :

# detach(package:dendextend)
# library(dendextend)
# 
library(cluster)
d <- iris[,-5]
cl <- diana(dist(d))
cutree(cl, 3)
cl <- agnes(dist(d))
cutree(cl, 3)

library(clValid)
intern <- clValid(d, 2:10,
                  clMethods = c("hierarchical", "kmeans", "diana", "fanny",
                                "model", "pam", "agnes"),
                  method = "ward", validation = "internal")

Sadly clValid seems to be orphaned so it is not possible to ask the developer to change the code (or am I wrong ?).

Solutions :

  • do not use dendextend and clValid at the same time
  • do not use agnes and diana in the clMethods argument of clValid
  • I have forked the clValid github repository (from CRAN) and made the small changes necessary in the code. You can install this version with devtools::install_github("GillesSanMartin/clValid"). It works for me also when dendextend is loaded.
Related