http://seqanswers.com/forums/showthread.php?p=115844#post115844
I was plotting a ChIP-seq data using the pheatmap, see code below:
km<- kmeans(m1,2) # determine 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 I control this?
The answer is to use the set.seed() function.
http://stackoverflow.com/questions/7501035/k-means-same-clusters-for-every-execution/7501152#7501152
http://stackoverflow.com/questions/13605271/reasons-for-using-the-set-seed-function
http://stackoverflow.com/questions/18215638/argument-of-set-seed-in-r
"Yes, calling
set.seed(foo)
immediately prior to running kmeans(....)
will give the same random start and hence the same clustering each time. foo
is a seed, like 42
or some other numeric value.""The
seed
argument in set.seed
is a single value, interpreted as an integer (as defined inhelp(set.seed())
. The seed
in set.seed
produces random values which are unique to thatseed
(and will be same irrespective of the computer you run and hence ensures reproducibility). So the random values generated by set.seed(1)
and set.seed(123)
will not be the same but the random values generated by R in your computer using set.seed(1)
and by R in my computer using the same seed
are the same.""For a normal RNG of decent quality, the value doesn't matter. "42" is a reference to a famous book; other people use their birthday or "123" or just "1"."
No comments:
Post a Comment