challenge_4
instructions
Centrality of a Network
Author

Akhilesh Kumar

Published

April 20, 2023

Code
library(igraph)

Attaching package: 'igraph'
The following objects are masked from 'package:stats':

    decompose, spectrum
The following object is masked from 'package:base':

    union
Code
library(network)
Warning: package 'network' was built under R version 4.2.3

'network' 1.18.1 (2023-01-24), part of the Statnet Project
* 'news(package="network")' for changes since last version
* 'citation("network")' for citation information
* 'https://statnet.org' for help, support, and other information

Attaching package: 'network'
The following objects are masked from 'package:igraph':

    %c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
    get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
    is.directed, list.edge.attributes, list.vertex.attributes,
    set.edge.attribute, set.vertex.attribute

Reading Data

Code
got_marriages <- read.csv("_data/got/got_marriages.csv")
head(got_marriages)
       From      To    Type  Notes Generation
1 Targaryen   Stark Married  R+L=J    Current
2 Baratheon Martell Engaged   died    Current
3 Baratheon   Stark Engaged broken    Current
4   Martell   Essos Married           Current
5   Martell   Reach  Affair           Current
6   Martell   Essos  Affair           Current
Code
got_marriages.ig <-graph_from_data_frame(got_marriages, directed = TRUE)

Challenge Overview

Describe the many measures of centrality of at least one network of your choice.

Describe the Network Data

If you have not done it before, always start by evaluating the basic structure of the network (number of edges and vertices, dyad and triad census, etc.). Create a data frame nodes with the corresponding local attributes such as degree or local transitivity.

Code
# Number of vertices in got_marriages network:

vcount(got_marriages.ig)
[1] 20
Code
# Number of edges in got_marriages network:

ecount(got_marriages.ig)
[1] 255
Code
# Bipartite or single mode network verification in got_marriages network:

is_bipartite(got_marriages.ig)
[1] FALSE
Code
# Directed or undirected edges verification in got_marriages network

is_directed(got_marriages.ig)
[1] TRUE
Code
# Weighted or unweighted edges verification in got_marriages network

is_weighted(got_marriages.ig)
[1] FALSE
Code
# Is Connected verification in got_marriages network

is_connected(got_marriages.ig)
[1] TRUE
Code
# Degree calculation in got_marriages network

degree = degree(got_marriages.ig)

# Indegree

degree(got_marriages.ig, mode = 'out')
  Targaryen   Baratheon     Martell   Lannister      Tyrell       Reach 
         65          15           9          22           8          16 
      North  Riverlands Westerlands       Stark        Vale       Arryn 
         13           5           3          27          11           6 
      Tully        Frey  Crownlands  Stormlands       Essos       Septa 
          2          47           5           1           0           0 
      Dorne Beyond Wall 
          0           0 
Code
# Local Transitivity calculation in got_marriages network

transitivity(got_marriages.ig, type = "local")
 [1] 0.3636364 0.4181818 0.4000000 0.7000000 1.0000000 0.3636364 0.1666667
 [8] 0.5714286 0.6666667 0.4166667 0.4666667 0.6666667 0.5000000 0.2888889
[15] 0.5714286 0.7000000 0.6000000       NaN 1.0000000       NaN
Code
# Global Transitivity calculation in got_marriages network

transitivity(got_marriages.ig, type = "global")
[1] 0.4411765
Code
# Local Average Transitivity calculation in got_marriages network

transitivity(got_marriages.ig, type = "localaverage")
[1] 0.5478074

Centrality

Compute at least one measure of centrality for each node, adding them as attributes to nodes in the form of extra columns: eigencentrality, betweenness, closeness, etc. Compute the distribution of at least one of those measures. What do we learn from comparing these measures of centrality with an equivalent random network (i.e. with the same number of edges and vertices)?

Code
# Betweenness

V(got_marriages.ig)$betweenness <-igraph::betweenness(got_marriages.ig)

# closeness

V(got_marriages.ig)$closeness <-igraph::closeness(got_marriages.ig)

# eigen_centrality

V(got_marriages.ig)$eigen_centrality <-igraph::eigen_centrality(got_marriages.ig)$vector


# Compute the Distribution eigencentrality

eigen_table <- table(V(got_marriages.ig)$eigen_centrality)
print(eigen_table)

0.00188957823728034 0.00267282274631418  0.0181467469620076  0.0460144124992236 
                  1                   1                   1                   1 
 0.0553241324308682  0.0572737777541879  0.0799514319349534  0.0892625167697576 
                  1                   1                   1                   1 
 0.0893830361240075  0.0894787158944623  0.0996550027383894   0.113194498217204 
                  1                   1                   1                   1 
  0.126568322197616   0.151988689747416   0.192767892034525   0.214820599128922 
                  1                   1                   1                   1 
  0.272772859787063   0.282784491545717    0.29936226451705                   1 
                  1                   1                   1                   1 
Code
# Compute the Distribution closeness

close_table <- table(V(got_marriages.ig)$closeness)
print(close_table)

0.0256410256410256 0.0285714285714286 0.0294117647058824 0.0333333333333333 
                 1                  1                  1                  1 
0.0357142857142857 0.0416666666666667 0.0476190476190476 0.0526315789473684 
                 1                  1                  1                  1 
0.0666666666666667 0.0769230769230769 0.0833333333333333                0.1 
                 1                  1                  2                  1 
 0.166666666666667                  1 
                 1                  1 
Code
# Compute the Distribution of betweenness

between_table <- table(V(got_marriages.ig)$betweenness)
print(between_table)

               0              0.6             1.75             2.25 
               6                1                1                1 
2.66666666666667              4.5              4.9              5.5 
               1                1                1                2 
7.55714285714286           12.375               14 20.1424603174603 
               1                1                1                1 
            25.5 31.7587301587302 
               1                1