igraph中加权图的模块化计算 [英] Modularity calculation for weighted graphs in igraph
问题描述
我在igraph中使用fastgreedy算法在加权无向图中进行社区检测.之后,我想看一下模块化,对于不同的方法,我得到了不同的价值,我想知道为什么.我提供了一个简短的示例,演示了我的问题:
I used the fastgreedy algorithm in igraph for my community detection in a weighted, undirected graph. Afterwards I wanted to have a look at the modularity and I got different values for different methods and I am wondering why. I included a short example, which demonstrates my problem:
library(igraph)
d<-matrix(c(1, 0.2, 0.3, 0.9, 0.9,
0.2, 1, 0.6, 0.4, 0.5,
0.3, 0.6, 1, 0.1, 0.8,
0.9, 0.4, 0.1, 1, 0.5,
0.9, 0.5, 0.8, 0.5, 1), byrow=T, nrow=5)
g<-graph.adjacency(d, weighted=T, mode="lower",diag=FALSE, add.colnames=NA)
fc<-fastgreedy.community(g)
fc$modularity[3]
#[1] -0.05011095
modularity(g,membership=cutat(fc,steps=2),weights=get.adjacency(g,attr="weight"))
#[1] 0.07193047
我希望这两个值都相同,并且如果我尝试对未加权的图进行相同操作,则会得到相同的值.
I would expect both of the values to be identical and if I try the same with an unweighted graph, I get the same values.
d2<-round(d,digits=0)
g2<- graph.adjacency(d2, weighted=NULL, mode="lower",diag=FALSE, add.colnames=NA)
fc2<-fastgreedy.community(g2)
plot(fc2,g2)
fc2$modularity[3]
#[1] 0.15625
modularity(g2,membership=cutat(fc2,steps=2))
#[1] 0.15625
另一个用户遇到了类似问题,但是我遇到了当前版本的igraph,所以应该不是问题.有人可以向我解释为什么我看不到我的代码有区别还是有问题吗?
Another user had a similar problem, but I have the current version of igraph, so that should not be the problem. Can someone explain to me why there is a difference or is there a problem with my code I don't see?
推荐答案
行
modularity(g,membership=cutat(fc,steps=2),weights=get.adjacency(g,attr="weight"))
是错误的.如果要将边缘的权重传递给modularity()
,请使用E(g)$weight
:
is wrong. If you want to pass the weights of edges to modularity()
, do it with E(g)$weight
:
modularity(g, membership = cutat(fc, steps = 2), weights = E(g)$weight)
# [1] -0.05011095
这篇关于igraph中加权图的模块化计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!