Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clustering given a distance matrix 256x256

So, I have 256 objects, and have calculated the distance matrix (pairwise distances) among them. A subset of my distance matrix is given below:

> dm[1:10, 1:10]
      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
 [1,]  0  1  1  1  1  2  2  2  1   2
 [2,]  1  0  1  1  2  1  2  2  2   1
 [3,]  1  1  0  1  2  2  1  2  2   2
 [4,]  1  1  1  0  2  2  2  1  2   2
 [5,]  1  2  2  2  0  1  1  1  1   2
 [6,]  2  1  2  2  1  0  1  1  2   1
 [7,]  2  2  1  2  1  1  0  1  2   2
 [8,]  2  2  2  1  1  1  1  0  2   2
 [9,]  1  2  2  2  1  2  2  2  0   1
[10,]  2  1  2  2  2  1  2  2  1   0

> str(dm)
 int [1:256, 1:256] 0 1 1 1 1 2 2 2 1 2 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:256] "V1" "V2" "V3" "V4" ...

Now, I want to use this distance matrix to cluster those 256 objects accordingly. Therefore, I used hclust, but got an error:

> hclust(dm, method="single")
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed


> hclust(dm, method="complete")
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed

So, even if I use a smaller subset of the matrix, I would still get the same error:

> hclust(dm[1:10,1:10], method="complete")
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed

Any idea what is wrong with my analysis?

like image 404
Vahid Mirjalili Avatar asked Nov 26 '25 20:11

Vahid Mirjalili


1 Answers

dm needs to be an object of class "dist" in hclust.

So, you could calculate your dissimilarity matrix with the dist function and then, use the object inside hclust.

m_trix = matrix(data=1:2,nrow=10,ncol=10)

dm = dist(m_trix,method="euclidean")
cluster = hclust(dm, method="single")
plot(cluster)

Or you could use dist(m_trix) directly inside hclust:

cluster = hclust(dist(m_trix), method="single")
plot(cluster)
like image 99
Andre Silva Avatar answered Nov 28 '25 15:11

Andre Silva