Week3_Challenge_Niharika Pola

challenge_1
instructions
Degree and Density of a Network
Author

Niharika Pola

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 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ lubridate::%--%()      masks igraph::%--%()
✖ dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
✖ purrr::compose()       masks igraph::compose()
✖ tidyr::crossing()      masks igraph::crossing()
✖ dplyr::filter()        masks stats::filter()
✖ dplyr::lag()           masks stats::lag()
✖ purrr::simplify()      masks igraph::simplify()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Code
library(readr)
Code
got_distances <- read_csv("_data/got/got_distances.csv")
Rows: 200 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): Region From, From, To, Mode, Notes
dbl (1): Miles

ℹ 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(got_distances)
# A tibble: 6 × 6
  `Region From` From          To               Miles Mode  Notes   
  <chr>         <chr>         <chr>            <dbl> <chr> <chr>   
1 Westerlands   Casterly Rock the Golden Tooth   240 land  <NA>    
2 Westerlands   Casterly Rock Lannisport          40 land  <NA>    
3 Westerlands   Casterly Rock Kayce              100 land  <NA>    
4 Westerlands   Casterly Rock Kayce               12 water <NA>    
5 Westerlands   Casterly Rock Deep Den           240 land  Goldroad
6 Westerlands   Deep Den      King’s Landing     590 land  Goldroad
Code
got_distances.ig <-graph_from_data_frame(got_distances, directed = TRUE)
Code
# number of edges
ecount(got_distances.ig)
[1] 200
Code
# number of vertices
vcount(got_distances.ig)
[1] 103
Code
# vertex and edge attributes
vertex_attr_names(got_distances.ig)
[1] "name"
Code
edge_attr_names(got_distances.ig)
[1] "To"    "Miles" "Mode"  "Notes"
Code
# network features
is_directed(got_distances.ig)
[1] TRUE
Code
is_bipartite(got_distances.ig)
[1] FALSE
Code
is_weighted(got_distances.ig)
[1] FALSE
Code
# dyad census
igraph::dyad.census(got_distances.ig)
$mut
[1] 0

$asym
[1] 93

$null
[1] 5160
Code
# triad census
triad_census(got_distances.ig)
 [1] 167960   3917   4472    502      0      0      0      0      0      0
[11]      0      0      0      0      0      0

The vertex attribute is ‘name’ and edge attributes are “To”, “Miles”, “Mode”, “Notes”.

