[R] Generating MVN Data

Chuck Cleland ccleland at optonline.net
Tue Feb 13 15:35:40 CET 2007


Rauf Ahmad wrote:
> Dear All
> 
> I want to generate multivariate normal data in R for a given covariance 
> matrix, i.e. my generated data must have the given covariance matrix. I 
> know the rmvnorm command is to be used but may be I am failing to 
> properly assign the covariance matrix.
> 
> Any help will be greatly appreciated

library(MASS)

mat <- mvrnorm(100, mu=rep(0,4), Sigma = diag(4), empirical=TRUE)

cov(mat)
              [,1]          [,2]          [,3]          [,4]
[1,]  1.000000e+00 -1.634448e-16 -1.004223e-16 -2.015521e-16
[2,] -1.634448e-16  1.000000e+00  4.244391e-17  1.544399e-17
[3,] -1.004223e-16  4.244391e-17  1.000000e+00 -1.921951e-16
[4,] -2.015521e-16  1.544399e-17 -1.921951e-16  1.000000e+00

?mvrnorm

> thanks.
> 
> M. R. Ahmad
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list