challenge_4
instructions
Centrality of a Network
Author

Niharika Pola

Published

March 10, 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.2.3
Code
got_distances <- read_csv("_data/got/got_distances.csv")
Rows: 200 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): Region From, From, To, Mode, Notes
dbl (1): Miles

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
head(got_distances)
# A tibble: 6 × 6
  `Region From` From          To               Miles Mode  Notes   
  <chr>         <chr>         <chr>            <dbl> <chr> <chr>   
1 Westerlands   Casterly Rock the Golden Tooth   240 land  <NA>    
2 Westerlands   Casterly Rock Lannisport          40 land  <NA>    
3 Westerlands   Casterly Rock Kayce              100 land  <NA>    
4 Westerlands   Casterly Rock Kayce               12 water <NA>    
5 Westerlands   Casterly Rock Deep Den           240 land  Goldroad
6 Westerlands   Deep Den      King’s Landing     590 land  Goldroad
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
net <- graph.data.frame(got_distances, directed = FALSE)
# Basic structure of the network
edges <- ecount(net)
vertices <- vcount(net)
# Dyad and triad census
dyads <- dyad_census(net)
triads <- triad_census(net)
# Create the nodes data frame with local attributes
nodes <- data.frame(
  vertex = V(net)$name,  # Assuming the vertices have a 'name' attribute
  degree = degree(net),
  transitivity = transitivity(net, type = "local")
)
Code
# Print the basic network structure
cat("Number of edges:", edges, "\n")
Number of edges: 200 
Code
cat("Number of vertices:", vertices, "\n")
Number of vertices: 103 
Code
print(dyads)
$mut
[1] 93

$asym
[1] 0

$null
[1] 5160
Code
print(triads)
 [1] 172432      0   3917      0      0      0      0      0      0      0
[11]    502      0      0      0      0      0
Code
# Print the nodes data frame
print(nodes)
                                                       vertex degree
Westerlands                                       Westerlands     14
Reach                                                   Reach     19
Riverlands                                         Riverlands     18
Vale                                                     Vale     19
Crownlands                                         Crownlands     13
Stormlands                                         Stormlands     13
Dorne                                                   Dorne     16
North                                                   North     26
Iron Islands                                     Iron Islands     10
Essos                                                   Essos     57
Casterly Rock                                   Casterly Rock      5
Deep Den                                             Deep Den      1
Kayce                                                   Kayce      1
Faircastle                                         Faircastle      1
the Crag                                             the Crag      1
Ashemark                                             Ashemark      2
Lannisport                                         Lannisport      2
Crakehall                                           Crakehall      1
Old Oak                                               Old Oak      1
Highgarden                                         Highgarden      5
Oldtown                                               Oldtown      2
Blackcrown                                         Blackcrown      2
Three Towers                                     Three Towers      1
Mouth of the Mander River           Mouth of the Mander River      1
Golden Grove                                     Golden Grove      1
Cider Hall                                         Cider Hall      2
Longtable                                           Longtable      2
Harroway                                             Harroway      7
Seagard                                               Seagard      3
Riverrun                                             Riverrun      3
Harrenhal                                           Harrenhal      2
Saltpans                                             Saltpans      1
Maidenpool                                         Maidenpool      2
Border                                                 Border      1
the Bloody Gate                               the Bloody Gate      1
Wickenden                                           Wickenden      2
Gulltown                                             Gulltown      1
Runestone                                           Runestone      2
Old Anchor                                         Old Anchor      2
Longbow Hall                                     Longbow Hall      2
Snakewoods                                         Snakewoods      3
Heart’s Home                                     Heart’s Home      1
the Sisters                                       the Sisters      2
Pebble Island                                   Pebble Island      1
the Paps                                             the Paps      1
King’s Landing                                 King’s Landing      7
Duskendale                                         Duskendale      2
Rook’s Rest                                       Rook’s Rest      1
Sharp Point                                       Sharp Point      1
Dragonstone                                       Dragonstone      2
Wendwater                                           Wendwater      1
Bronze Gate                                       Bronze Gate      1
Storm’s End                                       Storm’s End      4
Summerhall                                         Summerhall      1
Blackhaven                                         Blackhaven      1
Rain House                                         Rain House      1
Mistwood                                             Mistwood      2
Weeping Tower                                   Weeping Tower      1
Stonehelm                                           Stonehelm      1
Wyl                                                       Wyl      1
Yronwood                                             Yronwood      1
the Tor                                               the Tor      1
Godsgrace                                           Godsgrace      3
Saltshore                                           Saltshore      1
Vaith                                                   Vaith      1
Hellholt                                             Hellholt      3
Skyreach                                             Skyreach      1
Moat Cailin                                       Moat Cailin      2
Cerwyn                                                 Cerwyn      1
Greywater Watch                               Greywater Watch      1
Flint’s Finger                                 Flint’s Finger      1
White Harbor                                     White Harbor      2
Ramsgate                                             Ramsgate      2
Widow’s Watch                                   Widow’s Watch      1
Karhold                                               Karhold      2
Dreadfort                                           Dreadfort      2
Winterfell                                         Winterfell      5
Deepwood Motte                                 Deepwood Motte      2
Barrowton                                           Barrowton      5
the Shield Islands                         the Shield Islands      1
the Arbor                                           the Arbor      1
the Southern Dornish Coastline the Southern Dornish Coastline      1
Lemonwood                                           Lemonwood      2
Sunspear                                             Sunspear      1
Tyrosh                                                 Tyrosh      7
Pentos                                                 Pentos      2
Braavos                                               Braavos      6
Lorath                                                 Lorath      1
Lys                                                       Lys      2
Volantis                                             Volantis      5
New Ghis                                             New Ghis      9
Astapor                                               Astapor      2
Yunkai                                                 Yunkai      2
Dagger Lake                                       Dagger Lake      2
the Stepstones                                 the Stepstones      1
Myr                                                       Myr      3
Selhorys                                             Selhorys      2
Qohor                                                   Qohor      1
Norvos                                                 Norvos      2
Vaes Dothrak                                     Vaes Dothrak      4
Mantarys                                             Mantarys      2
Bhorash                                               Bhorash      1
Meereen                                               Meereen      3
                               transitivity
