Question: How To Use Clusgap() When The Cluster Method Is Hierarchical Clustering?
0
liux.bio340 wrote:

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 • 8.1k views
modified 6.8 years ago by Michael Dondrup47k • written 6.8 years ago by liux.bio340
1
Michael Dondrup47k wrote:

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.

``````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))`