I have been doing some research about coexpression network. “coexpression” means that genes have similar expression profiles across different conditions or tissues. In the network, genes are nodes, and “coexpression” relationship between two genes can be reprensented as edges. The coexpressed genes may involve in similar pathways or biological process.
In a small part of my research, I am testing some algorithms to detect coexpression relationship. One way to test algorithm is simulation. In an ideal (simple) case, the expression values of two coexpressed genes can be considered as bivariate normal distributed. To generate expression values of such gene pair or a group of genes given a correlation coefficient, is just to simulate multivariate normal distribution. MASS library in R has an function, mvrnorm, to do that, but it requires a covariance matrix.
The function below is to firstly generate the covariance matrix in order to use the mvnorm function. Because we only know the correlation coefficient, i.e. coexpression relationship (degree), the mean and variance of each gene’s expression profile are random generated in the function. Then the matrix can be calulated as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
