#install package igraph for network analysis install.packages("igraph") library(igraph) #load data with the information on transactions between 50 different banks data <- read.csv2('network.csv') #understand the structure of the data: lender, borrower, loan amount, interest rate, date of transaction str(data) #load the characteristics of the banks (assets size) size <- read.csv2('vertices.csv') #create graph object using both datasets network <- graph.data.frame(data, vertices = size) #analyze network's basic properties #Is network fully connected? Did all banks trade with each other? is.connected(network) #Are there multiple edges? Do banks lend/borrow from the same partners? table(is.multiple(network)) #What is the percentage of banks lending/borrowing to each other? reciprocity(network) #Are there any loops in the data? Are the data collected accurately? is.loop(network) #exclude loops from the network snetwork <- simplify(network, edge.attr.comb = "sum") #plot the network plot(snetwork, edge.arrow.size = 0.2) #subset the data to analyze crisis period monthlynetwork <- subset(data, (Year == 2008) & (Month == 9)) #calculate the changes in the amount of borrowings mAmount <- with(data, aggregate(Amount, by = list(Month = Month, Year = Year), FUN = sum)) #plot the amounts of borrowings plot(ts(mAmount$x, start = c(2007, 1), frequency = 12), ylab = 'Amount') ##calculate the network density over time (calculating the values for each month) ds <- sapply(2007:2010, function(year) { sapply(1:12, function(month) { mdata <- subset(data, (Year == year) & (Month == month)) graph.density(graph.data.frame(mdata)) }) }) #plot the changes in density plot(ts(as.vector(ds), start = c(2007, 1), frequency=12)) abline(v = 2008 + 259/366, col = 'red') ## calculate the systemic importance of banks based on different centrality measures g <- graph.data.frame(data) degree <- degree(g, normalized = TRUE) between <- betweenness(g, normalized = TRUE) closeness <- closeness(g, normalized = TRUE) eigenv <- evcent(g, directed = TRUE)$vector prank <- page.rank(g) #plot the histograms for all centrality measures hist(degree) hist(between) hist(closeness) hist(eigenv) hist(prank$vector)