rmatrixigraph

How to convert an incidence matrix, of a bipartite network, to an unipartite projection


I would like to convert an incidence matrix of a bipartite procjection into a matrix of an unipartite projection.

For example: I have this matrix, in which the columns are interacting with the respective rows when element equals > 0.

int <- matrix(rbinom(48, 5, 0.2),nrow=8,ncol=6)
colnames(int) <- letters[1:6]
rownames(int) <- letters[7:14]

I would like to generate a adjacency (square matrix) of the higher nodes (the columns), in which the new elements will be the at least number they shared interaction with the same rows (g to n, for example).

So if my int matrix is this one:

  a b c d e f
g 1 0 0 1 0 1
h 1 0 2 1 1 1
i 1 1 1 0 0 2
j 1 2 0 1 0 1
k 1 1 3 0 2 1
l 1 1 2 3 1 0
m 3 2 2 1 0 1
n 2 1 0 1 1 0

My unipartite projection resulting will be like:

   a  b  c  d  e f
a  0  7  6  6
b     0  5
c        0
d           0
e              0   
f                 0

In which, for example the pairs ab shared interaction with six rows and the number will be the at least x numbers shared 1+1+1+1+2+1, which refers to interactions with the shared interactions with i, j, k, l, m and n.

Any ideas on how to perform this, please?


Solution

  • `diag<-`(crossprod(df>0), 0)
    
      a b c d e f
    a 0 2 3 3 3 0
    b 2 0 2 1 1 0
    c 3 2 0 3 2 0
    d 3 1 3 0 3 0
    e 3 1 2 3 0 0
    f 0 0 0 0 0 0