how to transform the following similarity matrix to distance matrix for performing hclust?

3.3k views Asked by At

I am trying to cluster nodes (C1, C2, C3...) of a graph using hclust and my similarity metric is number of links between nodes.

I have data like

c = matrix( c(0,1,3,1,0,5,3,5,0), nrow=3, ncol=3)

Basically this is a similarity matrix

    C1  C2  C3
C1  0   1   3
C2  1   0   5
C3  3   5   0

This is an undirected graph where similarity between C1 and C3 is 3 links. I need to transform this data to a suitable dist.matrix like

    C1  C2
C2  1
C3  1/3   1/5

format based on my similarity metric (#links between two nodes). How do I do this?

2

There are 2 answers

3
jlhoward On BEST ANSWER

It seems like you want to use hierarchical clustering based on edge-betweenness. You can do this directly in igraph.

library(igraph)
c  <- matrix( c(0,1,3,1,0,5,3,5,0), nc=3)
g  <- graph.adjacency(c,mode="undirected")
bc <- edge.betweenness.community(g)
par(mfrow=c(1,2))
plot(g)
plot(as.dendrogram(bc))

c  <- matrix(c(0,0,0,4,0, 
               0,0,0,1,0, 
               0,0,0,4,1, 
               4,1,4,0,3, 
               0,0,1,3,0),nc=5)
g  <- graph.adjacency(c,mode="undirected")
bc <- edge.betweenness.community(g)
plot(g)
plot(as.dendrogram(bc))

0
darwin On

Take a look at the daisy() function. http://www.inside-r.org/r-doc/cluster/daisy