Mario GS
Mario GS

Reputation: 879

Weighted Adjacency matrix igraph and R

I would kindly ask you for your help. I have a data structure similar than the one bellow:

  ID    Name
  1      A
  2      B
  1      C
  1      B
  2      C
  2      D
  3      A
  3      B 

The "ID" column, is a unique identifier for a paper, and the "Name" column stands for the name of an author that has a collaboration in a paper. I need to produce an undirected, weighted Adjacency Matrix. The nodes(or vertex) in the matrix will be pairs of authors (ij) in the "Name" column. The weighted values in the matrix (ij, i≠j) will be given by the sum of a collaboration ratio, using an algorithm that I can describe with an example:

For instance, pair A-B, in the data above, has two collaborations together in paper "1" and "3". Further, paper 1 has 3 collaborators (A,B,C); and paper 3 has 2 collaborators (A,B). The weight for the node AB will be the sum of the inverted number of collaborators minus 1 for each paper in column ID from which A and B has participated together. i.e.,

                                    Paper 1          Paper 3
                   Collaborators   A, B, C = 3      A, B = 2
                   Weight          1 / (3 - 1)     1 / (2 - 1)

Sum of the Weight(AB) = (1/2)+(1) = (3/2)
Note that if if A and B has collaborated in a paper the numerator will be one, otherwise 0.The key of the algorithm would be to compute this sum for each pair of authors in column "Name" and produce a weighed adjacency matrix.

Thank for your help and suggestions

Mario.

Upvotes: 0

Views: 671

Answers (1)

Gabor Csardi
Gabor Csardi

Reputation: 10825

What you have is a bipartite graph, and you need the unipartite projection of it. This is easy:

## Sample data
data <- "  ID    Name
  1      A
  2      B
  1      C
  1      B
  2      C
  2      D
  3      A
  3      B
"

## Read it in an edge list
el <- read.table(textConnection(data), header = TRUE)

## Create the bipartite graph from it
G <- graph.data.frame(el, directed = FALSE)
V(G)$type <- grepl("[0-9]+", V(G)$name)
G
#> IGRAPH UN-B 7 8 -- 
#> + attr: name (v/c), type (v/l)
#> + edges (vertex names):
#> [1] 1--A 2--B 1--C 1--B 2--C 2--D 3--A 3--B


## Project it to authors (and papers)
G_one <- bipartite.projection(G)

## You only need the authors
GA <- G_one[[1]]

## Create an adjacency matrix form it
GA[]
#> 4 x 4 sparse Matrix of class "dgCMatrix"
#>   A B C D
#> A . 2 1 .
#> B 2 . 2 1
#> C 1 2 . 1
#> D . 1 1 .

Upvotes: 0

Related Questions