hw_challenge_3
Cynthia Hester
Degree and Density of a Network
Author

Cynthia Hester

Published

March 6, 2023

GOT distances data set

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

Import Data

Code
library(readr)

got_distances <- readr::read_csv("C:/Users/Bud/Desktop/DACSS_697E_Social Network Analysis_SP2023/Social_Networks_SP_2023/posts/_data/got/got_distances.csv")
Rows: 200 Columns: 6
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (5): Region From, From, To, Mode, Notes
dbl (1): Miles

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_distances)
Code
got_distances.ig <-graph_from_data_frame(got_distances, directed = T)
Code
#nodes
vcount(got_distances.ig)
Code
#edges
ecount(got_distances.ig)
Code
#names of nodes attributes
vertex_attr_names(got_distances.ig)
Code
#edge attributes

edge_attr_names(got_distances.ig)
Code
#dyad census

igraph::dyad.census(got_distances.ig)
Code
#triad census

igraph::triad.census(got_distances.ig)

Density

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

Code
# density
graph.density(got_distances.ig)
Code
# density without loops
graph.density(got_distances.ig, loops=FALSE)
Code
# density with loops
graph.density(got_distances.ig, loops=TRUE)
Code
# the degree of each node
node_degrees <- igraph::degree(got_distances.ig)
print(node_degrees)


# average degree
avg_degree <- mean(node_degrees)
print(avg_degree)
Code
# data frame with node names and degree
graph_nodes <- data.frame(name = V(got_distances.ig)$name, degree = igraph::degree(got_distances.ig, loops = FALSE))

# in_degree and out_degree 
graph_nodes <- graph_nodes %>%
    mutate(in_degree = igraph::degree(got_distances.ig, mode = "in", loops = FALSE),
           out_degree = igraph::degree(got_distances.ig, mode = "out", loops = FALSE))

rechecking rows

Code
head(graph_nodes)
Code
num_nodes <- vcount(got_distances.ig)
num_edges <- ecount(got_distances.ig)
Code
print(num_nodes)
print(num_edges)
Code
random_network_erdos <- sample_gnm(n = num_nodes, m = num_edges, directed = FALSE, loops = FALSE)

print(random_network_erdos)

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
# Density
density_original <- graph.density(got_distances.ig)
density_random <- graph.density(random_network_erdos)

print(paste("Density of GOT Distances network: ", density_original))
print(paste("Density of random network: ", density_random))

# Dyad census
dyad_original <- dyad_census(got_distances.ig)
dyad_random <- dyad_census(random_network_erdos)

print(dyad_original)
print(dyad_random)

# Triad census
triad_original <- triad_census(got_distances.ig)
triad_random <- triad_census(random_network_erdos)
Warning in triad_census(random_network_erdos): At core/misc/motifs.c:1165 :
Triad census called on an undirected graph.
Code
print(triad_original)
print(triad_random)

# Degree distribution

degree_original <- degree_distribution(got_distances.ig, cumulative = TRUE)
degree_random <- degree_distribution(random_network_erdos, cumulative = TRUE)

plot(degree_original, main="Degree Distribution of GOT Distances", xlab="Degree", ylab="Frequency", type='l')

Code
plot(degree_random, main="Degree Distribution of Random Network_Erdos-Renyi", xlab="Degree", ylab="Frequency", type='l')