簡體   English   中英

如何用R中的igraph計算加權度分布?

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

考慮一個數據幀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$weightsE(g)$weights g$weights不同?

函數graph.strength可以使用權weights參數給出權重向量。 我認為你的代碼出了什么問題,你應該調用權重屬性E(g)$weight而不是E(g)$weights

我通過獲取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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM