The following objects are masked from 'package:stats':
decompose, spectrum
The following object is masked from 'package:base':
union
Code
library(network)
Warning: package 'network' was built under R version 4.2.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
Challenge Overview
Describe the many measures of degree, as well as density, of a network and compare
From To Type Notes Generation
1 Targaryen Stark Married R+L=J Current
2 Baratheon Martell Engaged died Current
3 Baratheon Stark Engaged broken Current
4 Martell Essos Married Current
5 Martell Reach Affair Current
6 Martell Essos Affair Current
The “got_marriages” network consists of 20 nodes, which represent characters, and 255 edges, which represent the marriage relation between the characters. The edges are not weighted. The network is not bipartite, meaning that all nodes are of the same type, in this case, characters. Additionally, the network is directed, indicating that the marriage are mutual between the characters.
The analysis also shows that each node has a “name” attribute, which corresponds to the name of the character it represents. The names of all the characters are listed in the output. Additionally, each edge has “Type”, “Notes”,“Generation” attributes, which corresponds to the relationship between the characters connected by the edge.
This analysis provides useful insights into the relationships between the characters in the GoT Marriage Dataset. By studying the Edge Attribute Type, it is possible to identify the relationship status of the chcaracters.
The output shows that:
The network got_like_dislike has 20 nodes and 255 edges.
The network is not bipartite.
The network is directed.
The network doesn’t have weighted edges.
The only vertex attribute in the network is name, which gives the names of all the nodes.
The names of all the nodes in the network are shown in the output.
The edge attributes in the network “Type”, “Notes”,“Generation”
Code
# Number of vertices in got_marriages network:vcount(got_marriages.ig)
[1] 20
Code
# Number of edges in got_marriages network:ecount(got_marriages.ig)
[1] 255
Code
# Bipartite or single mode network verification in got_marriages network:is_bipartite(got_marriages.ig)
[1] FALSE
Code
# Directed or undirected edges verification in got_marriages networkis_directed(got_marriages.ig)
[1] TRUE
Code
# Weighted or unweighted edges verification in got_marriages networkis_weighted(got_marriages.ig)
[1] FALSE
Code
# Is Connected verification in got_marriages networkis_connected(got_marriages.ig)
[1] TRUE
Code
# Vertex attributes for got_marriages networkvertex_attr_names(got_marriages.ig)
[1] "name"
Code
# Vertex attribute, Nodes in got_marriages network:V(got_marriages.ig)$name
The first analysis calculates the dyad census, which classifies all possible pairs of nodes in the network into three categories: mutual ties, asymmetric ties and null ties. In this case, there were 3 mutual ties and 57 null ties, and 130 asymmetric ties.
The triad census counts the number of each possible combination of three nodes in a network, which are called triads. The function returns a vector with 16 values, each corresponding to the count of a particular triad type. In this case, there are 408 triads of type 003, 227 triads of type 012, 217 triads of type 102, 110 triads of type 021D, 44 triads of type 021Um, 47 triads of 021C type etc.
Finally, the code sums up the total number of triads returned by the triad census, which is 1140 This provides an overall picture of the patterns of relationships between nodes in the network.
Code
# Classify all Dyads in the got_like_dislike network: igraphigraph::dyad.census(got_marriages.ig)
$mut
[1] 3
$asym
[1] 57
$null
[1] 130
Code
# Classify all Triads in the got_like_dislike Network: igraphigraph::triad_census(got_marriages.ig)
[1] 408 227 217 110 44 47 9 18 50 1 0 3 5 1 0 0
Code
# Total number of Triads returned by triad censussum(igraph::triad_census(got_marriages.ig))
[1] 1140
Degree
The degree distribution of the “got_marriages” network, obtained from the GoT Marriage Dataset, provides insights into the connectivity of characters within the context of marriages. The degree of a node represents the number of connections or marriages it has with other characters in the network.
Network In Degree, Out Degree, Degree are calculated below:
Network density measures the level of connectivity within a network. It is the ratio of the actual number of connections to the maximum possible connections. It provides insight into how tightly connected the nodes are within the network structure. Density is a local measure, focusing on internal connections rather than external factors. In simple undirected networks, density is related to average degree, but this relationship may not hold for all network types. Considering the specific characteristics of the network being studied is crucial for understanding the relationship between density and average degree.
Code
graph.density(got_marriages.ig)
[1] 0.6710526
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
erdos_renyi.ig <-sample_gnm(200, 200, directed =TRUE, loops =FALSE)# density of random networkgraph.density(erdos_renyi.ig)
[1] 0.005025126
Code
# dyad census of random networkigraph::dyad.census(erdos_renyi.ig)
$mut
[1] 0
$asym
[1] 200
$null
[1] 19700
Code
# triad census of random networkigraph::triad.census(erdos_renyi.ig)
---title: "Week 3 Challenge Instructions"author: "Akhilesh Kumar"description: "Degree and Density of a Network"date: "05/06/2023"format: html: toc: true code-fold: true code-copy: true code-tools: true# editor: visualcategories: - challenge_3 - instructions---```{r}library(igraph)library(network)```## Challenge OverviewDescribe the many measures of degree, as well as density, of a network and compare ## Describe the Network Data```{r}got_marriages <-read.csv("_data/got/got_marriages.csv")head(got_marriages)got_marriages.ig <-graph_from_data_frame(got_marriages, directed =TRUE)```## Network Size, features and attributesThe "got_marriages" network consists of 20 nodes, which represent characters, and 255 edges, which represent the marriage relation between the characters. The edges are not weighted. The network is not bipartite, meaning that all nodes are of the same type, in this case, characters. Additionally, the network is directed, indicating that the marriage are mutual between the characters.The analysis also shows that each node has a "name" attribute, which corresponds to the name of the character it represents. The names of all the characters are listed in the output. Additionally, each edge has "Type", "Notes","Generation" attributes, which corresponds to the relationship between the characters connected by the edge. This analysis provides useful insights into the relationships between the characters in the GoT Marriage Dataset. By studying the Edge Attribute Type, it is possible to identify the relationship status of the chcaracters. The output shows that:- The network got_like_dislike has 20 nodes and 255 edges.- The network is not bipartite.- The network is directed.- The network doesn't have weighted edges.- The only vertex attribute in the network is name, which gives the names of all the nodes.- The names of all the nodes in the network are shown in the output.- The edge attributes in the network "Type", "Notes","Generation"```{r}# Number of vertices in got_marriages network:vcount(got_marriages.ig)# Number of edges in got_marriages network:ecount(got_marriages.ig)# Bipartite or single mode network verification in got_marriages network:is_bipartite(got_marriages.ig)# Directed or undirected edges verification in got_marriages networkis_directed(got_marriages.ig)# Weighted or unweighted edges verification in got_marriages networkis_weighted(got_marriages.ig)# Is Connected verification in got_marriages networkis_connected(got_marriages.ig)# Vertex attributes for got_marriages networkvertex_attr_names(got_marriages.ig)# Vertex attribute, Nodes in got_marriages network:V(got_marriages.ig)$name# Edge attributes for got_marriages network:edge_attr_names(got_marriages.ig)E(got_marriages.ig)$TypeE(got_marriages.ig)$NotesE(got_marriages.ig)$Generation```## Diad & Triad AnalysisThe first analysis calculates the dyad census, which classifies all possible pairs of nodes in the network into three categories: mutual ties, asymmetric ties and null ties. In this case, there were 3 mutual ties and 57 null ties, and 130 asymmetric ties.The triad census counts the number of each possible combination of three nodes in a network, which are called triads. The function returns a vector with 16 values, each corresponding to the count of a particular triad type. In this case, there are 408 triads of type 003, 227 triads of type 012, 217 triads of type 102, 110 triads of type 021D, 44 triads of type 021Um, 47 triads of 021C type etc.Finally, the code sums up the total number of triads returned by the triad census, which is 1140 This provides an overall picture of the patterns of relationships between nodes in the network.```{r}# Classify all Dyads in the got_like_dislike network: igraphigraph::dyad.census(got_marriages.ig)# Classify all Triads in the got_like_dislike Network: igraphigraph::triad_census(got_marriages.ig)# Total number of Triads returned by triad censussum(igraph::triad_census(got_marriages.ig))```## DegreeThe degree distribution of the "got_marriages" network, obtained from the GoT Marriage Dataset, provides insights into the connectivity of characters within the context of marriages. The degree of a node represents the number of connections or marriages it has with other characters in the network.Network In Degree, Out Degree, Degree are calculated below:```{r}in_degree = igraph::degree(got_marriages.ig, mode ="in")out_degree = igraph::degree(got_marriages.ig, mode ="out")degree = igraph::degree(got_marriages.ig)print(degree)print(in_degree)print(out_degree)```## DensityNetwork density measures the level of connectivity within a network. It is the ratio of the actual number of connections to the maximum possible connections. It provides insight into how tightly connected the nodes are within the network structure. Density is a local measure, focusing on internal connections rather than external factors. In simple undirected networks, density is related to average degree, but this relationship may not hold for all network types. Considering the specific characteristics of the network being studied is crucial for understanding the relationship between density and average degree.```{r}graph.density(got_marriages.ig)```## Random NetworkCreate 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? ```{r}erdos_renyi.ig <-sample_gnm(200, 200, directed =TRUE, loops =FALSE)# density of random networkgraph.density(erdos_renyi.ig)# dyad census of random networkigraph::dyad.census(erdos_renyi.ig)# triad census of random networkigraph::triad.census(erdos_renyi.ig)```