challenge_2
Mekhala Kumar
got like_dislike
Describing the Basic Structure of a Network
Author

Mekhala Kumar

Published

March 24, 2023

Reading in the data

Code
like_dislike<-read_csv("_data/got/got_like_dislike.csv")
Rows: 46 Columns: 49
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (3): Current house, Former house, Name
dbl (46): Lysa Arryn, Petyr Baelish, Joffrey Baratheon, Margaery Tyrell, Ren...

ℹ 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(like_dislike)
# A tibble: 6 × 49
  Curren…¹ Forme…² Name  Lysa …³ Petyr…⁴ Joffr…⁵ Marga…⁶ Renly…⁷ Rober…⁸ Stann…⁹
  <chr>    <chr>   <chr>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 Arryn    Tully   Lysa…      NA       3       0       0       0       1       0
2 Baelish  <NA>    Pety…       3      NA      -1       1      -2       1      -1
3 Lannist… Barath… Joff…       0      -1      NA       4      -3       2      -5
4 Tyrell   Barath… Marg…       0       1       3      NA       5       0      -5
5 Barathe… <NA>    Renl…       0      -2      -5       5      NA       3      -5
6 Barathe… <NA>    Robe…       2       1       4       0       3      NA       2
# … with 39 more variables: `Brienne of Tarth` <dbl>, Bronn <dbl>,
#   `Gregor Clegane` <dbl>, `Sandor Clegane` <dbl>, `Xaro Xhoan Daxos` <dbl>,
#   Gendry <dbl>, `Balon Greyjoy` <dbl>, `Theon Greyjoy` <dbl>,
#   `Jaqen H'ghar` <dbl>, Hodor <dbl>, `Khal Drogo` <dbl>,
#   `Cersei Lannister` <dbl>, `Jaime Lannister` <dbl>,
#   `Lancel Lannister` <dbl>, `Tyrion Lannister` <dbl>,
#   `Tywin Lannister` <dbl>, `Maester Luwin` <dbl>, Melisandre <dbl>, …

Converting into Network data format

In the adjacency matrix I looked for which values had an edge to compare the number of edges reported once the network is created.

Code
adj<-subset(like_dislike[4:49])
vertex_names<-colnames(adj)
row.names(adj) <- vertex_names
Warning: Setting row names on a tibble is deprecated.
Code
adj_mat <- data.matrix(adj)
#diag(adj_mat) <- 0
table(adj_mat != 0)

FALSE  TRUE 
  927  1143 
Code
#igraph
like_dislike.net <- graph_from_adjacency_matrix(adj_mat, mode = "directed", weighted = TRUE, diag = FALSE)
V(like_dislike.net)$name
 [1] "Lysa Arryn"            "Petyr Baelish"         "Joffrey Baratheon"    
 [4] "Margaery Tyrell"       "Renly Baratheon"       "Robert Baratheon"     
 [7] "Stannis Baratheon"     "Brienne of Tarth"      "Bronn"                
[10] "Gregor Clegane"        "Sandor Clegane"        "Xaro Xhoan Daxos"     
[13] "Gendry"                "Balon Greyjoy"         "Theon Greyjoy"        
[16] "Jaqen H'ghar"          "Hodor"                 "Khal Drogo"           
[19] "Cersei Lannister"      "Jaime Lannister"       "Lancel Lannister"     
[22] "Tyrion Lannister"      "Tywin Lannister"       "Maester Luwin"        
[25] "Melisandre"            "Jeor Mormont"          "Jorah Mormont"        
[28] "Osha"                  "Podrick Payne"         "Pyat Pree"            
[31] "Grand Maester Pycelle" "Ros"                   "Davos Seaworth"       
[34] "Shae"                  "Jon Snow"              "Arya Stark"           
[37] "Bran Stark"            "Catelyn Stark"         "Robb Stark"           
[40] "Sansa Stark"           "Daenerys Targaryen"    "Viserys Targaryen"    
[43] "Samwell Tarly"         "Loras Tyrell"          "Varys"                
[46] "Eddard Stark"         
Code
#statnet
like_dislike_statnet <- network(adj_mat,matrix.type="adjacency", directed=TRUE, loops = FALSE)

