【问题标题】:How to create a degree correlation matrix for a network如何为网络创建度相关矩阵
【发布时间】:2021-06-20 04:52:42
【问题描述】:

我想为网络创建一个度数相关矩阵,其中的列和行捕获网络的度数。我不是在寻找一个全局度量——比如 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-06-19 创建

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    我们可以通过创建度数图来执行以下操作,即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
    

    【讨论】:

    • 这是天才。谢谢你。你能向我解释一下 with() 函数内部发生了什么吗?我看到的是它正在应用数据。 frame 函数从图表中移到边缘列表——但我无法从概念上理清它在做什么。
    • @avgoustisw 你看到get.data.frame(g) 有两列,fromto。使用with,我们进入get.data.frame(g)的环境,可以使用fromto作为数据的名称(而不是get.data.frame(g)$fromget.data.frame(g)$to)。如果你输入?with,你会看到更多的例子和解释。
    猜你喜欢
    • 2013-09-15
    • 2012-05-27
    • 2022-01-12
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多