Today we are going to generate some random networks. Colloquially, when people say “random” they often mean “uniformly distributed” rather than “stochastic”. Importantly, there are many different ways to stochastically generate a network, and we will discuss a few day.

The point of this lesson is to get you think about the processes generating your own networks, and how you might characterize them (and give you a couple places to look when starting with these analyses). If you want a rigorous mathematical treatment of random networks I recommend any of the textbooks/courses listed in the extended resources section on the wiki (most textbooks dedicate a significant amount of space to these topics). Importantly, a typical graduate-level network science course would spend several weeks at a minimum on the topics I introduce here.

First let’s load in the packages we will be working with. This lesson will mostly use igraph, though we again use the example gut network provided with SpiecEasi. I’ll also briefly introduce the randnet package at the end of the lesson for fitting stochastic block models.

#Make sure installed
if("igraph" %in% rownames(installed.packages()) == FALSE){install.packages("igraph")}
if("SpiecEasi" %in% rownames(installed.packages()) == FALSE){devtools::install_github("zdk123/SpiecEasi")}
if("randnet" %in% rownames(installed.packages()) == FALSE){install.packages("randnet")}

## Attaching package: 'igraph'
## The following object is masked from 'package:SpiecEasi':
##     make_graph
## The following objects are masked from 'package:stats':
##     decompose, spectrum
## The following object is masked from 'package:base':
##     union
## Loading required package: Matrix
## Loading required package: entropy
## Loading required package: AUC
## AUC 0.3.0
## Type AUCNews() to see the change log and ?AUC to get an overview.

Erdos-Reyni Networks

The Erdos-Reyni random network model is what people usually mean when they say “random network”. This type of network can be generated in two ways:
1. Setting a number of edges L and randomly placing them between nodes
2. Setting a probability p that any two nodes are connected by an edge

We will use approach 2, which is a little easier to work with mathematically and is more widely used (though #1 is the original definition).

Let’s give our network 100 nodes, and a probability of 0.1 that any given pair of nodes is connected (each node should have around one connection, a bit less).

n <- 100
p <- 0.1 <-, p, type = "gnp")