epaul epaul - 2 months ago 16
R Question

How to generate a series of random networks in R

I am an R beginner, so please bear with me.

I am attempting to create a set of around 100 random networks, to compare with my original network of 60 nodes and 246 weighted links.

I would like each network to have a random number of nodes between 0-100, each with a random density (i.e. random probability that any one node will connect with another), and randomly weighted edges between 1-5.

I'm very new to network analysis in R, so I am not sure how to create a script that allows me to output 100 of these (guessing with a loop), nor how to specify random edge densities and weights.

Any advice would be great.

Answer

igraph is a good start:

library(igraph)
set.seed(1)
gs <- list()
for (x in seq_len(100L)) {
  gs[[x]] <- erdos.renyi.game(sample(1:100, 1), p.or.m = runif(1))
  E(gs[[x]])$weight <- sample(1:5, ecount(gs[[x]]), T)
}
plot(gs[[1]], edge.width = E(gs[[1]])$weight) # plot first graph

See ?erdos.renyi.game for the documentation on the random graph generation.