Westerlands                               0
Reach                                     0
Riverlands                                0
Vale                                      0
Crownlands                                0
Stormlands                                0
Dorne                                     0
North                                     0
Iron Islands                            NaN
Essos                                     0
Casterly Rock                           NaN
Deep Den                                NaN
Kayce                                   NaN
Faircastle                              NaN
the Crag                                NaN
Ashemark                                NaN
Lannisport                              NaN
Crakehall                               NaN
Old Oak                                 NaN
Highgarden                              NaN
Oldtown                                 NaN
Blackcrown                              NaN
Three Towers                            NaN
Mouth of the Mander River               NaN
Golden Grove                            NaN
Cider Hall                              NaN
Longtable                               NaN
Harroway                                NaN
Seagard                                 NaN
Riverrun                                NaN
Harrenhal                               NaN
Saltpans                                NaN
Maidenpool                              NaN
Border                                  NaN
the Bloody Gate                         NaN
Wickenden                               NaN
Gulltown                                NaN
Runestone                               NaN
Old Anchor                              NaN
Longbow Hall                            NaN
Snakewoods                              NaN
Heart’s Home                            NaN
the Sisters                             NaN
Pebble Island                           NaN
the Paps                                NaN
King’s Landing                          NaN
Duskendale                              NaN
Rook’s Rest                             NaN
Sharp Point                             NaN
Dragonstone                             NaN
Wendwater                               NaN
Bronze Gate                             NaN
Storm’s End                             NaN
Summerhall                              NaN
Blackhaven                              NaN
Rain House                              NaN
Mistwood                                NaN
Weeping Tower                           NaN
Stonehelm                               NaN
Wyl                                     NaN
Yronwood                                NaN
the Tor                                 NaN
Godsgrace                               NaN
Saltshore                               NaN
Vaith                                   NaN
Hellholt                                NaN
Skyreach                                NaN
Moat Cailin                             NaN
Cerwyn                                  NaN
Greywater Watch                         NaN
Flint’s Finger                          NaN
White Harbor                            NaN
Ramsgate                                NaN
Widow’s Watch                           NaN
Karhold                                 NaN
Dreadfort                               NaN
Winterfell                              NaN
Deepwood Motte                          NaN
Barrowton                               NaN
the Shield Islands                      NaN
the Arbor                               NaN
the Southern Dornish Coastline          NaN
Lemonwood                               NaN
Sunspear                                NaN
Tyrosh                                  NaN
Pentos                                  NaN
Braavos                                 NaN
Lorath                                  NaN
Lys                                     NaN
Volantis                                NaN
New Ghis                                NaN
Astapor                                 NaN
Yunkai                                  NaN
Dagger Lake                             NaN
the Stepstones                          NaN
Myr                                     NaN
Selhorys                                NaN
Qohor                                   NaN
Norvos                                  NaN
Vaes Dothrak                            NaN
Mantarys                                NaN
Bhorash                                 NaN
Meereen                                 NaN
Code
in_net <- centr_degree(net, loops = FALSE, mode = "in")
in_net
$res
  [1] 14 19 18 19 13 13 16 26  0 57  5  1  1  1  1  2  2  1  1  5  2  2  1  1  1
 [26]  2  2  7  3  3  2  1  2  1  1  2  1  2  2  2  3  1  2  1  1  7  2  1  1  2
 [51]  1  1  4  1  1  1  2  1  1  1  1  1  3  1  1  3  1  2  1  1  1  2  2  1  2
 [76]  2  5  2  5  1  1  1  2  1  7  2  6  1  2  5  9  2  2  2  1  3  2  1  2  4
[101]  2  1  3

$centralization
[1] 0.5320326

$theoretical_max
[1] 10302
Code
out_net <- centr_degree(net, loops = FALSE, mode = "out")
out_net
$res
  [1] 14 19 18 19 13 13 16 26  0 57  5  1  1  1  1  2  2  1  1  5  2  2  1  1  1
 [26]  2  2  7  3  3  2  1  2  1  1  2  1  2  2  2  3  1  2  1  1  7  2  1  1  2
 [51]  1  1  4  1  1  1  2  1  1  1  1  1  3  1  1  3  1  2  1  1  1  2  2  1  2
 [76]  2  5  2  5  1  1  1  2  1  7  2  6  1  2  5  9  2  2  2  1  3  2  1  2  4
[101]  2  1  3

$centralization
[1] 0.5320326

$theoretical_max
[1] 10302
Code
net_total <- centr_degree(net, loops = FALSE, mode = "total")
net_total
$res
  [1] 14 19 18 19 13 13 16 26  0 57  5  1  1  1  1  2  2  1  1  5  2  2  1  1  1
 [26]  2  2  7  3  3  2  1  2  1  1  2  1  2  2  2  3  1  2  1  1  7  2  1  1  2
 [51]  1  1  4  1  1  1  2  1  1  1  1  1  3  1  1  3  1  2  1  1  1  2  2  1  2
 [76]  2  5  2  5  1  1  1  2  1  7  2  6  1  2  5  9  2  2  2  1  3  2  1  2  4
[101]  2  1  3

$centralization
[1] 0.5320326

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