R Code for A Justification and Application of Eigenvector Centrality

November 8, 2012
By

(This article was first published on Econometric Sense, and kindly contributed to R-bloggers)

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))
Created by Pretty R at inside-R.org

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

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.