Loading the libraries

library("network")
library("ggnetwork")
library("sna")
library("dplyr")
library("knitr")
library("tibble")
library("ggrepel")


setwd("C:/Users/jebet/OneDrive/Escritorio/UEXT/ULB/Social network analysis/Directory")
all_data <- read.table("alliance_v4.1_by_dyad.csv", sep=",", header=TRUE, na.strings="", stringsAsFactors = FALSE)
View(all_data)

Our Functions

We made a bunch of fuctions to help us making easy substet

subseting <- function(start_d, end_d){

  sub <- subset(all_data,
                     asymmetric == 0 & defense == 1 &
                       (dyad_st_year <= end_d | (is.na(dyad_st_year) & left_censor == 1)) & 
                       (dyad_end_year >= start_d | (is.na(dyad_end_year) & right_censor == 1)))

  country_codes <- sort(unique(c(sub$ccode1, sub$ccode2)))
  N <- length(country_codes)
  
  sub$snd <- match(sub$ccode1, country_codes)
  sub$rec <- match(sub$ccode2, country_codes)
  sub$val <- 1
  sub_edges <- sub[, c("snd", "rec", "val")]
  
  country_names_all <- data.frame(ccode = c(sub$ccode1, sub$ccode2),
                                  state = c(as.character(sub$state_name1),
                                            as.character(sub$state_name2)),
                                  stringsAsFactors = FALSE)
  
  country_names_table <- sort(unique(country_names_all))
  country_names <- country_names_table$state[match(country_codes, country_names_table$ccode)]
  
  sub_edges_b <- unique(sub_edges)
  sub_edgelist <- as.matrix(sub_edges_b) 

  attr(sub_edgelist, "n") <- N
  attr(sub_edgelist, "vnames") <- country_names
  
  sub_mat <- as.sociomatrix.sna(sub_edgelist)
  sub_net <- as.network(sub_edgelist, matrix.type = "edgelist", directed = FALSE, multiple = FALSE, edge.check = TRUE) 
}
  
plot_that <- function(sub,sub_title=""){
  
  ggplot(sub, aes(x = x, y = y, xend = xend, yend = yend)) +
    ggtitle(sub_title)+
    geom_edges(alpha = 0.5, colour = "lightgray", size = 1) +
    geom_nodetext_repel(mapping = aes(label = vertex.names),force = 14) + 
    geom_nodes() +
    theme_blank() 

  
}

central_mat <- function(sub_mat){
  
  n_sub_mat <- nrow(sub_mat)
  base_sub_df <- data.frame(Country = rownames(sub_mat), 
                            Degree = degree(sub_mat, gmode = "graph"),
                            StdDegree = degree(sub_mat, gmode = "graph")/(n_sub_mat-1),  
                            Betweenness = betweenness(sub_mat, gmode = "graph"),
                            StdBetweenness = betweenness(sub_mat, gmode = "graph")/((n_sub_mat-1)*(n_sub_mat-2)/2),
                            Closeness = closeness(sub_mat, gmode = "graph", g = 39)/(n_sub_mat-1),
                            StdCloseness = closeness(sub_mat, gmode = "graph"),
                            stringsAsFactors = FALSE)
  
}

Making our Subsets

We making for the test some subsets:

cw_net <- subseting(1946,1989)
now_net <- subseting(1990,2012)

cw_mat <- as.sociomatrix.sna(cw_net)
now_mat <- as.sociomatrix.sna(now_net)

cw_ego <- ego.extract(cw_net)
now_ego <- ego.extract(now_net)

Now we adding the plots

set.seed(7)
cw_coord <- fortify(cw_net, layout = "kamadakawai")
set.seed(3)
now_coord <- fortify(now_net, layout = "kamadakawai")

plot_that(cw_coord, "Network Showing Aliances during ColdWar")

plot_that(now_coord, "Network Showing Aliances Between 1990 and 2012")

Lets look some centrality mesures

base_cw_df <- central_mat(cw_mat)
base_now_df <- central_mat(now_mat)

sort_cw_df <- base_cw_df[order(base_cw_df$StdBetweenness ,decreasing = TRUE),]
sort_now_df <- base_now_df[order(base_now_df$StdBetweenness ,decreasing = TRUE),]


kable(sort_cw_df[1:11,], digits = 2)
Country Degree StdDegree Betweenness StdBetweenness Closeness StdCloseness
38 France 19 0.17 1842.17 0.31 0 0.46
1 United States of America 48 0.44 1802.67 0.30 0 0.46
34 United Kingdom 17 0.15 1646.62 0.27 0 0.46
93 Iraq 18 0.16 1298.36 0.22 0 0.40
52 Russia 15 0.14 1061.99 0.18 0 0.37
2 Canada 39 0.35 925.00 0.15 0 0.44
63 Mauritania 26 0.24 480.63 0.08 0 0.35
74 Gabon 14 0.13 341.66 0.06 0 0.40
75 Central African Republic 14 0.13 341.66 0.06 0 0.40
76 Chad 14 0.13 341.66 0.06 0 0.40
77 Congo 14 0.13 341.66 0.06 0 0.40
kable(sort_now_df[1:11,], digits = 2)
Country Degree StdDegree Betweenness StdBetweenness Closeness StdCloseness
82 Gabon 11 0.09 2597.00 0.33 0 0
39 France 19 0.15 2592.27 0.33 0 0
102 Sudan 28 0.22 2345.00 0.30 0 0
71 Mauritania 19 0.15 1305.00 0.17 0 0
1 United States of America 47 0.37 1173.27 0.15 0 0
2 Canada 45 0.36 972.27 0.12 0 0
85 Congo 17 0.13 468.00 0.06 0 0
94 Angola 17 0.13 468.00 0.06 0 0
83 Central African Republic 15 0.12 369.00 0.05 0 0
86 Democratic Republic of the Congo 15 0.12 369.00 0.05 0 0
90 Burundi 15 0.12 369.00 0.05 0 0
ten_cw_df <- sort_cw_df[1:11,]
ten_now_df <- sort_now_df[1:11,]

Lets look some centrality graphs

ggplot(data = ten_cw_df, aes(x =  StdBetweenness, y = StdCloseness, label = Country, size = Degree)) +
  geom_point()+
  geom_label_repel(size = 5)

ggplot(data = ten_now_df, aes(x =  StdBetweenness, y = StdCloseness, label = Country, size = Degree)) +
  geom_point()+
  geom_label_repel(size = 5)
## Warning: ggrepel: 2 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

StdBetweenes Network

ten_cw_country <- ten_cw_df$Country
ten_now_country <- ten_now_df$Country

ten_cw_mat <- cw_mat[ten_cw_country,ten_cw_country]
ten_cw_net <- as.network(ten_cw_mat,  directed = FALSE, multiple = FALSE, edge.check = TRUE)

ten_now_mat <- now_mat[ten_now_country,ten_now_country]
ten_now_net <- as.network(ten_now_mat,  directed = FALSE, multiple = FALSE, edge.check = TRUE) 


plot_that(ten_cw_net, "Most StdBetweeness CW")

plot_that(ten_now_net,"Most StdBetweeness 1990 - 2012")

#Not done yet

#library("igraph")


#cw_graph <- intergraph::asIgraph(ten_cw_mat)

#test <- cliques(cw_graph)

#plot_that(test)