组合网络分析和igraph / r问题。它是cross posted with Mathematics(我希望不会被嘲笑)。
我试图找到无向加权图的共生系数。边缘具有权重,反映了节点之间的价值流(如果您感到好奇,那就是交易商之间交换货物的市场图表。)
我看到协调性的标准Newman (2002)实现可以应用于加权网络,用度数强度替换度数,ala
(其中 i 索引图形的 M 边缘,F(i)表示通过链接 i 连接的两个节点的集合,以及 s_ \ phi是强度或总权重;连接到节点\ phi 的边权重之和。)
我看到这是由r包igraph通过assortativity(g, types1 = graph.strength(g))
实现的,其中types1是节点权重。
如果 s_ \ phi 只是度数,那么可以使用总或剩余度数,你可以得到相同的答案。但是,使用总权重(graph.strength(g)
以上),我担心在我的协同性指标中引入向上偏差,因为每个节点权重包括我们迭代的边权重(连接这两个节点的边)。虽然我现在不能指出一个引用,但似乎我们应该找到剩余权重的相关性。
也就是说,上面的 s_ \ phi 是连接到节点 \ phi 的边的权重之和,包括连接它们的边,它应该是剩余重量;连接到节点 \ phi 的边的权重之和,边 i 除外。
首先,我很好奇这是否是一个问题?使用这种类型的图表,使用节点权重的共同性会偏向我的相关估计吗?这没关系吗?
其次,我很好奇是否可以使用r中的igraph包或networkX python库。也许是加权度依赖的最近邻度的实现?看起来不像,但我想我会问。
我汇总了一个例子来向你展示我的意思:
library(igraph)
g <- read.table(
text =
" a b c d e f
a 0 4 5 8 1 0
b 4 0 1 4 8 9
c 5 1 0 4 1 0
d 8 4 4 0 2 7
e 1 8 1 2 0 1
f 0 9 0 7 1 0
",
header = T
)
g <- as.matrix(g)
g <- graph.adjacency(g, mode="undirected", weighted=TRUE)
E(g)$width <- E(g)$weight; plot(g)
assortativity.degree(g) # degree assortativity (ie unweighted)
[1] -0.4444444
assortativity(g, types1 = graph.strength(g), directed = F) #assortativity with weights
[1] -0.2421219
assortativity.weightEdge <- function(g){
linkedNodes <- data.frame(
n1 = rep(NA, length(E(g))),
n2 = rep(NA, length(E(g))),
s1 = rep(NA, length(E(g))),
s2 = rep(NA, length(E(g))),
s1_remaining = rep(NA, length(E(g))),
s2_remaining = rep(NA, length(E(g))),
k_nn_1 = rep(NA, length(E(g))),
k_nn_2 = rep(NA, length(E(g)))
)
# standard Newman 2002, http://arxiv.org/pdf/cond-mat/0205405v1.pdf
# but usign the "remaining strengths"
num1 = 0
num2 = 0
den1 = 0
#iterate over edges
for (i in 1:length(E(g))){
n1 = ends(g,E(g))[i,1]
n2 = ends(g,E(g))[i,2]
s1 = sum(g[n1,]) - g[n1,n2] # stregth of "remaining" connected nodes
s2 = sum(g[n2,]) - g[n1,n2] # "
linkedNodes$n1[i] <- n1
linkedNodes$n2[i] <- n2
linkedNodes$s1_remaining[i] <- s1
linkedNodes$s2_remaining[i] <- s2
num1 = num1 + s1 * s2
num2 = num2 + s1 + s2
den1 = den1 + (s1^2 + s2^2)
}
num1 = num1 / length(E(g))
num2 = num2 / (length(E(g)) * 2)
num2 = num2 * num2
den1 = den1 / (length(E(g)) * 2)
print(
paste(
"Assortativity, remaining weights:",
(num1-num2) / (den1-num2))
)
##########
num1 = 0
num2 = 0
den1 = 0
#iterate over edges
for (i in 1:length(E(g))){
n1 = ends(g,E(g))[i,1]
n2 = ends(g,E(g))[i,2]
s1 = sum(g[n1,])
s2 = sum(g[n2,])
linkedNodes$s1[i] <- s1
linkedNodes$s2[i] <- s2
num1 = num1 + s1 * s2
num2 = num2 + s1 + s2
den1 = den1 + (s1^2 + s2^2)
}
num1 = num1 / length(E(g))
num2 = num2 / (length(E(g)) * 2)
num2 = num2 * num2
den1 = den1 / (length(E(g)) * 2)
print(
paste(
"Assortativity, total weights:",
(num1-num2) / (den1-num2))
)
return(linkedNodes)
}
assortativity.weightEdge(g)
[1] "Assortativity, remaining weights: -0.3579013116802"
[1] "Assortativity, total weights: -0.242121917988836"
n1 n2 s1 s2 s1_remaining s2_remaining k_nn_1 k_nn_2
1 a b 18 26 14 22 NA NA
2 a c 18 11 13 6 NA NA
3 a d 18 25 10 17 NA NA
4 a e 18 13 17 12 NA NA
5 b c 26 11 25 10 NA NA
6 b d 26 25 22 21 NA NA
7 b e 26 13 18 5 NA NA
8 b f 26 17 17 8 NA NA
9 c d 11 25 7 21 NA NA
10 c e 11 13 10 12 NA NA
11 d e 25 13 23 11 NA NA
12 d f 25 17 18 10 NA NA
13 e f 13 17 12 16 NA NA
对于某些证据,请注意总权重与igraph的匹配。并且如所怀疑的那样,剩余的权重比例与总权重相比略低。