Reputation: 3751
While performing sparse Hierarchical clustering in R using sparcl package in R, I could not obtain the cluster labeling for the data. In the help documentation, they have the following code:
# Generate 2-class data
set.seed(1)
x <- matrix(rnorm(100*50),ncol=50)
y <- c(rep(1,50),rep(2,50))
x[y==1,1:25] <- x[y==1,1:25]+2
# Do tuning parameter selection for sparse hierarchical clustering
perm.out <- HierarchicalSparseCluster.permute(x, wbounds=c(1.5,2:6), nperms = 5)
# Perform sparse hierarchical clustering
sparsehc <- HierarchicalSparseCluster(dists=perm.out$dists,
wbound=perm.out$bestw, method="complete")
Now, how do I get the cluster label from object sparsehc is my question?
For Kmeans, we have a simple attribute "cs" created. Eg.
## Choosing tuning parameters
km.perm <- KMeansSparseCluster.permute(data_mat, K = 10, wbounds= seq(3,7, len =
20), nperms=5)
## Performing kmean sparce clustring
sparse_data_clus <- KMeansSparseCluster(data_mat, K = 10, wbounds= km.perm$bestw)
clusterlabel <- sparse_data_clus[[1]]$Cs
How can I get a similar label in HierarchicalSparseCluster()?
Thanks!
Upvotes: 0
Views: 518
Reputation: 1
It's a little late to respond to this, but I had the same problem. Here's what worked for me:
set.seed(1)
x <- matrix(rnorm(100*50),ncol=50)
y <- c(rep(1,50),rep(2,50))
x[y==1,1:25] <- x[y==1,1:25]+2
data_mat <- x
Do the permutation on the matrix you created
hier.perm <- HierarchicalSparseCluster.permute(data_mat,
wbounds= seq(3,7, len = 20),
nperms=5)
Run HierarchicalSparse
on the results
hier.sparse <- HierarchicalSparseCluster(dists=hier.perm$dists,
wbound=hier.perm$bestw,
method='complete')
Run hclust
on the value $u
from previous line, then use cutree
to divvy it up as you'd like
cluster = hclust(dist(hier.sparse$u))
cutree(cluster,3)
Upvotes: 0
Reputation: 77454
Hierarchical clustering will usually return a dendrogram (i.e. a hierarchy of clusters, with single elements at the bottom and the complete data set at the top), not a strict partitioning.
If you want a strict partitioning (such as produced by regular k-means), you will have to extract such a partitioning from this hierarchy. There are many methods available to do so, the simplest is to use a threshold level.
As I don't use R a lot (too slow), I cannot give you details here though. Have a look at ?cutree
.
Upvotes: 1