Question: K-Means Clustering And Heatmap
0
6.4 years ago by
Ming Tang2.5k
Houston/MD Anderson Cancer Center
Ming Tang2.5k wrote:

Hi everyone,

I was plotting a ChIP-seq data using the pheatmap, see code below:

``````km<- kmeans(m1,2) # determin how many cluster you want, I specify 2 here

m.kmeans<- cbind(m1, km\$cluster) # combine the cluster with the matrix

dim(m.kmeans)
# [1] 903 602
# the last column is 602
o<- order(m.kmeans[,602]) # order the last column

m.kmeans<- m.kmeans[o,] # order the matrix according to the order of the last column

pheatmap( m.kmeans[,1:601], cluster_rows = F, cluster_cols = F, col= hmcols, breaks = bk, legend=FALSE, show_rownames=FALSE, show_colnames=FALSE)
``````

It works fine for me, I clustered the data to two groups by specifying K=2, the problem is that group 1 sometimes shows up in the upper part of the heatmap, sometimes it shows up in the bottom part of the figure if I plot it several times.

I think it has to do with the assignment of the group number, say, the first group is assigned to 1, the other is assigned to 2. However, next time if you plot the same data, the first group assigned to 2, the other is assigned to 1. R randomly assigns the number to the groups.

How can control this?

Thank you every much!

heatmap • 10.0k views
modified 6.4 years ago by Madelaine Gogol5.1k • written 6.4 years ago by Ming Tang2.5k
1
6.4 years ago by
Kansas City

Try set.seed(1) in your code before the kmeans call.

http://stackoverflow.com/questions/7501035/k-means-same-clusters-for-every-execution/7501152#7501152

Thank you! but I am not sure how to set the seed argument of the set.seed() function though...

you just do

set.seed(1) or set.seed(2)

or whatever. That's it...