0

I want to find 1) the modularity of a network and find 2) the identities of the nodes in each community.

I think this is the way to get modularity:

g <- graph.full(5) %du% graph.full(5) %du% graph.full(5)
g <- add.edges(g, c(1,6, 1,11, 6, 11))
ebc <- edge.betweenness.community(g)

sizes(ebc)
#Community sizes
#1 2 3 
#5 5 5 

modularity(g,membership(ebc))
#[1] 0.5757576

but on this link by Gabor I get this code:

memberships <- list()
G <- graph.full(5) %du% graph.full(5) %du% graph.full(5)
G <- add.edges(G, c(1,6, 1,11, 6, 11))

### edge.betweenness.community
ebc <- edge.betweenness.community(G)
mods <- sapply(0:ecount(G), function(i) {
  g2 <- delete.edges(G, ebc$removed.edges[seq(length=i)])
  cl <- clusters(g2)$membership
  modularity(G, cl)
})

g2 <- delete.edges(G, ebc$removed.edges[1:(which.max(mods)-1)])
memberships$`Edge betweenness` <- clusters(g2)$membership

This seems to be doing the same thing that I am, I think that the delete.edges and clusters is about splitting the modules into separate components by deleting the connecting edges and then getting the IDs of nodes in each component.

Although there are a few questions I have:

  1. In @Gabor Csardil 's code why does the modularity call use clusters(G)$membership and not ebc like I did in my example? What is the difference? (I would have thought that would overestimate actual modularity?) There also seems to be an alternate using community.to.membership

  2. The mods code returns this error which I do not understand which is partly why I cannot explore g2 and cl to see more of what is happening:

Error in delete.edges(G, ebc$removed.edges[seq(length = i)]) : At iterators.c:1809 : Cannot create iterator, invalid edge id, Invalid vertex id

SlowLoris
  • 995
  • 6
  • 28
user1320502
  • 2,510
  • 5
  • 28
  • 46

1 Answers1

3

The general answer is that the code in the wiki is outdated. It works with igraph version 0.5.x, but not with 0.6.x. More specifically:

  1. In igraph version 0.5.x there was no direct way to get the membership vector, in version 0.6.x you can just say membership(ebc).

  2. In version 0.5.x vertex ids start with zero, in version 0.6.x they start with one, so you don't meed to subtract one in the delete.edges() line.

Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
  • thank you Gabor so `membership(ebc)` does give me what I want. Although the `delete.edges` call inside the `mod` was the one causing therror and I this doesnt have -1 subtraction for zero indexing. i.e `g2 <- delete.edges(G, ebc$removed.edges[seq(length=i)])` – user1320502 Jan 15 '13 at 14:57
  • I see, it is the same error, anyway, because `$removed.edges` is zero-based, I guess. Anyway, it does not really matter, just use `membership()` and `cutat()` if you want to cut the dendrogram somewhere else. – Gabor Csardi Jan 15 '13 at 15:10
  • this [link](http://www.sixhat.net/finding-communities-in-networks-with-r-and-igraph.html) though seems to suggest that `mods` is about getting a range of modularities for each possible split. Don't you need this to work out which possible split has the highest modularity? or does `membership` give you the division with the highest modularity already now? – user1317221_G Jan 15 '13 at 15:35
  • nope modularity does give the highest split `?communities` "modularity gives the modularity score of the partitioning. (See modularity.igraph for details. For algorithms that do not result a single partitioning, the highest modularity value is returned." – user1317221_G Jan 15 '13 at 15:40