Version info: Code for this page was tested in R Under development (unstable) (2012-07-05 r59734)
On: 2012-08-08
With: knitr 0.6.3
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, 0.5))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] ## [1,] 0 1 0 0 1 0 1 1 1 0 ## [2,] 1 0 0 1 1 1 0 0 1 0 ## [3,] 0 0 0 0 0 1 0 1 1 0 ## [4,] 0 1 0 0 0 0 0 0 0 1 ## [5,] 1 1 0 0 0 1 0 0 0 0 ## [6,] 0 1 1 0 1 0 1 1 1 1 ## [7,] 1 0 0 0 0 1 0 0 1 0 ## [8,] 1 0 1 0 0 1 0 0 0 0 ## [9,] 1 1 1 0 0 1 1 0 0 1 ## [10,] 0 0 0 1 0 1 0 0 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.
library(MCMCpack)
n <- 10 p <- 0.5 network <- xpnd(rbinom((n^2 + n)/2, 1, p), n) (network <- network - diag(diag(network)))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] ## [1,] 0 0 0 0 1 1 0 0 0 0 ## [2,] 0 0 1 1 1 0 0 1 1 1 ## [3,] 0 1 0 0 0 1 0 1 1 1 ## [4,] 0 1 0 0 1 0 0 0 1 0 ## [5,] 1 1 0 1 0 1 0 1 0 1 ## [6,] 1 0 1 0 1 0 1 0 1 0 ## [7,] 0 0 0 0 0 1 0 0 1 0 ## [8,] 0 1 1 0 1 0 0 0 1 0 ## [9,] 0 1 1 1 0 1 1 1 0 1 ## [10,] 0 1 1 0 1 0 0 0 1 0