Describe the Network Data

The network has 46 vertices and 1143 edges. It is unimodal, directed and weighted. I tried the commands both in igraph and statnet because initially I was getting different number of edges for the two so I wanted to check if there were any inconsistencies remaining.

Code
#igraph
vcount(like_dislike.net)
[1] 46
Code
ecount(like_dislike.net)
[1] 1143
Code
is_bipartite(like_dislike.net)
[1] FALSE
Code
is_directed(like_dislike.net)
[1] TRUE
Code
is_weighted(like_dislike.net)
[1] TRUE
Code
vertex_attr_names(like_dislike.net)
[1] "name"
Code
edge_attr_names(like_dislike.net)
[1] "weight"
Code
V(like_dislike.net)$name
 [1] "Lysa Arryn"            "Petyr Baelish"         "Joffrey Baratheon"    
 [4] "Margaery Tyrell"       "Renly Baratheon"       "Robert Baratheon"     
 [7] "Stannis Baratheon"     "Brienne of Tarth"      "Bronn"                
[10] "Gregor Clegane"        "Sandor Clegane"        "Xaro Xhoan Daxos"     
[13] "Gendry"                "Balon Greyjoy"         "Theon Greyjoy"        
[16] "Jaqen H'ghar"          "Hodor"                 "Khal Drogo"           
[19] "Cersei Lannister"      "Jaime Lannister"       "Lancel Lannister"     
[22] "Tyrion Lannister"      "Tywin Lannister"       "Maester Luwin"        
[25] "Melisandre"            "Jeor Mormont"          "Jorah Mormont"        
[28] "Osha"                  "Podrick Payne"         "Pyat Pree"            
[31] "Grand Maester Pycelle" "Ros"                   "Davos Seaworth"       
[34] "Shae"                  "Jon Snow"              "Arya Stark"           
[37] "Bran Stark"            "Catelyn Stark"         "Robb Stark"           
[40] "Sansa Stark"           "Daenerys Targaryen"    "Viserys Targaryen"    
[43] "Samwell Tarly"         "Loras Tyrell"          "Varys"                
[46] "Eddard Stark"         
Code
#statnet
print(like_dislike_statnet)
 Network attributes:
  vertices = 46 
  directed = TRUE 
  hyper = FALSE 
  loops = FALSE 
  multiple = FALSE 
  bipartite = FALSE 
  total edges= 1143 
    missing edges= 0 
    non-missing edges= 1143 

 Vertex attribute names: 
    vertex.names 

 Edge attribute names not shown 

Dyad and Triad Census

There are 554 mutual connections, 35 non-mutual connections and 446 nodes which are not connected.

Code
igraph::dyad.census(like_dislike.net)
$mut
[1] 554

$asym
[1] 35

$null
[1] 446
Code
sna::dyad.census(like_dislike_statnet)
     Mut Asym Null
[1,] 554   35  446

Triad Census

Code
igraph::triad_census(like_dislike.net)
 [1] 1780  308 4637    5   11   22  222  285    1    1 3849    8   19   26  537
[16] 3469
Code
sna::triad.census(like_dislike_statnet, mode="graph")
        0    1    2    3
[1,] 1975 5109 4405 3691

Global and Local Transitivity or Clustering

The global transitivity is slightly smaller than the average local clustering coefficient. This shows on average, the density globally is comparable to the density for the local ego networks.

Code
#Global
transitivity(like_dislike.net, type="global")
[1] 0.7349339
Code
##Average local clustering coefficient
transitivity(like_dislike.net, type="average")
[1] 0.754549
Code
gtrans(like_dislike_statnet)
[1] 0.7184677

It can be seen that the density for Ros’ network higher in comparison to the density across the network whereas the density for Arya Stark’s and Bronn’s networks are comparable to the global and average clustering coefficients.

Code
#Local
V(like_dislike.net)[c("Arya Stark","Ros", "Bronn")]
+ 3/46 vertices, named, from c2734b5:
[1] Arya Stark Ros        Bronn     
Code
transitivity(like_dislike.net,type="local",vids=V(like_dislike.net)[c("Arya Stark","Ros", "Bronn")])
[1] 0.7045455 0.8888889 0.7632184

