avgoustisw
avgoustisw

Reputation: 223

How to create a degree correlation matrix for a network

I would like to create a degree correlation matrix for a network, where the columns and rows capture the degrees of the network. I am not looking for a global measure--like assortativity_degree(), but an actual correlation matrix, where each element in the matrix is the number of edges that exist in the graph for nodes with degree = whatever and degree = whatever. I have dug around in the igraph documentation and Googled but nothing quite gets to what I want. I have cobbled together the following, which seems to work, but I'm wondering if there is a more straightforward method that I'm ignorant. I don't think what I'm after is so esoteric that no one else has thought about it--and perhaps there is a function in igraph or the like and I just don't quite know what it's called.

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

Created on 2021-06-19 by the reprex package (v2.0.0)

Upvotes: 3

Views: 856

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101247

We can do something like below by creating a graph of degrees, i.e., 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]

which gives

   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

Upvotes: 3

Related Questions