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