diagonals in a network

2 minute read

A typical example in which diagonals can be helpful is Social Network Analysis. For example, if we use matrices to represent friendship perceptions between individuals, then we need a dyadic matrix.

# generate a dyadic matrix for 3 individuals
m <- matrix(sample(0:1, 9, replace=TRUE), nrow=3, ncol=3)
m
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    0
## [3,]    0    1    0

Let says that we want to look at second-order connections (i.e. friends of friends). If we now want to represent the data from both time period in a single object, we need a 4-dimensional array. Higher-order arrays are hard to visualise, another way of doing this is by representing two dimensions along each of the two edges of a matrix. We can do this using the Knonecker Product (denoted ⊗), which we can call in R using the alias %x%.

M <- m %x% m
M
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1    1    1    1    1    1    1    1    1
##  [2,]    1    1    0    1    1    0    1    1    0
##  [3,]    0    1    0    0    1    0    0    1    0
##  [4,]    1    1    1    1    1    1    0    0    0
##  [5,]    1    1    0    1    1    0    0    0    0
##  [6,]    0    1    0    0    1    0    0    0    0
##  [7,]    0    0    0    1    1    1    0    0    0
##  [8,]    0    0    0    1    1    0    0    0    0
##  [9,]    0    0    0    0    1    0    0    0    0

Feelings of friendship towards oneself aren’t always particularly insightful. We can now use the diagonals library to eliminate those.

# load the library
library(diagonals)
##
## D I
## A G
##     O N
##     A L
##         S
# remove the elements along the diagonal of width 2
minus_block_matrix(M, size=3)
## Error in eval(expr, envir, enclos): could not find function "minus_block_matrix"

The diagonals package now available on CRAN and can therefore be install directly from inside R using:

install.packages("diagonals")

Subsequently the package can be loaded using:

library(diagonals)

The above demonstration is also available as a vignette that is included in the package. It can be accessed from R using:

vignette("network")

A general introduction to diagonals is available in next weeks post: diagonals. This post is also available as a vignette that is included in the package

For more information on the package and its development please see yesterday’s post diagonals on CRAN.