Centrality Measures

WEN, Tzai-Hung (NTU Geography)

library(sna)
library(igraph)

1. Degree Centrality Measures

setwd("D:/R_Labs")
data<- "sample_adjmatrix.csv"
el<-read.table(data, header=T, row.names=1, sep=",")
m=as.matrix(el)
gplot(m, displaylabels=TRUE)

sna::degree(m)
## [1]  6  8  2  8  4 10  2
sna::degree(m, cmode = "indegree")
## [1] 3 4 1 4 2 5 1
sna::degree(m,cmode="outdegree")
## [1] 3 4 1 4 2 5 1
# Calculate the degree of m using gapply
gapply(m,1,rep(1,7),sum)
## 23732 23778 23824 23871 58009 58098 58256 
##     3     4     1     4     2     5     1

2. Basic Centrality Measures

Degree, Clonseness, Betweenness
Graph Centrality, Stress Centrality, Eigenvector Centrality and Information Centrality
# Basic Measures
sna::degree(m)
## [1]  6  8  2  8  4 10  2
sna::closeness(m)
## [1] 0.6666667 0.7500000 0.4615385 0.7500000 0.5454545 0.8571429 0.5000000
sna::betweenness(m)
## [1]  0 10  0  3  0 13  0
sna::graphcent(m) # Graph Centrality Scores
## [1] 0.5000000 0.5000000 0.3333333 0.5000000 0.3333333 0.5000000 0.3333333
sna::stresscent(m) #  Stress Centrality Scores 
## [1]  0 12  0  6  0 16  0
sna::evcent(m) # Eigenvector Centrality Scores 
## [1] 0.4187492 0.4477773 0.1294436 0.4836268 0.2893056 0.5171524 0.1494986
sna::infocent(m) # Information Centrality Scores
## [1] 1.3207547 1.4583333 0.7142857 1.4507772 1.0894942 1.5819209 0.7427056
g1=graph.adjacency(m,weighted=NULL) # PageRank Scores
(pr<-page_rank(g1)$vector)
##     X23732     X23778     X23824     X23871     X58009     X58098 
## 0.14446947 0.19732697 0.06336055 0.18885024 0.10253749 0.24104847 
##     X58256 
## 0.06240681
gplot(m, vertex.cex=pr*10 , displaylabels=TRUE)

Comparisons between Degree and Betweenness
dg<- sna::degree(m)
bt<- sna::betweenness(m)
plot(bt,dg, xlab="Betweenness Centrality", ylab="Degree Centrality", main = "Degree vs. Betweenness", col="red", pch=5)

gplot(m, vertex.cex=sqrt(dg) , displaylabels=TRUE)   

gplot(m, vertex.cex=sqrt(bt)+1 , displaylabels=TRUE)

3. Create geodesic path count and geodesic distance matrix

geodist(m)
## $counts
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    1    1    1    1    2    1    1
## [2,]    1    1    1    1    2    1    1
## [3,]    1    1    1    1    2    1    1
## [4,]    1    1    1    1    1    1    1
## [5,]    2    2    2    1    1    1    1
## [6,]    1    1    1    1    1    1    1
## [7,]    1    1    1    1    1    1    1
## 
## $gdist
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    0    1    2    1    2    1    2
## [2,]    1    0    1    1    2    1    2
## [3,]    2    1    0    2    3    2    3
## [4,]    1    1    2    0    1    1    2
## [5,]    2    2    3    1    0    1    2
## [6,]    1    1    2    1    1    0    1
## [7,]    2    2    3    2    2    1    0
$counts: a matrix containing the number of geodesics between each pair of vertices
$gdist: a matrix containing the geodesic distances between each pair of vertices