Week 3 Challenge Instructions

challenge_3
instructions
Degree and Density of a Network
Author

Yakub Rabiutheen

Published

March 6, 2023

Code
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.1.3
-- Attaching packages --------------------------------------- tidyverse 1.3.2 --
v ggplot2 3.3.5     v purrr   0.3.4
v tibble  3.1.6     v dplyr   1.0.9
v tidyr   1.2.0     v stringr 1.4.0
v readr   2.1.2     v forcats 0.5.1
Warning: package 'ggplot2' was built under R version 4.1.3
Warning: package 'tibble' was built under R version 4.1.3
Warning: package 'tidyr' was built under R version 4.1.3
Warning: package 'readr' was built under R version 4.1.3
Warning: package 'purrr' was built under R version 4.1.3
Warning: package 'dplyr' was built under R version 4.1.3
Warning: package 'stringr' was built under R version 4.1.3
Warning: package 'forcats' was built under R version 4.1.3
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
Code
library(igraph)
Warning: package 'igraph' was built under R version 4.1.3

Attaching package: 'igraph'

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
library(statnet)
Warning: package 'statnet' was built under R version 4.1.3
Loading required package: tergm
Warning: package 'tergm' was built under R version 4.1.3
Loading required package: ergm
Warning: package 'ergm' was built under R version 4.1.3
Loading required package: network
Warning: package 'network' was built under R version 4.1.3

'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


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

'ergm' 4 is a major update that introduces some backwards-incompatible
changes. Please type 'news(package="ergm")' for a list of major
changes.

Loading required package: networkDynamic
Warning: package 'networkDynamic' was built under R version 4.1.3

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

Registered S3 method overwritten by 'tergm':
  method                   from
  simulate_formula.network ergm

'tergm' 4.1.1 (2022-11-07), part of the Statnet Project
* 'news(package="tergm")' for changes since last version
* 'citation("tergm")' for citation information
* 'https://statnet.org' for help, support, and other information


Attaching package: 'tergm'

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

    snctrl

Loading required package: ergm.count
Warning: package 'ergm.count' was built under R version 4.1.3

'ergm.count' 4.1.1 (2022-05-24), part of the Statnet Project
* 'news(package="ergm.count")' for changes since last version
* 'citation("ergm.count")' for citation information
* 'https://statnet.org' for help, support, and other information

Loading required package: sna
Warning: package 'sna' was built under R version 4.1.3
Loading required package: statnet.common
Warning: package 'statnet.common' was built under R version 4.1.3

Attaching package: 'statnet.common'

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

    snctrl

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

    attr, order

sna: Tools for Social Network Analysis
Version 2.7-1 created on 2023-01-24.
copyright (c) 2005, Carter T. Butts, University of California-Irvine
 For citation information, type citation("sna").
 Type help(package="sna") to get started.


Attaching package: 'sna'

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

    betweenness, bonpow, closeness, components, degree, dyad.census,
    evcent, hierarchy, is.connected, neighborhood, triad.census

Loading required package: tsna
Warning: package 'tsna' was built under R version 4.1.3

'statnet' 2019.6 (2019-06-13), part of the Statnet Project
* 'news(package="statnet")' for changes since last version
* 'citation("statnet")' for citation information
* 'https://statnet.org' for help, support, and other information

unable to reach CRAN
Code
library(ggplot2)

Challenge Overview

Describe the many measures of degree, as well as density, of a network and compare

Code
library(igraph)
library(network)

Degree

Reading in Climate Data.

The Climate Dataframe has a Semicolor for it’s Seperator.

Code
climate_df <- read.csv("climate0205-allop.csv", sep =  ";") 
Code
climate_df <- climate_df %>% subset(select = -c(X))

Now Turning the Climate Dataframe into an igraph Object and then a Network.

Code
climate.ig <- graph_from_adjacency_matrix(as.matrix(climate_df), weighted = TRUE, mode = "directed", add.rownames = TRUE)
climate.net <- as.network(as.matrix(climate_df), directed = TRUE, weighted = TRUE)

Printing the Network Attributes

Code
print(climate.net)
 Network attributes:
  vertices = 34 
  directed = TRUE 
  hyper = FALSE 
  loops = FALSE 
  multiple = FALSE 
  bipartite = FALSE 
  total edges= 291 
    missing edges= 0 
    non-missing edges= 291 

 Vertex attribute names: 
    vertex.names 

No edge attributes

Network attributes: vertices = 34 directed = TRUE hyper = FALSE loops = FALSE multiple = FALSE bipartite = FALSE total edges= 291 missing edges= 0 non-missing edges= 291

The Climate network has 34 vertices and is undirected, with bidirectional connections between entities. There are no loops or multiple edges present, indicating a simpler structure. The bipartite nature of the network divides the vertices into two distinct sets. In total, there are 34 edges representing the direct relationships between entities, with no missing connections in the network representation.

