benmaq
benmaq

Reputation: 148

How to cut the dendrogram with varclus in R?

I am using varclusfrom the packageHmisc to perform a clustering of variables (my variables are all numeric). However, I can't find a way to get more information about the clusters I obtained. I tried cutree from hclust but it didn't work. The only information on the clusters and the variables contained in them I can get is by visualizing the tree, but it is not very useful...

Does someone know a similar function than cutree for varclus? Or another function to perform clustering of variables? I am a new R user so any help is very welcomed!

Here is an example of a dendrogram I want to cut.

install.packages('dprep')
library(dprep)
library(datasets)

mxionosphere <- data.matrix(ionosphere)
v <- varclus(mxionosphere)
plot(v)

I want to cut the dendrogram I obtain as output.

Thanks a lot!

Upvotes: 1

Views: 1557

Answers (1)

Andrew Haynes
Andrew Haynes

Reputation: 2640

You can still use cutree() for varclus() you just need to extract the hclust object first. You can also do this using the ClustOfVar package with the hclustvar() and cutreevar() function:

Using Varclus():

You can use the cutree function, you just have to extract the hclust object from v first. This works because the clustering done in the varclus() function is actually done by hclust(). See ?Hmisc::varclus

Then your code can be used as below:

library(dprep)
library(datasets)
mxionosphere <- data.matrix(ionosphere)
v <- varclus(mxionosphere)
groups <- cutree(v$hclust, 10)

which will output which cluster each variable belongs to, just as it does for hclust.

Using ClustOfVar package: (https://cran.r-project.org/web/packages/ClustOfVar/ClustOfVar.pdf)

Similar to varclus(), hclustvar() will perform hierarchical clustering on variables. You can then use cutreevar() to cut the dendogram into k groups.

libary(ClustOfVar)
h<-hclustvar(mxionosphere)
clusters<-cutreevar(h, k=10)
groups<-clusters$cluster ##extract clusters values similar to cutree()

Upvotes: 1

Related Questions