如何在 R 中使用 igraph 计算加权度分布?
考虑一个数据帧 df
,其中前两列是节点对,连续列 V1
、V2
、...、Vn 表示节点之间的流量(可能为 0,意味着该列的网络没有边缘)。我想使用流量作为权重来对度、社区检测和其他网络度量进行分析。
然后根据 V1
中的权重分析图表,我这样做:
# create graph and explore unweighted degrees with respect to V1
g <- graph.data.frame( df[df$V1!=0,] )
qplot(degree(g))
x <- 0:max(degree(g))
qplot(x,degree.distribution(g))
# set weights and explore weighted degrees using V1
E(g)$weights <- E(g)$V1
qplot(degree(g))
第三个 qplot 的输出与第一个 qplot 没有什么不同。我做错了什么?
更新:
所以graph.strength
是我正在寻找的,但是 graph.strength(g)
在我的情况下给出了标准度输出,然后是:
Warning message:
In graph.strength(g) :
At structural_properties.c:4928 :No edge weights for strength calculation,
normal degree
我必须错误地设置权重,这还不够吗E(g)$权重<- E(g)$V1
以及为什么 g$weights
与 E(g)$weights
不同?
Consider a dataframe df
where the first two columns are node pairs and successive columns V1
, V2
, ..., Vn
represent flows between the nodes (potentially 0, implying no edge for that column's network). I would like to conduct analysis on degree, community detection, and other network measures using the flows as weights.
Then to analyze the graph with respect to the weights in V1
I do:
# create graph and explore unweighted degrees with respect to V1
g <- graph.data.frame( df[df$V1!=0,] )
qplot(degree(g))
x <- 0:max(degree(g))
qplot(x,degree.distribution(g))
# set weights and explore weighted degrees using V1
E(g)$weights <- E(g)$V1
qplot(degree(g))
The output from the third qplot is no different than the first. What am I doing wrong?
Update:
So graph.strength
is what I am looking for, but graph.strength(g)
in my case gives standard degree output followed by:
Warning message:
In graph.strength(g) :
At structural_properties.c:4928 :No edge weights for strength calculation,
normal degree
I must be setting the weights incorrectly, is it not sufficient to do E(g)$weights <- E(g)$V1
and why can g$weights
differ from E(g)$weights
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以通过
weights
参数为函数graph.strength
提供一个权重向量。我认为您的代码中出现的问题是您应该调用权重属性E(g)$weight
而不是E(g)$weights
。The function
graph.strength
can be given a weights vector with theweights
argument. I think what is going wrong in your code is that you should call the weights attributeE(g)$weight
notE(g)$weights
.我通过采用
Degree.distribution 代码并进行一项更改,为我自己的代码创建了一个等效的用于加权图的
Degree.distribution 函数:
I created an equivalent
degree.distribution
function for weighted graphs for my own code by taking thedegree.distribution
code and making one change: