challenge_4
instructions
Centrality of a Network
Author

Rahul Gundeti

Published

March 15, 2023

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.

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
library(readr)
Warning: package 'readr' was built under R version 4.1.3
Code
got_marriages <- read_csv("_data/got/got_marriages.csv")
Rows: 255 Columns: 5
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (5): From, To, Type, Notes, Generation

i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
head(got_marriages)
# A tibble: 6 x 5
  From      To      Type    Notes  Generation
  <chr>     <chr>   <chr>   <chr>  <chr>     
1 Targaryen Stark   Married R+L=J  Current   
2 Baratheon Martell Engaged died   Current   
3 Baratheon Stark   Engaged broken Current   
4 Martell   Essos   Married <NA>   Current   
5 Martell   Reach   Affair  <NA>   Current   
6 Martell   Essos   Affair  <NA>   Current   
Code
# Load the required package
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
# Create the network from the dataset
netw <- graph.data.frame(got_marriages, directed = FALSE)
# Basic structure of the network
edges <- ecount(netw)
vertices <- vcount(netw)
# Dyad and triad census
dyads <- dyad_census(netw)
triads <- triad_census(netw)
# Create the nodes data frame with local attributes
nodes <- data.frame(
  vertex = V(netw)$name,  # Assuming the vertices have a 'name' attribute
  degree = degree(netw),
  transitivity = transitivity(netw, type = "local")
)
Code
# Print the basic network structure
cat("Number of edges:", edges, "\n")
Number of edges: 255 
Code
cat("Number of vertices:", vertices, "\n")
Number of vertices: 20 
Code
print(dyads)
$mut
[1] 60

$asym
[1] 0

$null
[1] 130
Code
print(triads)
 [1] 625   0 227   0   0   0   0   0   0   0 228   0   0   0   0  60
Code
# Print the nodes data frame
print(nodes)
                 vertex degree transitivity
Targaryen     Targaryen     85    0.3636364
Baratheon     Baratheon     17    0.4181818
Martell         Martell     12    0.4000000
Lannister     Lannister     28    0.7000000
Tyrell           Tyrell     12    1.0000000
Reach             Reach     49    0.3636364
North             North     46    0.1666667
Riverlands   Riverlands     39    0.5714286
Westerlands Westerlands     31    0.6666667
Stark             Stark     34    0.4166667
Vale               Vale     37    0.4666667
Arryn             Arryn     10    0.6666667
Tully             Tully      5    0.5000000
Frey               Frey     51    0.2888889
Crownlands   Crownlands     22    0.5714286
Stormlands   Stormlands     11    0.7000000
Essos             Essos     15    0.6000000
Septa             Septa      1          NaN
Dorne             Dorne      4    1.0000000
Beyond Wall Beyond Wall      1          NaN
Code
in_netw <- centr_degree(netw, loops = FALSE, mode = "in")
in_netw
$res
 [1] 47 17 12 20 12 25 22 33 25 26 17  8  5 47 22 11 15  1  4  1

$centralization
[1] 1.666667

$theoretical_max
[1] 342
Code
out_netw <- centr_degree(netw, loops = FALSE, mode = "out")
out_netw
$res
 [1] 47 17 12 20 12 25 22 33 25 26 17  8  5 47 22 11 15  1  4  1

$centralization
[1] 1.666667

$theoretical_max
[1] 342
Code
netw_total <- centr_degree(netw, loops = FALSE, mode = "total")
netw_total
$res
 [1] 47 17 12 20 12 25 22 33 25 26 17  8  5 47 22 11 15  1  4  1

$centralization
[1] 1.666667

$theoretical_max
[1] 342
Code
eigen <- eigen_centrality(netw)$vector
betweeness <- igraph::betweenness(netw, directed = F)
close <- igraph::closeness(netw, mode = "out")
Code
# Compute the distribution of one centrality measure (e.g., eigencentrality)
eigen_distance <- table(nodes$eigen)
# Print the distribution of eigencentrality
print(eigen_distance)
< table of extent 0 >