[英]How do I calculate weighted degree distributions with igraph in R?
考虑一个数据帧df
,其中前两列是节点对,连续列V1
, V2
,..., 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
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.