如何使用R中的igraph计算加权度分布?

10
考虑一个数据框 df,其中前两列是节点对,接下来的列 V1V2、...、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 的输出与第一个没有任何区别。我做错了什么?
更新:
所以 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)$weights <- E(g)$V1 是否不足以满足要求?为什么 g$weights 可能与 E(g)$weights 不同?


2
关于“g$weights”和“E(g)$weights”的区别:g$weights查找名为“weights”的图形属性,而E(g)$weights查找名为“weights”的边缘属性。顺便说一下,还有V(g)$weights,它查找名为“weights”的顶点属性。 - Tamás
图形强度文档(现在链接在问题中)表明它正在寻找名为 weight 的 graph 属性,然而在我的例子中,一旦我将类型修正为 E(g)$weight,graph.strength 就像预期的那样工作了。为什么?它是先搜索图属性还是边属性? - mindless.panda
1
查找图属性是没有意义的,因为这样的属性将附加到图本身而不是单个边缘。graph.strength仅查找边缘属性。文档中说“如果图具有weight边缘属性...”,所以我想这里需要一个边缘属性。 - Tamás
好的,我最终会学会阅读。谢谢。 - mindless.panda
2个回答

7

函数graph.strength可以使用weights参数给定权重向量。我认为你的代码出了问题,应该调用权重属性E(g)$weight而不是E(g)$weights


4
我为了自己的代码创建了一个等效的用于加权图的“degree.distribution”功能,方法是对“degree.distribution”代码进行修改:
graph.strength.distribution <- function (graph, cumulative = FALSE, ...)
{
  if (!is.igraph(graph)) {
    stop("Not a graph object")
  }
  # graph.strength() instead of degree()
  cs <- graph.strength(graph, ...)
  hi <- hist(cs, -1:max(cs), plot = FALSE)$density
  if (!cumulative) {
    res <- hi
  }
  else {
    res <- rev(cumsum(rev(hi)))
  }
  res
}

这确实非常有用。您介意将它添加到 igraph 维基中吗?以下是适当的维基页面链接:http://igraph.wikidot.com/r-recipes - Tamás
谢谢分享!不确定这是否是正确的地方提问,但是,您有使用graph.strength.distribution()函数的示例吗?我查看了https://rdrr.io/github/abnormally-distributed/rsfcNet/man/strength_distribution.html,但由于我的R studio版本问题,在安装该软件包时出现错误,因此回到了这个帖子。 - Grace

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接