Kruzlic Week 2 Challenge

challenge_2
instructions
Basics of a Network: Dyads and Triads
Author

Bryn Kruzlic

Published

March 6, 2023

Describe the Network Data

Code
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.0     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.1     ✔ tibble    3.1.8
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Code
library(readr)
library(igraph)

Attaching package: 'igraph'

The following objects are masked from 'package:lubridate':

    %--%, union

The following objects are masked from 'package:dplyr':

    as_data_frame, groups, union

The following objects are masked from 'package:purrr':

    compose, simplify

The following object is masked from 'package:tidyr':

    crossing

The following object is masked from 'package:tibble':

    as_data_frame

The following objects are masked from 'package:stats':

    decompose, spectrum

The following object is masked from 'package:base':

    union
Code
got_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
View(got_like_dislike)
  1. List and inspect List the objects to make sure the datafiles are working properly.
  2. Network Size What is the size of the network? You may use vcount and ecount.
  3. Network features Are these networks weighted, directed, and bipartite?
  4. Network Attributes Now, using commands from either statnet or igraph, list the vertex and edge attributes.

First, we can list the object by using the command ‘ls’ to make suret the proper data is coming through.

Code
ls(got_like_dislike)
 [1] "Arya Stark"            "Balon Greyjoy"         "Bran Stark"           
 [4] "Brienne of Tarth"      "Bronn"                 "Catelyn Stark"        
 [7] "Cersei Lannister"      "Current house"         "Daenerys Targaryen"   
[10] "Davos Seaworth"        "Eddard Stark"          "Former house"         
[13] "Gendry"                "Grand Maester Pycelle" "Gregor Clegane"       
[16] "Hodor"                 "Jaime Lannister"       "Jaqen H'ghar"         
[19] "Jeor Mormont"          "Joffrey Baratheon"     "Jon Snow"             
[22] "Jorah Mormont"         "Khal Drogo"            "Lancel Lannister"     
[25] "Loras Tyrell"          "Lysa Arryn"            "Maester Luwin"        
[28] "Margaery Tyrell"       "Melisandre"            "Name"                 
[31] "Osha"                  "Petyr Baelish"         "Podrick Payne"        
[34] "Pyat Pree"             "Renly Baratheon"       "Robb Stark"           
[37] "Robert Baratheon"      "Ros"                   "Samwell Tarly"        
[40] "Sandor Clegane"        "Sansa Stark"           "Shae"                 
[43] "Stannis Baratheon"     "Theon Greyjoy"         "Tyrion Lannister"     
[46] "Tywin Lannister"       "Varys"                 "Viserys Targaryen"    
[49] "Xaro Xhoan Daxos"     

Network size can be determined with the following:

Code
got_like_dislike.ig <- graph_from_data_frame(got_like_dislike)
Warning in graph_from_data_frame(got_like_dislike): In `d' `NA' elements were
replaced with string "NA"
Code
vcount(got_like_dislike.ig)
[1] 11
Code
ecount(got_like_dislike.ig)
[1] 46

Currently, we can see there are 11 vertices with 46 edges.

We can determine the network features, discussed in Challenge 1, with the following:

Code
is.bipartite(got_like_dislike.ig)
[1] FALSE
Code
is.directed(got_like_dislike.ig)
[1] TRUE
Code
is.weighted(got_like_dislike.ig)
[1] FALSE

While the network is not bipartite or weighted, it is directed. Since we know that we will be discussing triads later on, it is important to determine and make sure that our network is directed. Undirected graphs will have no asymmetric edges.

The network attributes can be found here:

Code
igraph::vertex_attr_names(got_like_dislike.ig)
[1] "name"
Code
igraph::edge_attr_names(got_like_dislike.ig)
 [1] "Name"                  "Lysa Arryn"            "Petyr Baelish"        
 [4] "Joffrey Baratheon"     "Margaery Tyrell"       "Renly Baratheon"      
 [7] "Robert Baratheon"      "Stannis Baratheon"     "Brienne of Tarth"     
[10] "Bronn"                 "Gregor Clegane"        "Sandor Clegane"       
[13] "Xaro Xhoan Daxos"      "Gendry"                "Balon Greyjoy"        
[16] "Theon Greyjoy"         "Jaqen H'ghar"          "Hodor"                
[19] "Khal Drogo"            "Cersei Lannister"      "Jaime Lannister"      
[22] "Lancel Lannister"      "Tyrion Lannister"      "Tywin Lannister"      
[25] "Maester Luwin"         "Melisandre"            "Jeor Mormont"         
[28] "Jorah Mormont"         "Osha"                  "Podrick Payne"        
[31] "Pyat Pree"             "Grand Maester Pycelle" "Ros"                  
[34] "Davos Seaworth"        "Shae"                  "Jon Snow"             
[37] "Arya Stark"            "Bran Stark"            "Catelyn Stark"        
[40] "Robb Stark"            "Sansa Stark"           "Daenerys Targaryen"   
[43] "Viserys Targaryen"     "Samwell Tarly"         "Loras Tyrell"         
[46] "Varys"                 "Eddard Stark"         

Dyad and Triad Census

Dyad Census:

Three key components of a dyad census involve determining the relationships. In this case, we can observe the relationships by using the functions- reciprocal or ‘mut’, asymmetric or ‘asym’ and finally, absent or ‘null’. We can determine the answers by the following:

Code
igraph::dyad.census(got_like_dislike.ig)
$mut
[1] 1

$asym
[1] 12

$null
[1] 42

Since this is a directed graph, we can see the listings of reciprocal dyads (being 1), asymmetric dyads (being 12) and absent dyads (being 42).

Triad Census:

Code
igraph::triad.census(got_like_dislike.ig)
 [1] 83 32 18  1 22  1  5  0  1  0  0  2  0  0  0  0

Global and Local Transitivity Clustering

Transitivity here refers to the probability that the vertices will be connected in a given network. In this case, we can determine the local transitivity and global transitivity with the following:

Code
transitivity(got_like_dislike.ig)
[1] 0.2368421
Code
transitivity(got_like_dislike.ig, type = 'global')
[1] 0.2368421
Code
transitivity(got_like_dislike.ig, type = 'average') 
[1] 0.5867347

I have realized in my efforts that the standard transitivity() command automatically defaults to global.

We can round this up to 0.24, representing that almost 25% of the global triads are transitive, while 59% of the local triads are transitive. This local transivity can be called the average clustering coefficient, focusing on the low degree nodes of the network.

Path Length and Component Structure

One can determine the path length and diameter by the following

Code
average.path.length(got_like_dislike.ig,directed = T)
[1] 1.3
Code
igraph::components(got_like_dislike.ig)$no
[1] 1
Code
igraph::components(got_like_dislike.ig)$csize
[1] 11

As shown above, the average path length appears to be 1.3 with 1 component at a size of 11.