如何解决如何为网络创建度相关矩阵
我想为网络创建一个度相关矩阵,其中列和行捕获网络的度。我不是在寻找一个全局度量——比如 assortativity_degree(),而是一个实际的相关矩阵,其中矩阵中的每个元素是图中存在的边数,用于度数 = 任意和度 = 任意的节点。我已经在 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 年 6 月 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。