如何为网络创建度相关矩阵

How to create a degree correlation matrix for a network

我想为网络创建一个度相关矩阵,其中的列和行捕获网络的度。我不是在寻找一个全局度量——比如 assortativity_degree(),而是一个实际的相关矩阵,其中矩阵中的每个元素都是图中存在的边数,对于度数 = whatever 和度数 =任何。我在 igraph 文档中进行了深入研究,并在 Google 上进行了搜索,但没有找到我想要的东西。我拼凑了以下内容,这似乎可行,但我想知道是否有一种我不知道的更直接的方法。我不认为我追求的是深奥到没有人想过它——也许 igraph 或类似的函数中有一个函数,我只是不太知道它叫什么。

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

g <- make_graph("Zachary")

x <- sort(unique(degree(g))) # vector of all degrees in the network
y <- sort(unique(degree(g))) # vector for all degrees in the network

datalist = list()

# this loop creates a vector that identifies the number of 
# edges that occur between nodes of degree whatever and degree whatever
for(i in y) {               
 row <- mapply(function(x) 
 {length(E(g)[V(g)[degree(g) == i] %--% V(g)[degree(g) == x]])},
 x)      
 datalist[[i]] <- row 
}

# takes the data list created in the previous for loop and row bind it into a 
# matrix
m = do.call(rbind, datalist)

# label rows and columns with the relevatn degree
rownames(m) <- unique(sort(degree(g)))
colnames(m) <- unique(sort(degree(g)))

m
#>    1 2 3 4 5 6 9 10 12 16 17
#> 1  0 0 0 0 0 0 0  0  0  1  0
#> 2  0 0 0 3 0 1 2  1  5  3  7
#> 3  0 0 2 3 1 3 1  1  0  3  2
#> 4  0 3 3 1 3 1 2  2  2  3  3
#> 5  0 0 1 3 0 1 1  2  2  2  3
#> 6  0 1 3 1 1 0 1  1  1  2  1
#> 9  0 2 1 2 1 1 0  1  0  1  0
#> 10 0 1 1 2 2 1 1  0  1  1  0
#> 12 0 5 0 2 2 1 0  1  0  0  1
#> 16 1 3 3 3 2 2 1  1  0  0  0
#> 17 0 7 2 3 3 1 0  0  1  0  0

reprex package (v2.0.0)

于 2021-06-19 创建

我们可以通过创建度数图来做类似下面的事情,即 g.dg

dg <- degree(g)
g.dg <- graph_from_data_frame(
    with(
        get.data.frame(g),
        data.frame(dg[from], dg[to])
    ),
    directed = FALSE
)
mat <- get.adjacency(g.dg, sparse = FALSE)
ord <- order(as.numeric(row.names(mat)))
out <- mat[ord, ord]

这给出了

   1 2 3 4 5 6 9 10 12 16 17
1  0 0 0 0 0 0 0  0  0  1  0
2  0 0 0 3 0 1 2  1  5  3  7
3  0 0 2 3 1 3 1  1  0  3  2
4  0 3 3 1 3 1 2  2  2  3  3
5  0 0 1 3 0 1 1  2  2  2  3
6  0 1 3 1 1 0 1  1  1  2  1
9  0 2 1 2 1 1 0  1  0  1  0
10 0 1 1 2 2 1 1  0  1  1  0
12 0 5 0 2 2 1 0  1  0  0  1
16 1 3 3 3 2 2 1  1  0  0  0
17 0 7 2 3 3 1 0  0  1  0  0