Dyad Census

Code
dyad.census(climate.net)
     Mut Asym Null
[1,]  65  161  335
 Mut Asym Null

[1,] 65 161 335 -65 mutually connected dyads, indicating bidirectional connections between pairs of entities. - 151 Asymmetrical -365 Null dyads.

Triad Census

Code
triad.census(climate.net)
      003  012 102 021D 021U 021C 111D 111U 030T 030C 201 120D 120U 120C 210
[1,] 1644 1641 497  347  255  231  239  314  175    3 126  100  116   81 164
     300
[1,]  51
  003  012 102 021D 021U 021C 111D 111U 030T 030C 201 120D 120U 120C 210 300

[1,] 1644 1641 497 347 255 231 239 314 175 3 126 100 116 81 164 51

Triad Census -1644 triads with no ties, suggesting the presence of isolated entities or disconnected clusters within the network

##Create Climate Node

Code
name <- climate.net %v% "vertex.names"
degree <- degree(climate.net)
indegree <- degree(climate.net, cmode = "indegree")
outdegree <- degree(climate.net, cmode = "outdegree")

climate.nodes <- data.frame(
  name = name,
  degree = degree,
  indegree = indegree,
  outdegree = outdegree
)
Code
mean(climate.nodes$degree)
[1] 17.11765

Average is 17.

Code
summary(climate.nodes)
     name               degree         indegree        outdegree     
 Length:34          Min.   : 2.00   Min.   : 0.000   Min.   : 0.000  
 Class :character   1st Qu.:10.00   1st Qu.: 5.250   1st Qu.: 3.000  
 Mode  :character   Median :16.50   Median : 7.500   Median : 8.000  
                    Mean   :17.12   Mean   : 8.559   Mean   : 8.559  
                    3rd Qu.:21.00   3rd Qu.:11.000   3rd Qu.:11.000  
                    Max.   :40.00   Max.   :22.000   Max.   :29.000  
 name               degree         indegree        outdegree     

Length:34 Min. : 2.00 Min. : 0.000 Min. : 0.000
Class :character 1st Qu.:10.00 1st Qu.: 5.250 1st Qu.: 3.000
Mode :character Median :16.50 Median : 7.500 Median : 8.000
Mean :17.12 Mean : 8.559 Mean : 8.559
3rd Qu.:21.00 3rd Qu.:11.000 3rd Qu.:11.000
Max. :40.00 Max. :22.000 Max. :29.000

-34 vertices in total,

-the degree distribution ranges from a minimum of 2 to a maximum of 40. =

  • The median degree of 16.5 further reinforces this observation, indicating that half of the vertices have a degree of 16 or lower

Density

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

Code
network.density(climate.net)
[1] 0.2593583

[1] 0.2593583 The Density is 0.259, which indicates approximately 25.9% of the possible edges are present in the network.

Density is a global measure that quantifies the proportion of actual edges present in the network compared to the total number of possible edges.

With an average degree of 17.17 and a density of 0.259, we observe a moderate level of connectivity in the “climate.nodes” network. The average degree signifies a relatively high number of edges per vertex, indicating a denser network.

Random Network

Create a random (Erdos-Renyi) network with the same number of nodes and edges than the network of your choice. On igraph, the necessary commands are random.graph.game(n, p.or.m, type = c("gnp", "gnm"), directed = FALSE, loops = FALSE) (deprecated), sample_gnp(n, p, directed = FALSE, loops = FALSE) or sample_gnm(n, m, directed = FALSE, loops = FALSE). The p.or.m argument is to specify the probability of an edge \(p\) or the number of edges \(m\).

Compare the densities, dyad and triad censuses, and degree distributions, with the measures of the network of your choice. Does the comparison us something about the network of your choice?

Code
set.seed(1)
random.ig <- sample_gnm(34, 291)
dyads <- dyad_census(random.ig)
triads <- triad_census(random.ig)
Warning in triad_census(random.ig): At core/misc/motifs.c:1165 : Triad census
called on an undirected graph.
Code
density <- edge_density(random.ig)
degrees <- igraph::degree(random.ig)
Code
summary(dyads)
     Length Class  Mode   
mut  1      -none- numeric
asym 1      -none- numeric
null 1      -none- numeric
Code
summary(triads)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    0.0     0.0     0.0   374.0   171.5  2334.0 
Code
summary(density)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.5187  0.5187  0.5187  0.5187  0.5187  0.5187 

summary(triads) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0 0.0 0.0 374.0 171.5 2334.0 summary(density) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.5187 0.5187 0.5187 0.5187 0.5187 0.5187

Both networks have the same density of 0.5187, indicating a similar level of overall connectivity in terms of edge presence relative to the total number of possible edges. However, the Climate Network had more complete triads compared to the randomly generated graph