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

时间:2022-06-14 21:45:42

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.

考虑一个数据帧df,其中前两列是节点对,连续列V1,V2,...,Vn表示节点之间的流(可能为0,暗示该列的网络没有边缘)。我想使用流量作为权重来对度,社区检测和其他网络度量进行分析。

Then to analyze the graph with respect to the weights in V1 I do:

然后分析关于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))

The output from the third qplot is no different than the first. What am I doing wrong?

第三个qplot的输出与第一个没有什么不同。我究竟做错了什么?

Update:

更新:

So graph.strength is what I am looking for, but graph.strength(g) in my case gives standard degree output followed by:

所以graph.strength是我正在寻找的,但在我的情况下graph.strength(g)给出了标准度输出,后跟:

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?

我必须正确设置重量,不足以做E(g)$权重< - E(g)$ V1以及为什么g $权重与E(g)$权重不同?

2 个解决方案

#1


7  

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.

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

#2


3  

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

我通过获取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
}

#1


7  

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.

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

#2


3  

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

我通过获取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
}