Week3_Challenge_Rahul Gundeti

challenge_3
instructions
Degree and Density of a Network
Author

Rahul Gundeti

Published

March 10, 2023

Code
#loading libraries
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
library(network)

'network' 1.18.1 (2023-01-24), part of the Statnet Project
* 'news(package="network")' for changes since last version
* 'citation("network")' for citation information
* 'https://statnet.org' for help, support, and other information

Attaching package: 'network'
The following objects are masked from 'package:igraph':

    %c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
    get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
    is.directed, list.edge.attributes, list.vertex.attributes,
    set.edge.attribute, set.vertex.attribute
Code
library(tidyverse)
-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
v dplyr     1.1.2     v readr     2.1.4
v forcats   1.0.0     v stringr   1.5.0
v ggplot2   3.4.2     v tibble    3.2.1
v lubridate 1.9.2     v tidyr     1.3.0
v purrr     1.0.1     
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x lubridate::%--%()      masks igraph::%--%()
x dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
x purrr::compose()       masks igraph::compose()
x tidyr::crossing()      masks igraph::crossing()
x dplyr::filter()        masks stats::filter()
x dplyr::lag()           masks stats::lag()
x purrr::simplify()      masks igraph::simplify()
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Code
library(readr)
Code
#Loading dataset
got_teams <- read_csv("_data/got/got_teams.csv")
Rows: 33 Columns: 5
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (3): team, alliance, joins
dbl (2): id, gh

i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
head(got_teams)
# A tibble: 6 x 5
  team            id alliance     joins                    gh
  <chr>        <dbl> <chr>        <chr>                 <dbl>
1 Drogo            0 Opportunists follows Drogo             0
2 Littlefinger     1 Opportunists follows Littlefinger      0
3 Martell          2 Opportunists joins the Martells        0
4 Red God          3 Opportunists worships the Red God      0
5 Renly            4 Opportunists follows Renly             0
6 Second Sons      5 Opportunists joins the Second Sons     0
Code
got_teams.igraph <-graph_from_data_frame(got_teams, directed = TRUE)
Code
# number of edges
ecount(got_teams.igraph)
[1] 33
Code
# number of vertices
vcount(got_teams.igraph)
[1] 66
Code
# vertex and edge attributes
vertex_attr_names(got_teams.igraph)
[1] "name"
Code
edge_attr_names(got_teams.igraph)
[1] "alliance" "joins"    "gh"      
Code
# network features
is_directed(got_teams.igraph)
[1] TRUE
Code
is_bipartite(got_teams.igraph)
[1] FALSE
Code
is_weighted(got_teams.igraph)
[1] FALSE
Code
# dyad census
igraph::dyad.census(got_teams.igraph)
$mut
[1] 0

$asym
[1] 33

$null
[1] 2112
Code
# triad census
triad_census(got_teams.igraph)
 [1] 43648  2112     0     0     0     0     0     0     0     0     0     0
[13]     0     0     0     0

The vertex attribute is ‘name’ and edge attributes are “alliance”, “joins”, “gh” and this network has 255 edges and 20 vertices.

Code
# number of components
igraph::components(got_teams.igraph)$no
[1] 33
Code
# size of components
igraph::components(got_teams.igraph)$csize 
 [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

Compute the density of the network. Is this a global or local measure? Does it have a relationship with average degree?

Code
# network density
graph.density(got_teams.igraph)
[1] 0.007692308
Code
# density without loops
graph.density(got_teams.igraph, loops=TRUE)
[1] 0.007575758
Code
# average network degree
igraph::degree(got_teams.igraph)
                      Drogo                Littlefinger 
                          1                           1 
                    Martell                     Red God 
                          1                           1 
                      Renly                 Second Sons 
                          1                           1 
                    Stannis                  Stormcrows 
                          1                           1 
                  Targaryen                      Tyrell 
                          1                           1 
                  Windblown          Bolton (Lannister) 
                          1                           1 
           Frey (Lannister)                   Lannister 
                          1                           1 
           Brave Companions                       Essos 
                          1                           1 
                    Greyjoy              Mountain clans 
                          1                           1 
                 the Others   Wildlings (north of wall) 
                          1                           1 
             Bolton (North)                     Brienne 
                          1                           1 
Brotherhood without Banners                Frey (North) 
                          1                           1 
                      Stark                 Night Watch 
                          1                           1 
                       none                      Robert 
                          1                           1 
                     Tyrion           Wildling refugees 
                          1                           1 
                    Citadel                Faceless Men 
                          1                           1 
                      Faith                           0 
                          1                           1 
                          1                           2 
                          1                           1 
                          3                           4 
                          1                           1 
                          5                           6 
                          1                           1 
                          7                           8 
                          1                           1 
                          9                          10 
                          1                           1 
                         11                          12 
                          1                           1 
                         13                          14 
                          1                           1 
                         15                          16 
                          1                           1 
                         17                          18 
                          1                           1 
                         19                          20 
                          1                           1 
                         21                          22 
                          1                           1 
                         23                          24 
                          1                           1 
                         25                          26 
                          1                           1 
                         27                          28 
                          1                           1 
                         29                          30 
                          1                           1 
                         31                          32 
                          1                           1 
Code
nodes_igraph<-data.frame(name=V(got_teams.igraph)$name, degree=igraph::degree(got_teams.igraph,loops=FALSE))
nodes_igraph<-nodes_igraph %>%
    mutate(indegree=igraph::degree(got_teams.igraph, mode="in", loops=FALSE),
           outdegree=igraph::degree(got_teams.igraph, mode="out", loops=FALSE))
head(nodes_igraph)
                     name degree indegree outdegree
Drogo               Drogo      1        0         1
Littlefinger Littlefinger      1        0         1
Martell           Martell      1        0         1
Red God           Red God      1        0         1
Renly               Renly      1        0         1
Second Sons   Second Sons      1        0         1
Code
erdos_renyi.igraph <- sample_gnm(103, 200, directed = TRUE, loops = FALSE)
# density of random network
graph.density(erdos_renyi.igraph)
[1] 0.01903674
Code
# dyad census of random network
igraph::dyad.census(erdos_renyi.igraph)
$mut
[1] 3

$asym
[1] 194

$null
[1] 5056
Code
# triad census of random network
igraph::triad.census(erdos_renyi.igraph)
 [1] 157700  18133    283    166    190    349     13      6      8      2
[11]      0      0      1      0      0      0
Code
random_nodes<-data.frame(degree=igraph::degree(erdos_renyi.igraph))
random_nodes<-rand_nodes %>%
    mutate(indegree=igraph::degree(erdos_renyi.igraph, mode="in", loops=FALSE),
           outdegree=igraph::degree(erdos_renyi.igraph, mode="out", loops=FALSE))
Error in mutate(., indegree = igraph::degree(erdos_renyi.igraph, mode = "in", : object 'rand_nodes' not found
Code
head(random_nodes)
  degree
1      5
2      2
3      4
4      7
5      7
6      4
Code
summary(random_nodes)
     degree      
 Min.   : 0.000  
 1st Qu.: 3.000  
 Median : 4.000  
 Mean   : 3.883  
 3rd Qu.: 5.000  
 Max.   :10.000