Cynthia Hester
challenge_2
Describing the Basic Structure of a Network
Author

Cynthia Hester

Published

March 6, 2023

Challenge 2


Load necessary libraries


Import the GOT marriages data

Code
got_marriages <- read_csv("/Users/Bud/Desktop/DACSS_697E_Social Network Analysis_SP2023/Social_Networks_SP_2023/posts/_data/got/got_marriages.csv",show_col_types = FALSE)

View(got_marriages)

Looks at the first five rows and last five rows of data set

Code
#first five rows of marriages data
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
#last five rows of marriages data
tail(got_marriages)
# A tibble: 6 x 5
  From       To         Type    Notes Generation
  <chr>      <chr>      <chr>   <chr> <chr>     
1 Targaryen  Crownlands Married <NA>  Past      
2 Crownlands Stormlands Affair  <NA>  Past      
3 Targaryen  Crownlands Married <NA>  Past      
4 Targaryen  Crownlands Married <NA>  Past      
5 Targaryen  Targaryen  Married <NA>  Past      
6 Targaryen  Targaryen  Married <NA>  Past      

Types of Game of Throne Marriages

Code
table(got_marriages$Type)

 Affair Engaged Married 
     36       2     217 

Create a network object

Code
# new network object
marriages.ig <- graph_from_data_frame(got_marriages, directed = FALSE)

