Week 2 Challenge - GOT Marriages - Census, Transitivity, etc

challenge_2
Miranda Manka
Describing the Basic Structure of a Network
Author

Miranda Manka

Published

March 22, 2023

Challenge Overview

Code
got_marriages = read_csv("_data/got/got_marriages.csv", show_col_types = FALSE)
got_marriages
# A tibble: 255 × 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   
 7 Martell   Essos     Affair  <NA>   Current   
 8 Martell   Septa     Affair  <NA>   Current   
 9 Martell   Dorne     Affair  <NA>   Current   
10 Martell   Targaryen Married <NA>   Current   
# … with 245 more rows

Describe the Network Data

Code
got_marriages.ig = graph_from_data_frame(got_marriages, directed = FALSE) #directed = false because with relationships, if a is married to b then b is also married to a
got_marriages.ig
IGRAPH a8a852e UN-- 20 255 -- 
+ attr: name (v/c), Type (e/c), Notes (e/c), Generation (e/c)
+ edges from a8a852e (vertex names):
 [1] Targaryen--Stark       Baratheon--Martell     Baratheon--Stark      
 [4] Martell  --Essos       Martell  --Reach       Martell  --Essos      
 [7] Martell  --Essos       Martell  --Septa       Martell  --Dorne      
[10] Targaryen--Martell     Targaryen--Essos       Targaryen--Essos      
[13] Baratheon--Lannister   Baratheon--Vale        Baratheon--Riverlands 
[16] Baratheon--Crownlands  Baratheon--Reach       Baratheon--Westerlands
[19] Baratheon--Crownlands  Lannister--Lannister   Baratheon--Reach      
[22] Baratheon--Tyrell      Tyrell   --Reach       Tyrell   --Reach      
+ ... omitted several edges
Code
vcount(got_marriages.ig)
[1] 20
Code
ecount(got_marriages.ig)
[1] 255
Code
is_bipartite(got_marriages.ig)
[1] FALSE
Code
is_directed(got_marriages.ig)
[1] FALSE
Code
is_weighted(got_marriages.ig)
[1] FALSE
Code
vertex_attr_names(got_marriages.ig)
[1] "name"
Code
edge_attr_names(got_marriages.ig)
[1] "Type"       "Notes"      "Generation"
Code
table(got_marriages$Type)

 Affair Engaged Married 
     36       2     217 
Code
plot(got_marriages.ig)

Dyad and Triad Census

Now try a full dyad census. This gives us the number of dyads where the relationship is:

  • Reciprocal (mutual), or mut
  • Asymmetric (non-mutual), or asym, and
  • Absent, or null

Now use triad.census in order to do a triad census.

Code
dyad_got = dyad.census(got_marriages.ig)
dyad_got
$mut
[1] 60

$asym
[1] 0

$null
[1] 130
Code
triad_got = triad.census(got_marriages.ig)
Warning in triad.census(got_marriages.ig): At core/misc/motifs.c:1165 : Triad
census called on an undirected graph.
Code
triad_got
 [1] 625   0 227   0   0   0   0   0   0   0 228   0   0   0   0  60

Global and Local Transitivity or Clustering

Compute global transitivity using transitivity on igraph or gtrans on statnet and local transitivity of specific nodes of your choice, in addition to the average clustering coefficient. What is the distribution of node degree and how does it compare with the distribution of local transitivity?

Code
global_transitivity = transitivity(got_marriages.ig, type = 'global')
global_transitivity
[1] 0.4411765
Code
V(got_marriages.ig)$name
 [1] "Targaryen"   "Baratheon"   "Martell"     "Lannister"   "Tyrell"     
 [6] "Reach"       "North"       "Riverlands"  "Westerlands" "Stark"      
[11] "Vale"        "Arryn"       "Tully"       "Frey"        "Crownlands" 
[16] "Stormlands"  "Essos"       "Septa"       "Dorne"       "Beyond Wall"
Code
V(got_marriages.ig)[c("Targaryen", "Baratheon", "Martell")]
+ 3/20 vertices, named, from a8a852e:
[1] Targaryen Baratheon Martell  
Code
local_transitivity = transitivity(got_marriages.ig, type="local", vids=V(got_marriages.ig)[c("Targaryen", "Baratheon", "Martell")]) 
local_transitivity
[1] 0.3636364 0.4181818 0.4000000
Code
avg_cluster_coef = transitivity(got_marriages.ig, type = 'average')
avg_cluster_coef
[1] 0.5478074

Path Length and Component Structure

Can you compute the average path length and the diameter of the network? Can you find the component structure of the network and identify the cluster membership of each node?

Code
avg_path_length = average.path.length(got_marriages.ig, directed=F)
avg_path_length
[1] 1.9
Code
names(igraph::components(got_marriages.ig))
[1] "membership" "csize"      "no"        
Code
no_compon = igraph::components(got_marriages.ig)$no
no_compon
[1] 1
Code
size_compon = igraph::components(got_marriages.ig)$csize
size_compon
[1] 20