For my first PhD project, I learned an exciting new method: Network analysis.

While there is lots of information out there, this is a relatively new method and the material often assumes an already quite thorough understanding. Here, you will find an easy to follow example of how to get started with network analysis, making it look pretty and helpful resources that guided me throughout my project.

In this introduction, we will use two data sets to

  1. Visualize a social network. We will change the layout and define groups using qgraph.

  2. Create a random matrix. Using Bootnet we will estimate a network, change the themes and make it look pretty. Lastly, we will cluster the matrix.

If you are interesting in reading more on network analysis in the wild, here is the link to the code of my article “Networks of Adversity in Childhood and Adolescence and their Relationship to Adult Mental Health”.


References are linked throughout the text.


1 Introduction

Network models are statistical structures designed to extract networks from data.

The basis of Network analysis

Variables are represented as nodes (N).

Relationships between variables are presented as edges (E) (lines), which can partial correlation coefficients. More information can be found here.

Your graph “G” contains a set of “N” nodes and “E” edges:

Adjacency matrix

An adjacency matrix is a method to format data in network analysis, as they reflect if there is a relationship between nodes (are they “adjacent” in the network?). An adjacency network is an NxN matrix, with N being the number of nodes (e.g., for three nodes – 3x3). Each element can be 0 or 1, with 0 representing no edge and 1 representing an edge.


1.1 Getting started

  1. If you have a basic understanding of network analysis, this is a useful introduction by the Amsterdam Summer School 2020.
  2. The most used R packages for network analysis are currently qgraph or igraph. Guidelines on which estimation methods to use can be found here.
  3. Bootnet. Bootnet allows to estimate and bootstrap networks based on the Gaussian graphical model and visualized in qgraph. For a tutorial paper, please refer to Epskamp et al. (2018).

1.1.1 Install required packages

#loading packages
library(qgraph)
library(bootnet)
library(ggplot2)
library(misty)
library(devtools)
library(EGAnet)
library(MASS)
library(rockchalk)

2 Method 1: Social Network

To get started please download the Game of Thrones data set here. Store it in the same folder as your RmD file.

2.1 Read in Data

GoT_Data <- read.csv("stormofswords.csv")

qgraph(GoT_Data)

2.2 Define Groups

Now, you can do lots of fun things with the network. For example, you could illustrate different groups. In this case different “houses” or families inside GoT.

#By changing the number behind the houses you can change the group membership
Groups <- c(rep("House Targaryen",15),rep("House Stark",10))

qgraph(GoT_Data, groups = Groups)

2.3 Change Layout

#Change layout
qgraph(GoT_Data, groups = Groups, layout = "circle")

#or try "groups". Remove "#" to run the next line.
#qgraph(GoT_Data, groups = Groups, layout = "groups")

3 Method 2: Random Network

3.1 Create Matrix

R <- lazyCor(X = 0.75, d = 13)
SD <- c(0.3, 0.5, -0.8, 0.2, 0.5, 0.6, 1.5, 1.2, 0.3, 1.2, 0.5, -0.2, 1.1)

Matrix <- lazyCov(Rho = R, Sd = SD)

3.2 Estimate Network using Bootnet

Bootnet allows to estimate and bootstrap networks based on the Gaussian graphical model, visualized in qgraph. Please refer to Epskamp et al. (2018).

Matrix_Network <- estimateNetwork(Matrix, default = "EBICglasso")

plot(Matrix_Network) 

3.3 Change Theme

#change themes, for example: theme = "Fried" or "Hollywood"

plot(Matrix_Network, theme = "Reddit") 

3.4 Pretty Network Analysis Layout

Here, we are using the colorblind-friendly colormap “viridis”, as seen in “Networks of Adversity in Childhood and Adolescence and their Relationship to Adult Mental Health”.

plot(Matrix_Network, posCol = "gray25", negCol = "grey", negDashed = TRUE, fade = FALSE, 
     esize = 4, color = c("#440154FF", "#21908CFF","#3B528BFF", "#5DC863FF", "#9FDA3AFF"), 
     border.color = "white", border.width = 2, label.color = "white", 
     vsize = 8.5, curve = 0.6, curveAll = T)

3.5 Centrality

Centrality of node

  • The centrality of a node is the relative importance of a node

  • This is measured through strength, betweenness, closeness, and expected influence

For example: Node strength

  • To determine node strength, the number of direct connections to other nodes as well as the absolute strength-values of connections is taken into account
centralityPlot(Matrix_Network, include = c("Strength", "Closeness","Betweenness","ExpectedInfluence"))

3.6 Clustering

Clusterring highlights how nodes are interconnected among themselves in contrast to other clusters in the network.

Please refer to this tutorial by E. Fried on clustering methods in network analysis and this paper by Golino & Epskamp (2017) specifically on the EGA algorithm.

EGAmatrix <-EGA(Matrix, plot.EGA = TRUE, plot.type = c("qgraph"), n=13)

***

4 Additional analysis

Bridge nodes.

We calculate bridge nodes/centrality using bridge strength. Bridge strength refers to the sum of the absolute value of all edges between a node of a cluster to all the nodes of the opposing cluster. Please refer to networktools, Jones et al. (2019) and Vanzulah (2017).

Network Comparison Test.

Network Comparison Test (NCT) is a permutation test. The NCT estimates the network structure and calculates a metric that functions as the observed test statistic. Group membership is multiple times rearranged via permutation, followed by a recalculation of the network structure and test statistic. This is resulting in a reference distribution. The NCT then compares the first observed test statistic with this reference distribution, indicating whether the observed test statistic is significantly different. For network comparisons, please refer to Borkulo et al. (2017).


Thank you for reading my introduction to & recommendations for materials on network analysis! If you have any questions, please don’t hesitate to get in touch.