0

I have a problem using cluster.stats on two different hclust clusters. Is that not possible?

I am on Version 0.99.491 – © 2009-2015 RStudio, Inc. with below packages installed:

mylib <- "/Users/Klaus/R Packages/"; mylib
install.packages("NbClust",lib=mylib)
install.packages("modeltools",lib=mylib)
install.packages("flexclust",lib=mylib)
install.packages("RTextTools",lib=mylib) # Amazon's default machine image uses the Atlas BLAS. R points to that when installed with all defaults, but RTextTools expects the ordinary BLAS. 
install.packages("mclust",lib=mylib)
install.packages("fpc",lib=mylib)

library("NbClust",lib=mylib)
library("modeltools",lib=mylib)
library("flexclust",lib=mylib)
library("cluster",lib=mylib)
library("mclust",lib=mylib)
library("fpc",lib=mylib)

I am creating these clusters:

data(nutrient, package="flexclust")
row.names(nutrient) <- tolower(row.names(nutrient))
nutrient.scaled <- scale(nutrient)
d_eucli <- dist(nutrient.scaled,"euclidean")
fit.single <- hclust(d_eucli, method="single")
fit.average <- hclust(d_eucli, method="average")

But when using cluster.stats on them I get an error:

cluster.stats(d_eucli, fit.single$cluster, fit.average$cluster)

Error (In Danish setup):

Fejl i `[<-`(`*tmp*`, j, i, value = Inf) : subscript out of bounds
In addition: Advarselsbeskeder:
1: I max(clustering) : no non-missing arguments to max; returning -Inf
2: I cluster.stats(d_eucli, fit.single$cluster, fit.average$cluster) :
  clustering renumbered because maximum != number of clusters
3: I min(bv) : no non-missing arguments to min; returning Inf
4: I min(sij) : no non-missing arguments to min; returning Inf

What am I missing?

Charlie
  • 2,004
  • 6
  • 20
  • 40

2 Answers2

0

hclust will compute a dendrogram, not a partitioning.

To do that latter step, use cutree. Then you should be able to proceed.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

The following code should work , I tried it myself:

fit.single <- hclust(d_eucli, method="single")

fit.average <- hclust(d_eucli, method="average")

cut.single <- cutree(fit.single, k=3) 

you should specify the number of cluster (K) if you know it or based on others validation test

cut.average <- cutree(fit.average, k=3)

cluster.stats(d_eucli, cut.single, cut.average)
Draken
  • 3,134
  • 13
  • 34
  • 54
Zee H
  • 23
  • 5