[R] Multivariate simulation

Chuck Cleland ccleland at optonline.net
Fri May 9 13:28:46 CEST 2008


On 5/9/2008 6:43 AM, Yemi Oyeyemi wrote:
> Dear everyone, I am having problem simulating multivariate data. Though I was able to simulate the data, but finding the variance-covariance matrix of simulated data did not give exact covariance matrix used in simulating the data. Unlike some other packages, like stata, using command "corr2data" will simulate data having the covariance matrix exactly with the specified covariance matrix.
>   I used "mvrnorm(n, means, covariance)" Is there any syntax in R to use to give me what I want?
>   Thanks

   See the "empirical" argument, which is FALSE by default:

library(MASS)

var(mvrnorm(100, mu=rep(0,3), Sigma=diag(3)))
            [,1]        [,2]        [,3]
[1,]  1.0973749  0.15329337 -0.25827350
[2,]  0.1532934  1.21175520 -0.01226041
[3,] -0.2582735 -0.01226041  1.03644978

var(mvrnorm(100, mu=rep(0,3), Sigma=diag(3), empirical=TRUE))
               [,1]          [,2]          [,3]
[1,]  1.000000e+00 -6.848835e-18 -3.800031e-17
[2,] -6.848835e-18  1.000000e+00 -5.833391e-17
[3,] -3.800031e-17 -5.833391e-17  1.000000e+00

> ---------------------------------
> [[elided Yahoo spam]]
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org 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. (www.ndri.org)
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