This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Use Clusgap() When The Cluster Method Is Hierarchical Clustering?

Hello, Biostars.I am new in R and statistics.I am using package 'cluster' in R to analyse microarray data. "FUNcluster" in the "clusGap" needs a argument k while hierarchical clustering method like hclust() has no argument k. What should I do? Best wishes.

r clustering

1 answer

You can use the function cutree(tree, k = NULL, h = NULL) with k parameter as follows to define the cluster function for clusGap:

mycluster <- function(x, k) list(cluster=cutree(hclust(dist(x), method = "average"),k=k))

Edit: According to ?clusGap, FUNcluster needs to be a list with one element named 'cluster' containing integer numbers for the clusters.

Thanks! I did following your advice .But there is an error.

llibrary(cluster)
mydist <- function(x) as.dist((1-cor(t(x)))/2)
mycluster <- function(x, k) cutree(hclust(mydist(x), method = "average"),k=k)
myclusGap <- clusGap(data.selection03,
                                   FUN = mycluster, 
                                   K.max = 10, 
                                   B = 100)

Error: $ operator is invalid for atomic vectors

I checked the code of clusGap. Maybe something is wrong with cutree.

Sorry, didn't have time to test the code. I guess mycluster has to return a list, this is documented in the help page of clusGap(). I'm sure you can figure it out.

Try mycluster <- function(x, k) list(cluster=cutree(hclust(mydist(x), method = "average"),k=k))

Yeah,you are right.Thank you.

Log in to answer this question.