Path Length, Geodesic and Component Structure

I looked into the shortest paths and distances between a few of the people.

Code
all_shortest_paths(like_dislike.net,from="Ros",to="Jon Snow", mode="out",weights=NA)$res
[[1]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros              Cersei Lannister Jon Snow        

[[2]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros           Theon Greyjoy Jon Snow     

[[3]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros              Robert Baratheon Jon Snow        

[[4]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros               Joffrey Baratheon Jon Snow         
Code
all_shortest_paths(like_dislike.net,from="Ros",to="Jon Snow", mode="in",weights=NA)$res
[[1]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros              Cersei Lannister Jon Snow        

[[2]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros           Theon Greyjoy Jon Snow     

[[3]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros              Robert Baratheon Jon Snow        

[[4]]
+ 3/46 vertices, named, from c2734b5:
[1] Ros               Joffrey Baratheon Jon Snow         
Code
all_shortest_paths(like_dislike.net,from="Bronn",to="Arya Stark", mode="in",weights=NA)$res
[[1]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn        Eddard Stark Arya Stark  

[[2]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn      Varys      Arya Stark

[[3]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn        Loras Tyrell Arya Stark  

[[4]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn             Viserys Targaryen Arya Stark       

[[5]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn              Daenerys Targaryen Arya Stark        

[[6]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn       Sansa Stark Arya Stark 

[[7]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn      Robb Stark Arya Stark

[[8]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn         Catelyn Stark Arya Stark   

[[9]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn          Davos Seaworth Arya Stark    

[[10]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn         Jorah Mormont Arya Stark   

[[11]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn      Melisandre Arya Stark

[[12]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn           Tywin Lannister Arya Stark     

[[13]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn           Jaime Lannister Arya Stark     

[[14]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn            Cersei Lannister Arya Stark      

[[15]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn         Theon Greyjoy Arya Stark   

[[16]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn      Gendry     Arya Stark

[[17]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn          Gregor Clegane Arya Stark    

[[18]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn            Brienne of Tarth Arya Stark      

[[19]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn             Stannis Baratheon Arya Stark       

[[20]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn             Joffrey Baratheon Arya Stark       

[[21]]
+ 3/46 vertices, named, from c2734b5:
[1] Bronn         Petyr Baelish Arya Stark   
Code
distances(like_dislike.net,v="Bronn",to="Ros",weights=NA,algorithm="johnson")
      Ros
Bronn   2
Code
distances(like_dislike.net,v="Bronn",to="Arya Stark",weights=NA,algorithm="johnson")
      Arya Stark
Bronn          2
Code
distances(like_dislike.net,v="Arya Stark",to="Jon Snow",weights=NA,algorithm="johnson")
           Jon Snow
Arya Stark        1

Average path length

Since this network has negative weights, I was unable to use the commands for average path length and diameter directly. Hence, I converted the values by adding 10 to each observation to make the values positive. This made the original neutral connections equal to 10 which I changed back to 0 in order to avoid confusion regarding the distances calculated.

Code
new_adj<-adj_mat+10
new_adj[new_adj == 10] <- 0
like_dislike2 <- graph_from_adjacency_matrix(new_adj, mode = "directed", weighted = TRUE, diag = FALSE)

average.path.length(like_dislike2,directed=T)
[1] 11.24589
Code
diameter(like_dislike2)
[1] 24

Component Structure

This network consists of a single component with 46 members.

Code
names(igraph::components(like_dislike.net))
[1] "membership" "csize"      "no"        
Code
#igraph::components(like_dislike.net)$membership #very long list
igraph::components(like_dislike.net)$no 
[1] 1
Code
igraph::components(like_dislike.net)$csize
[1] 46

There are no isolates in this network.

Code
isolates(like_dislike_statnet)
integer(0)

Plot

Code
plot(like_dislike.net)
Warning in v(graph): Non-positive edge weight found, ignoring all weights
during graph layout.