Network analysis


In-depth Articles

Data Loading

library(readxl)
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
dati <- read.csv("from_to.csv")[,-1]
info <- read.csv("info.csv")[,-1]

head(dati)
##   From To
## 1    E  A
## 2    D  B
## 3    D  E
## 4    C  B
## 5    C  B
## 6    B  D
info
##   Node Age Department Job
## 1    A  35         d1  w1
## 2    B  24         d1  w2
## 3    C  29         d2  w3
## 4    D  45         d2  w1
## 5    E  40         d3  w2
## 6    F  53         d3  w4
df<-data.frame(table(dati))
d<-which(df$Freq==0)
if(length(d)>0){df<-df[-d,]}

Network Creation

# create data:
links <- data.frame(
  source=df$From,
  target=df$To,
  importance=df$Freq
)
nodes <- data.frame(
  name=info$Node,
  eta=info$Age,
  dep=info$Department,
  title=info$Job
)
# Turn it into igraph object
network <- graph_from_data_frame(d=links, vertices=nodes, directed=F) 
network
## IGRAPH e42a75f UN-- 6 23 -- 
## + attr: name (v/c), eta (v/n), dep (v/c), title (v/c), importance (e/n)
## + edges from e42a75f (vertex names):
##  [1] A--B A--C A--D A--E A--B B--C B--D B--E B--F A--C B--C C--D C--E C--F A--D
## [16] B--D C--D D--E A--E B--E C--E D--E E--F

Degree

The simplest measure of centrality in a social network is the degree. It is the simplest and most common way to find important nodes. The degree of a node is the sum of its edges. If, for example, a node has three lines extending from it to other nodes, its degree is three.

There are two types of degree centrality: indegree and outdegree.

centralization.degree(network, mode = c(“all”, “out”, “in”, “total”))

degree_val<-centralization.degree(network);degree_val
## $res
## [1] 8 9 9 8 9 3
## 
## $centralization
## [1] 0.2666667
## 
## $theoretical_max
## [1] 30

Closeness

The second measure we will discuss is called Closeness. There are also other names for it; sometimes it is called access centrality. In short, Closeness centrality captures the average distance from the focal node to all other nodes in the social network. The mathematical representation of closeness is the following:

\[ l_i = \frac{1}{n-1} \sum_j d_{i,j} \]

We are trying to compute the closeness of the node to all other nodes in the network; hence Closeness. The numerator is the sum of all pairwise distances between node i and all other nodes j (excluding i). The sum of distances is then divided by the total number of nodes in the network \(n\) minus 1 (to adjust the count to exclude node i). Now we have farness, which is the average distance of node i from all other nodes in the network. Taking the reciprocal gives us closeness.

\[ C_i = \frac{1}{l_i} = \frac{n-1}{\sum_j d_{i,j}} \]

closeness_val<-centralization.closeness(network)

Betweenness

It is perhaps one of the most powerful measures of centrality and is closely related to the idea of structural holes. Betweenness can be calculated as:

\[b_i = \sum_{s, t} w_{s,t}^{i} = \sum_{s, t} \frac{n_{s,t}^{i}}{n_{s,t}} \]

Betweenness is the measure by which one understands whether a node acts as a bridge between other nodes in the network. It is calculated by looking at all pairs of nodes in the network and examining the frequency with which i, the focal node, lies on the shortest paths between nodes j and k.

In simpler terms, it is the extent to which a node lies on paths between other nodes. Nodes with a high level of betweenness can have considerable influence within a network by virtue of their control over the information that passes between others. They are also those whose removal from the network will most disrupt communications between other nodes because they lie on the greatest number of paths followed by messages.

betweenness_val<-centralization.betweenness(network)

Eigenvector

A natural extension of degree centrality is eigenvector centrality. Degree centrality gives a centrality point for every link received by a node. But not all vertices are equivalent: some are more relevant than others and, reasonably, endorsements from important nodes count more. The thesis of eigenvector centrality states: A node is important if it is connected to other important nodes. Eigenvector centrality differs from degree centrality: a node that receives many links does not necessarily have high eigenvector centrality (it is possible that all linkers have low or zero eigenvector centrality). Moreover, a node with high eigenvector centrality is not necessarily highly connected (the node may have few but important linkers).

Let \(A = (a_{i, j})\) be the adjacency matrix of a graph. The eigenvector centrality \(x_{i}\) of node \(i\) is given by: \[x_i = \frac {1} {\lambda} \sum_k a_{k, i} \, x_k\] where \(\lambda \neq 0\) is a constant. In matrix form we have: \[\lambda x = x A\]

Thus the centrality vector \(x\) is the left eigenvector of the adjacency matrix \(A\) associated with the eigenvalue \(\lambda\). It is wise to choose \(\lambda\) as the largest eigenvalue in absolute value of the matrix \(A\). By virtue of the Perron-Frobenius theorem, this choice guarantees the following desirable property: if the matrix \(A\) is irreducible, or equivalently if the graph is (strongly) connected, then the eigenvector solution \(x\) is both unique and positive. Let \(m(v)\) denote the signed component of maximum magnitude of vector \(v\). If there is more than one maximum component, let \(m(v)\) be the first one. For example, \(m (-3,3,2) = -3\). Let \(x ^{(0)}\) be an arbitrary vector. For \(k \geq 1\): repeatedly compute \(x ^{(k)} = x ^{(k-1)} A\); normalize \(x ^{(k)} = x ^{(k)} / m (x ^{(k)})\); until reaching the desired precision. It follows that \(x ^{(k)}\) converges to the dominant eigenvector of \(A\) and \(m (x ^{(k)})\) converges to the dominant eigenvalue of \(A\). If the matrix \(A\) is sparse, each vector-matrix product can be performed in linear time in the size of the graph.

The method converges when the dominant eigenvalues (the largest) and the sub-dominant ones (the second largest) of \(A\), respectively denoted by \(\lambda_1\) and \(\lambda_2\), are separated, i.e. differ in absolute value, thus when \(| \lambda_1 | > | \lambda_2 |\). The rate of convergence is the rate at which \((\lambda_2 / \lambda_1) ^k\) goes to \(0\). Therefore, if the sub-dominant secondary eigenvalue is small relative to the dominant one, the method converges rapidly.

evcent_val<-centralization.evcent(network)

Representation of the measures

hist(closeness_val$res)

hist(betweenness_val$res)

hist(degree_val$res)

hist(evcent_val$vector)

pairs(~closeness_val$res+betweenness_val$res+degree_val$res+evcent_val$vector)

Rappresentazione grafica

library(RColorBrewer)
coul  <- brewer.pal(length(levels(as.factor(V(network)$dep))),"Accent") 

# Create a vector of color
my_color <- coul[as.numeric(as.factor(V(network)$dep))]

# Plot
plot(network, vertex.color=my_color, edge.width=E(network)$importance/3 ) # Per rimuovere testo: vertex.label=NA
legend("bottomleft", legend=levels(as.factor(V(network)$dep))  , with the = coul , bty = "n", pch=20 , pt.cex = 3, cex = 1.5, text.col=coul , horiz = FALSE, inset = c(0.1, 0.1))