Social Network Analysis

Worksheet 1: Introduction

Author

Termeh Shafie

R packages for network analysis

Throughout the course we will use a variety of different packages of doing network analysis, modeling and visualization. Make sure to install them all and have them ready to load when needed:

install.packages("igraph")   
install.packages("statnet")  #installs ergm, network, and sna
install.packages("snahelper")
install.packages("netUtils")
install.packages("ggraph")
install.packages("backbone")
install.packages("netrankr")
install.packages("signnet")
install.packages("intergraph")
install.packages("graphlayouts")
install.packages("visNetwork")
install.packages("patchwork")
install.packages("edgebundle")
install.packages("ggplot2")
install.packages("gganimate")
install.packages("ggforce")
install.packages("rsiena")

install.packages("remotes")
remotes::install_github("schochastics/networkdata")

Load Packages

We will start with loading the following packages:

library(igraph)
library(networkdata)
library(netUtils)

Handling package conflicts

never load sna and igraph at the same time

library(sna)
data("flo_marriage")
degree(flo_marriage)
Error in FUN(X[[i]], ...): as.edgelist.sna input must be an adjacency matrix/array, edgelist matrix, network, or sparse matrix, or list thereof.

If for any reason you have to load both, you can circumvent the error by explicitly stating package first

igraph::degree(flo_marriage)
  Acciaiuoli      Albizzi    Barbadori     Bischeri   Castellani       Ginori 
           1            3            2            3            3            1 
    Guadagni Lamberteschi       Medici        Pazzi      Peruzzi        Pucci 
           4            1            6            1            3            0 
     Ridolfi     Salviati      Strozzi   Tornabuoni 
           3            2            4            3 

The package intergraph can be used to transform an igraph object into a network object and vice versa.

#install.packages("intergraph")
library(intergraph)
asNetwork(flo_marriage)
 Network attributes:
  vertices = 16 
  directed = FALSE 
  hyper = FALSE 
  loops = FALSE 
  multiple = FALSE 
  bipartite = FALSE 
  total edges= 20 
    missing edges= 0 
    non-missing edges= 20 

 Vertex attribute names: 
    vertex.names wealth X.priors X.ties 

No edge attributes
degree(asNetwork(flo_marriage))
 [1]  2  6  4  6  6  2  8  2 12  2  6  0  6  4  8  6

You can unload a package without restarting R/RStudio.

detach("package:sna", unload = TRUE)