UCLA Academic Technology Services HomeServicesClassesContactJobs

R Code Fragments
How can I simulate a network in R?

Network data is defined by the existence, strength, or nature of relationships between subjects.  For examples, we can think of cities as subjects and the presence or absence of an airplane flight between the cities as defining whether or not two cities are connected; or we can think of a group of students and look, pair by pair, at which students have zero, one, or more than one class in common.  Often these networks are represented as graphs in which subjects are nodes with an edge between them if they are connected.  Alternatively, we can represent a network of n subjects as an n by n matrix in which entry (i,j) indicates the relationship between subjects i and j.  If the relationships are symmetric--that is, the relationship of i to j is the same as the relationship from j to i--then the matrix will also be symmetric.

We may wish to simulate a symmetric network matrix with n members where the probability that two members are connected is p, a connection is represented by 1, and a non-connection is represented by 0.  The code below is an R function that takes as arguments the number of members in the network and the probability of two members in the network being connected.

generate.network = function(n, p){

       # Generate matrix values, sampling 0 or 1 with given probabilities
       matvals = sample (c(0,1), n*(n-1)/2, replace = TRUE, prob = c(1-p, p))

       # From the values above, generate a symmetric matrix 
       networkmat = matrix(rep(0, n*n), ncol = n)
       mv = 1
       for (i in 1:n){
               for (j in 1:n){
                       if (i>j) {
                               networkmat [i,j] = matvals[mv]
                               networkmat [j,i] = matvals[mv]
                               mv = mv + 1
                       }
               }
       }
       return(networkmat)
}

network = generate.network(10, .5)

network

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    1    1    1    0    0    0    1     0
 [2,]    1    0    1    0    0    1    1    0    1     1
 [3,]    1    1    0    1    1    0    0    1    0     0
 [4,]    1    0    1    0    0    1    0    0    0     0
 [5,]    1    0    1    0    0    1    0    1    0     0
 [6,]    0    1    0    1    1    0    0    0    0     1
 [7,]    0    1    0    0    0    0    0    1    1     1
 [8,]    0    0    1    0    1    0    1    0    0     1
 [9,]    1    1    0    0    0    0    1    0    0     1
[10,]    0    1    0    0    0    1    1    1    1     0

Note that our matrix has zeroes down the diagonal and is symmetric.  Different values of p will yield different levels of connectedness in the network.

The same simulation can be achieved with just a few lines of code using the xpnd command in the MCMCpack library.


install.packages("MCMCpack")
library(MCMCpack)

n = 10
p = .5 

network <- xpnd(rbinom((n^2 + n)/2, 1, p), n)
network <- network - diag(diag(network))

network

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    1    0    0    1    1    0    1     1
 [2,]    1    0    0    0    1    1    1    1    1     0
 [3,]    1    0    0    0    0    0    1    1    1     1
 [4,]    0    0    0    0    0    0    1    0    0     1
 [5,]    0    1    0    0    0    1    1    1    0     1
 [6,]    1    1    0    0    1    0    1    0    1     0
 [7,]    1    1    1    1    1    1    0    1    1     0
 [8,]    0    1    1    0    1    0    1    0    0     1
 [9,]    1    1    1    0    0    1    1    0    0     1
[10,]    1    0    1    1    1    0    0    1    1     0

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.