Search code examples
rconsoleconsole-output

Retrieve best number of clusters from NbClust


Many functions in R provide some sort of console output (such as NbClust() etc.) Is there any way of retrieving some of the output (e.g. a certain integer value) without having a look at the output? Any way of reading from the console?

Imagine the output would look like the following output from example code provided in the package manual:

[1] "Frey index : No clustering structure in this data set"
*** : The Hubert index is a graphical method of determining the number of clusters.
                In the plot of Hubert index, we seek a significant knee that corresponds to a 
                significant increase of the value of the measure i.e the significant peak in Hubert
                index second differences plot. 

*** : The D index is a graphical method of determining the number of clusters. 
                In the plot of D index, we seek a significant knee (the significant peak in Dindex
                second differences plot) that corresponds to a significant increase of the value of
                the measure. 

******************************************************************* 
* Among all indices:                                                
* 1 proposed 2 as the best number of clusters 
* 2 proposed 4 as the best number of clusters 
* 2 proposed 6 as the best number of clusters 
* 1 proposed 7 as the best number of clusters 

                   ***** Conclusion *****                            

* According to the majority rule, the best number of clusters is  4 


*******************************************************************

How would I retrieve the value 4 from the last line of the above output?


Solution

  • It is better to work with objects rather than output in the console. Any "good" function would return hopefully structured output that can be accessed using $ or @ signs, use str() to see object's structure.

    In your case, I think this should work:

    length(unique(res$Best.partition))