I would like to make a heatmap with row clustering based on cosine distances. I'm using R and heatmap.2() for making the figure. I can see that there's a dist parameter in heatmap.2 but I cannot find a function to generate the cosine dissimilarity matrix. The builtin dist function doesn't support cosine distances, I also found a package called arules with a dissimilarity() function but it only works on binary data.
Tell me more
×
Cross Validated is a question and answer site for
statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.
|
|
|||||||||
|
|
As @Max indicated in the comments (+1) it would be simpler to "write your own" than to spend time looking for it somewhere else. As we know, the cosine similarity between two vectors $A,B$ of length $n$ is $$ C = \frac{ \sum \limits_{i=1}^{n}A_{i} B_{i} }{ \sqrt{\sum \limits_{i=1}^{n} A_{i}^2} \cdot \sqrt{\sum \limits_{i=1}^{n} B_{i}^2} } $$ which is straightforward to generate in
Then the matrix |
|||||||
|