Code
# number of components
igraph::components(got_distances.ig)$no
[1] 10
Code
# size of components
igraph::components(got_distances.ig)$csize 
 [1]  9 12  7 13  6 10 12 13  1 20

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_distances.ig)
[1] 0.01903674
Code
# density without loops
graph.density(got_distances.ig, loops=TRUE)
[1] 0.01885192
Code
# average network degree
igraph::degree(got_distances.ig)
                   Westerlands                          Reach 
                            14                             19 
                    Riverlands                           Vale 
                            18                             19 
                    Crownlands                     Stormlands 
                            13                             13 
                         Dorne                          North 
                            16                             26 
                  Iron Islands                          Essos 
                            10                             57 
                 Casterly Rock                       Deep Den 
                             5                              1 
                         Kayce                     Faircastle 
                             1                              1 
                      the Crag                       Ashemark 
                             1                              2 
                    Lannisport                      Crakehall 
                             2                              1 
                       Old Oak                     Highgarden 
                             1                              5 
                       Oldtown                     Blackcrown 
                             2                              2 
                  Three Towers      Mouth of the Mander River 
                             1                              1 
                  Golden Grove                     Cider Hall 
                             1                              2 
                     Longtable                       Harroway 
                             2                              7 
                       Seagard                       Riverrun 
                             3                              3 
                     Harrenhal                       Saltpans 
                             2                              1 
                    Maidenpool                         Border 
                             2                              1 
               the Bloody Gate                      Wickenden 
                             1                              2 
                      Gulltown                      Runestone 
                             1                              2 
                    Old Anchor                   Longbow Hall 
                             2                              2 
                    Snakewoods                   Heart’s Home 
                             3                              1 
                   the Sisters                  Pebble Island 
                             2                              1 
                      the Paps                 King’s Landing 
                             1                              7 
                    Duskendale                    Rook’s Rest 
                             2                              1 
                   Sharp Point                    Dragonstone 
                             1                              2 
                     Wendwater                    Bronze Gate 
                             1                              1 
                   Storm’s End                     Summerhall 
                             4                              1 
                    Blackhaven                     Rain House 
                             1                              1 
                      Mistwood                  Weeping Tower 
                             2                              1 
                     Stonehelm                            Wyl 
                             1                              1 
                      Yronwood                        the Tor 
                             1                              1 
                     Godsgrace                      Saltshore 
                             3                              1 
                         Vaith                       Hellholt 
                             1                              3 
                      Skyreach                    Moat Cailin 
                             1                              2 
                        Cerwyn                Greywater Watch 
                             1                              1 
                Flint’s Finger                   White Harbor 
                             1                              2 
                      Ramsgate                  Widow’s Watch 
                             2                              1 
                       Karhold                      Dreadfort 
                             2                              2 
                    Winterfell                 Deepwood Motte 
                             5                              2 
                     Barrowton             the Shield Islands 
                             5                              1 
                     the Arbor the Southern Dornish Coastline 
                             1                              1 
                     Lemonwood                       Sunspear 
                             2                              1 
                        Tyrosh                         Pentos 
                             7                              2 
                       Braavos                         Lorath 
                             6                              1 
                           Lys                       Volantis 
                             2                              5 
                      New Ghis                        Astapor 
                             9                              2 
                        Yunkai                    Dagger Lake 
                             2                              2 
                the Stepstones                            Myr 
                             1                              3 
                      Selhorys                          Qohor 
                             2                              1 
                        Norvos                   Vaes Dothrak 
                             2                              4 
                      Mantarys                        Bhorash 
                             2                              1 
                       Meereen 
                             3 
Code
ig_nodes<-data.frame(name=V(got_distances.ig)$name, degree=igraph::degree(got_distances.ig,loops=FALSE))
ig_nodes<-nodes_ig %>%
    mutate(indegree=igraph::degree(got_distances.ig, mode="in", loops=FALSE),
           outdegree=igraph::degree(got_distances.ig, mode="out", loops=FALSE))
Error in mutate(., indegree = igraph::degree(got_distances.ig, mode = "in", : object 'nodes_ig' not found
Code
head(ig_nodes)
                   name degree
Westerlands Westerlands     14
Reach             Reach     19
Riverlands   Riverlands     18
Vale               Vale     19
Crownlands   Crownlands     13
Stormlands   Stormlands     13
Code
erdos_renyi.ig <- sample_gnm(103, 200, directed = TRUE, loops = FALSE)
# density of random network
graph.density(erdos_renyi.ig)
[1] 0.01903674
Code
# dyad census of random network
igraph::dyad.census(erdos_renyi.ig)
$mut
[1] 2

$asym
[1] 196

$null
[1] 5055
Code
# triad census of random network
igraph::triad.census(erdos_renyi.ig)
 [1] 157566  18392    188    163    164    357      7      6      4      3
[11]      0      0      1      0      0      0
Code
rand_nodes<-data.frame(degree=igraph::degree(erdos_renyi.ig))
rand_nodes<-rand_nodes %>%
    mutate(indegree=igraph::degree(erdos_renyi.ig, mode="in", loops=FALSE),
           outdegree=igraph::degree(erdos_renyi.ig, mode="out", loops=FALSE))
head(rand_nodes)
  degree indegree outdegree
1      2        0         2
2      4        2         2
3      3        2         1
4      3        2         1
5      2        0         2
6      0        0         0
Code
summary(rand_nodes)
     degree         indegree       outdegree    
 Min.   :0.000   Min.   :0.000   Min.   :0.000  
 1st Qu.:3.000   1st Qu.:1.000   1st Qu.:1.000  
 Median :4.000   Median :2.000   Median :2.000  
 Mean   :3.883   Mean   :1.942   Mean   :1.942  
 3rd Qu.:5.000   3rd Qu.:3.000   3rd Qu.:3.000  
 Max.   :8.000   Max.   :5.000   Max.   :5.000