Question: K-Means Clustering And Heatmap
0
gravatar for Ming Tang
5.0 years ago by
Ming Tang2.4k
Houston/MD Anderson Cancer Center
Ming Tang2.4k 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 • 7.6k views
ADD COMMENTlink modified 5.0 years ago by Madelaine Gogol5.0k • written 5.0 years ago by Ming Tang2.4k
1
gravatar for Madelaine Gogol
5.0 years ago by
Madelaine Gogol5.0k
Kansas City
Madelaine Gogol5.0k wrote:

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

ADD COMMENTlink written 5.0 years ago by Madelaine Gogol5.0k

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

ADD REPLYlink written 5.0 years ago by Ming Tang2.4k

you just do

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

or whatever. That's it...

ADD REPLYlink written 5.0 years ago by Madelaine Gogol5.0k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1917 users visited in the last hour