Reader Comments

Post a new comment on this article

R code

Posted by jnode on 09 Nov 2011 at 12:39 GMT

Here an R code snippet to calculate the example in the Supporting Information section:

### Setting up

v <- c(1,1,1,1,1,1) # Unit vector
I <- diag(1,6) # Identity matrix

A <- matrix(0, nrow=6, ncol=6) # Initialize adjacency matrix
A[1,2] <- 0.1
A[2,3] <- 0.5
A[2,4] <- 0.5
A[2,5] <- 0.2
A[3,2] <- 0.3
A[3,5] <- 0.2
A[4,2] <- 0.3
A[4,5] <- 0.6
A[5,2] <- 0.3
A[5,3] <- 0.5
A[5,4] <- 0.5
A[5,6] <- 1


### Uncorrected computation

# From Eq. (2)
A_tilde <- solve(I - A) %*% A # Inverse of (I-A) is solve(I-A);
#matrix multiplication is %*%
c_net <- A_tilde %*% v

# Eq. (8) yields Eq. (20)
v_net <- c_net + v


### 1st correction

# Eq. (13)
D <- diag(1 / (diag(solve(I-A)))) # Eq. (21)

# Eq. (15)
v_hat <- D %*% v_net # Eq. (22)
# Or
v_hat <- D %*% A_tilde %*% v + D %*% v

No competing interests declared.