# Algorithm for choosing the number of clusters when using pam in R?

I am clustering a dataset using the pam command (from {cluster} package), and I wish to decide on the number of clusters to use.

I was able to implement The_Elbow_Method in R (see wiki) for doing that. But that doesn't provide me with any solid criteria (like AIC, for example) for decision.

I came by the {clValid} package which looks promising, but I wanted to know if there are any other R solutions (you know of) for choosing the number of clusters for pam?

Here's some dummy code if someone wants to show examples:

data(iris)
require(cluster)
pam(iris[,1:4], 3)

• I came across a package recently that is supposed to help with this and they reference using it with pam. I haven't tried it yet but it looks promising. Check out the vignette. cran.r-project.org/web/packages/clues/index.html – Robert Nov 18 '10 at 20:58

You may find an answer to a similar question useful. I have also used clValid but, as I recall, it was rather slow (at least for relatively large datasets).

• @Tal Galili You are welcome Tal! If you find out some other method for choosing the number of clusters, share it with us. – George Dontas Sep 16 '10 at 21:12

The fpc package provides a few clustering statistics. If you're looking for information criteria in particular, the cluster.stats method provides an information based distance. For mixture models based on clustering, the BIC is available.

• fpc also has pamk, which can detect the number of clusters. – Sam Brightman Dec 30 '13 at 12:02