How do I calculate weighted degree distributions with igraph in R?

跟風遠走 提交于 2019-12-10 02:26:37

问题


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?


回答1:


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.




回答2:


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
}


来源:https://stackoverflow.com/questions/8344565/how-do-i-calculate-weighted-degree-distributions-with-igraph-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!