# Dataframe summary
print(marriages.ig)
IGRAPH 9363713 UN-- 20 255 -- 
+ attr: name (v/c), Type (e/c), Notes (e/c), Generation (e/c)
+ edges from 9363713 (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

Network Attributes

Code
#network attributes
got_marriages.stat <- as.network(got_marriages, loops = TRUE, multiple = TRUE)

print(got_marriages.stat)
 Network attributes:
  vertices = 20 
  directed = TRUE 
  hyper = FALSE 
  loops = TRUE 
  multiple = TRUE 
  bipartite = FALSE 
  total edges= 255 
    missing edges= 0 
    non-missing edges= 255 

 Vertex attribute names: 
    vertex.names 

 Edge attribute names: 
    Generation Notes Type 

We have 255 vertices/nodes and 20 edges/ties

Name of the vertex or node attribute

Code
# node attribute name
vertex_attr_names(marriages.ig)
[1] "name"

Names

Code
#names extracted from the names column
V(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"

Name(s) of the edge attributes

Code
# edge attribute name(s)
edge_attr_names(marriages.ig)
[1] "Type"       "Notes"      "Generation"

Is the network directed or undirected?

Code
is_directed<-is_directed(marriages.ig)

if (is_directed) {
  print("The network is directed.")
} else {
  print("The network is undirected.")
}
[1] "The network is undirected."

Is the network weighted or unweighted?

Code
is_weighted<-is_weighted(marriages.ig)

if (is_weighted) {
  print("The network is weighted.")
} else {
  print("The network is unweighted.")
}
[1] "The network is unweighted."

Is the network unimodal or bipartite?

Code
is_bipartite <- is_bipartite(marriages.ig)

if (is_bipartite) {
  print("The network is bipartite.")
} else {
  print("The network is unimodal.")
}
[1] "The network is unimodal."

Let’s visualise GOT Marriages network

Code
# plot  marriage network
plot(marriages.ig,
     vertex.size = 7,
     vertex.color = "blue",
     vertex.label.cex = 0.8,
     main = "Game of Thrones Marriages")


Dyad and Triad Census

Dyad census

A dyad census is useful for studying the marriages within the context of Game of Thrones (GOT), specifically in the context of the in_relation to various noble houses and alliances. This provides insight into the power dynamics and political strategies within GOT. For instance, they could examine which Houses formed the most alliances via marriage and how these alliances changed, evolved,grew or disintegrated over time.

Code
#Dyad Census
dyad.census(got_marriages.stat)
     Mut Asym Null
[1,] 125  -65  130

Mut - This shows we have 125 mutual relationships, i.e. there are 125 pairs of individuals in the network that have a mutual connection. This makes sense since these are marriages.

Asym - indicates a negative indegree-outdegree difference shows that there is a relatively equalvalent number of incoming and outgoing ties.

Null - indicates that the 130 possible dyads may have no observed relationship.


Triad census

A triad is a group of three nodes that are connected by either two (a “dyad”) or three edges (a “triad”). A triad census involves counting the number of each type of triad that occurs in a network.

Code
##triad census
triad.census(got_marriages.stat)
     003 012 102 021D 021U 021C 111D 111U 030T 030C 201 120D 120U 120C 210 300
[1,] 408 426  18  110   44   47    9   18   50    1   0    3    5    1   0   0

Its observed that the high frequency of type 012 triads, which represent marriages where one partner is married to two others who are not married to each other, is consistent with the complexities of marriages and alliances in Game of Thrones. The significant number of type 120U and 120D triads, which represent situations where two people are married to the same person, is also consistent with the practice of polygamy and multiple marriages.


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?

Transitivity

Transitivity refers to the extent that nodes or vertices in a network have a tendency to be connected to other nodes that are also connected to each other.

Global Transitivity

Code
#global transitivity

got_global_transitivity = transitivity(marriages.ig, type = 'global')
got_global_transitivity
[1] 0.4411765

The Game of Thrones(GOT) Marriages Global transitivity captures the extent to which nodes or vertices in the network form tightly interconnected clusters. It yields a single value that represents the overall level of clustering in the network. In the context of GOT marriages we get a global transitivity of 0.4411765. This suggests that 44.1 percent of all possible triangles are closed, which indicates a moderately high level of clustering in the network. This makes sense in the context of GOT marriages.

Local Transitivity

Code
#local_transitivity

got_local_transitivity = transitivity(marriages.ig, type = 'local')
print(got_local_transitivity)
 [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

Local transitivity measures the proportion of triangles centered at a particular node that are closed.For this GOT marriages output it suggests that most of the nodes have moderate to high local transitivity with values ranging from 0.36 to 1.0. This indicates that the many of the characters in the Game of Thrones are highly connected to each other through marriages and alliances.

Of note,there are also a few nodes with low local transitivity, which may indicate these characters might be relatively isolated within the network,and have fewer connections to other characters. Furthermore, it is observed that there are a couple of nodes with missing values (NaN), which means they may have no connections to other nodes in the network.


Path Length and Component Structure

Shortest path between all pairs of nodes

Code
#shortest path between all pairs of nodes
marriages_sp<-shortest.paths(marriages.ig)
table(marriages_sp)
marriages_sp
  0   1   2   3   4 
 20 120 186  66   8 

In order to get the average path length, I first calculate the shortest path between all pairs of nodes in the object marriages_sp. The numbers in the matrix for the shortest path represent the shortest path distances between different pairs of marriages, where each marriage is represented by a node in the network.

Average path length

Code
# average path length

marriages_avg_path_length<-mean(marriages_sp[is.finite(marriages_sp)])
marriages_avg_path_length
[1] 1.805

Calculates the average path of the GOT marriages network by extracting all the finite elements from the matrix of the previously configured lengths of all pairs of marriage nodes, and then taking the mean of those finite elements. Thus, the average path length appears to be approximately 1.81.

Diameter of GOT Marriage network

Code
# Compute the diameter of network
marriage_diameter <- diameter(marriages.ig)

# diameter of marriage network
print(marriage_diameter)
[1] 4

With a diameter of 4, this suggests that the longest shortest path between any pair of nodes in the GOT marriage network is 4 edges.


Component Structure of network

Code
# component names extracted from got_marriages data

names(igraph::components(marriages.ig))
[1] "membership" "csize"      "no"        

Calculates connected components of the GOT Marriage network

Code
#calculates connected components using the components function.

igraph::components(marriages.ig)
$membership
  Targaryen   Baratheon     Martell   Lannister      Tyrell       Reach 
          1           1           1           1           1           1 
      North  Riverlands Westerlands       Stark        Vale       Arryn 
          1           1           1           1           1           1 
      Tully        Frey  Crownlands  Stormlands       Essos       Septa 
          1           1           1           1           1           1 
      Dorne Beyond Wall 
          1           1 

$csize
[1] 20

$no
[1] 1

In the context of the igraph::components() output a component refers to a subset of nodes in the GOT Marriages network that are connected to each other by edges/ties.

membership - indicates which component each node belongs to.

csize - provides the size of each component,which is a size of 20.

no - indicates the number of components.