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

发布于 2024-12-19 09:58:08 字数 1055 浏览 2 评论 0原文

考虑一个数据帧 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 的输出与第一个 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$weightsE(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀中猫帐中妖 2024-12-26 09:58:08

可以通过 weights 参数为函数 graph.strength 提供一个权重向量。我认为您的代码中出现的问题是您应该调用权重属性 E(g)$weight 而不是 E(g)$weights

The function graph.strength can be given a weights vector with the weights argument. I think what is going wrong in your code is that you should call the weights attribute E(g)$weight not E(g)$weights.

谁的年少不轻狂 2024-12-26 09:58:08

我通过采用 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
}

I created an equivalent degree.distribution function for weighted graphs for my own code by taking the degree.distribution code and making one change:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文