如何使用R中的IGRAPHE计算加权度分布?
考虑一个数据帧如何使用R中的IGRAPHE计算加权度分布?,r,igraph,R,Igraph,考虑一个数据帧df,其中前两列是节点对,后续列V1,V2,…,Vn表示节点之间的流(可能为0,表示该列的网络没有边缘)。我想使用流量作为权重,对度、社区检测和其他网络度量进行分析 然后根据V1I中的权重分析图表: # create graph and explore unweighted degrees with respect to V1 g <- graph.data.frame( df[df$V1!=0,] ) qplot(degree(g)) x <- 0:max(degre
df
,其中前两列是节点对,后续列V1
,V2
,…,Vn
表示节点之间的流(可能为0,表示该列的网络没有边缘)。我想使用流量作为权重,对度、社区检测和其他网络度量进行分析
然后根据V1
I中的权重分析图表:
# 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))
我一定是设置了错误的权重,做
E(g)$weights函数图是不够的。可以用weights
参数给strength
一个权重向量。我认为代码中的错误在于,您应该调用权重属性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
}
graph.strength.distribution重新定义g$weights
和E(g)$weights
之间的差异:g$weights
查找名为weights
的图形属性,E(g)$weights
查找名为weights
的边属性。顺便说一句,还有V(g)$weights
,它查找名为weights
的顶点属性。graph.strength文档(现在有问题的链接)表明它查找名为weight的图形属性,但是在我的示例中,一旦我将类型固定为E(g)$weight,graph.strength
就会按预期工作。为什么?它是否先搜索graph属性,然后搜索edge属性?查找graph属性没有意义,因为这样的属性将附加到graph本身,而不是单个边<代码>图形。强度
仅查找边缘属性。文档中说“如果图形有一个权重
边属性…”,那么我想很明显这里需要一个边属性。好的,我最终会学会阅读。谢谢这确实很有用。你介意把它也添加到igraph
wiki吗?这是指向相应wiki页面的链接:
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
}