R Code for A Justification and Application of Eigenvector Centrality

[This article was first published on Econometric Sense, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Leo Spizzirri  does an excellent job of providing mathematical intuition behind eigenvector centrality. As I was reading through it, I found it easier to just work through the matrix operations he proposes using R.  You can find his paper here: https://www.math.washington.edu/~morrow/336_11/papers/leo.pdf

My R code follows. This is for the example using degree centrality which made the most sense to me. For some reason, in the last section, for very large k there is an issue with computing the vector, I think it is stemming from my definition of B_K. The same issue occurs with large k using the function MM. It could be that the values get too large for R to handle?  Regardless, as pointed out on page 7, even at  k = 10 that ck approaches the eigenvector.

# *------------------------------------------------------------------
# | PROGRAM NAME: R_BASIC_SNA
# | DATE: 4/9/12
# | CREATED BY: MATT BOGARD
# | DATE: 11/5/12
# | PROJECT FILE: P:\R Code References\SNA
# *----------------------------------------------------------------
# | PURPOSE: COMPANION CODE TO Justification and Application of
# | Eigenvector Centrality by Leo Spizzirri
# | https://www.math.washington.edu/~morrow/336_11/papers/leo.pdf
# *------------------------------------------------------------------
 
 
# specify the adjacency matrix
A <- matrix(c(0,1,0,0,0,0,
1,0,1,0,0,0,
0,1,0,1,1,1,
0,0,1,0,1,0,
0,0,1,1,0,1,
0,0,1,0,1,0 ),6,6, byrow= TRUE)
EV <- eigen(A) # compute eigenvalues and eigenvectors
max(EV$values) # find the maximum eigenvalue
 
# get the eigenvector associated with the largest eigenvalue
centrality <- data.frame(EV$vectors[,1])
names(centrality) <- "Centrality"
print(centrality)
 
B <- A + diag(6)
EVB <- eigen(B) # compute eigenvalues and eigenvectors
# they are the same as EV(A)
 
# define matrix M
M <- matrix(c(1,1,0,0,
1,1,1,0,
0,1,1,1,
0,0,1,1),4,4, byrow= TRUE)
 
# define function for B^k for matrix M
MM <- function(k){
n <- (k-1)
B_K <- C
for (i in 1:n){
B_K <- B_K%*%M
}
return(B_K)
}
 
MM(2) # M^2
MM(3) # M^3
 
# define c
 
c <- matrix(c(2,3,5,3,4,3))
 
# define c_k for matrix B
 
ck <- function(k){
n <- (k-2)
B_K <- B
for (i in 1:n){
B_K <- B_K%*%B
}
c_k <- B_K%*%c
return(c_k)
}
 
# derive EV centrality as k -> infinity
library(matrixcalc)
# k = 5
ck(5)/frobenius.norm(ck(5))
# k = 10
ck(10)/frobenius.norm(ck(10))
print(v0)
# k = 100
ck(100)/frobenius.norm(ck(100))

To leave a comment for the author, please follow the link and comment on their blog: Econometric Sense.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)