-2

I'm woking with R and the eigenvector centrality algorithm from the igraph library. We have the following sql table :

person1 / person2 / score

A / B / 0.568

A / C / 1.233

B / A / 0.798

B / C / 0.493

C / A / 1.367

C / B / 1.276

The values are a score for the relationship between the two person.

Two question : - How can I create a matrix in R from the table above with the score as a weight ? Result

Person   A       B     C 
A        -     0.568  1.233
B      0.798     -    0.493
C     1.367    1.276   -
  • How can we take into a consideration the weight of the relationship to apply eigenvector centrality algorithm in R?
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Omar
  • 55
  • 2
  • 6

2 Answers2

2

To apply the edge-weights in the calculation of eigenvector centrality, simply reference the weights with the weights option of eigen_centrality:

rm(list=ls())

library(igraph)

# Some sample data, source: http://www.shizukalab.com/toolkits/sna/weighted-edgelists
el <- structure(list(V1 = c(23732L, 23732L, 23778L, 23778L, 23871L, 
23871L, 23871L, 58009L, 58098L, 58256L), V2 = c(23871L, 58098L, 
23732L, 23824L, 23778L, 58009L, 58098L, 58098L, 23778L, 58098L
), weight = c(1L, 10L, 8L, 1L, 15L, 1L, 5L, 7L, 1L, 1L)), .Names = c("V1", 
"V2", "weight"), class = "data.frame", row.names = c(NA, -10L
))

g <- graph.data.frame(el)

# Only showing the centrality scores, hence the $vector
eigen_centrality(g, directed=TRUE, weights=E(g)$weight)$vector

#     23732      23778      23871      58009      58098      58256      23824 
# 0.53685043 0.39782138 0.09055835 0.01527579 1.00000000 0.00000000 0.06710630 

To get the adjacency matrix of weights:

get.adjacency(g, attr='weight')
Michael Davidson
  • 1,391
  • 1
  • 14
  • 31
1

Just a quick and dirty way by creating a new matrix, naming the rows and columns by person 1 and 2, then assigning the values to the right spot. We use two R tips that aren't talked about enough to new users.

1) You can extract elements of an object by their name.

2) You can use a two-column matrix for subsetting.

p1 <- sort(unique(df$person1))
p2 <- sort(unique(df$person2))
mat <- matrix(0, length(p1), length(p2))
rownames(mat) <- p1
colnames(mat) <- p2
mat[as.matrix(df[1:2])] <- df$score
mat
#        A     B     C 
# A  0.000 0.568 1.233
# B  0.798 0.000 0.493
# C  1.367 1.276 0.000
Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • That doesn't work for me. we have 20 unique data person1 and this will give ZERO to all the scores in the matrix witch is not true because we are having several scores. There is an example of the data in the table : person1 person2 score 16182470 79389918 3.1 72765516 61389465 3.2 I will have a matrix after the code with 61389465 79389918 16182470 0 0 72765516 0 0 How can I correct that ? – Omar Aug 31 '16 at 11:49
  • Adding code to the comments doesn't help. Please make edits in your question. – Pierre L Aug 31 '16 at 11:54
  • Hi Pierre, to resume, The code is able to extract the unique value of persone1 and person2 but gives 0 for all the score value in the matrix.. Do you have an idea why ? – Omar Aug 31 '16 at 12:05
  • I have shown you that it works for your example. – Pierre L Aug 31 '16 at 12:44
  • Please post data showing the problematic behavior. Post it to your question. – Pierre L Aug 31 '16 at 15:40