[R] help about random generation of a Normal distribution of several variables

Gavin Simpson gavin.simpson at ucl.ac.uk
Sat Jul 5 18:43:46 CEST 2008


On Sat, 2008-07-05 at 18:21 +0200, Arnau Mir wrote:
> Hello.
> 
> Somebody knows how can I generate a set of n random vectors  of a normal
> distribution of several variables?
> For example, I want to generate n=100 random vectors of two dimensions for
> a normal with mean c(0,1)  and  variance matrix:  matrix(c(2,1,1,3),2,2).

One is mvrnorm() in the MASS package, part of the VR bundle that comes
with R.

> require(MASS)
> mu <- c(0,1)
> Sigma <- matrix(c(2,1,1,3),2,2)
> res <- mvrnorm(100, mu = mu, Sigma = Sigma)
> head(res)
           [,1]        [,2]
[1,]  2.7582876  1.04208798
[2,]  0.6364184 -0.08043244
[3,] -1.8897731  0.04051395
[4,]  2.6699881  0.83163661
[5,] -1.1942385 -1.17503716
[6,] -0.4303459 -0.80880649

HTH

G



More information about the R-help mailing list