1

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?

rk567
  • 289
  • 1
  • 4
  • 16
  • 1
    `1/c` gets you close. (BTW its not good practice to call other objects `c` as it is a R function) – user20650 Nov 18 '14 at 23:05
  • 1
    `as.dist(1/c)` yields the result in your question, but I'd recommend `igraph` for clustering graphs (see answer). – jlhoward Nov 19 '14 at 01:54

2 Answers2

4

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))

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • But how do I add labels for the dendogram? like replace numbers on x axis to some labels? V(g)$name <- c("a","b","c","d","e") and then? – rk567 Nov 19 '14 at 22:12
  • Never mind! got it. I had to include that line before bc <- edge.betweenness.community(g). – rk567 Nov 19 '14 at 23:09
  • 1
    Or set the `colnames(c)` before creating the graph. – jlhoward Nov 20 '14 at 16:16
0

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

darwin
  • 433
  • 2
  • 7