[R] Generating MVN Data

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Feb 13 15:54:07 CET 2007


'should work', yes.
Do what he asked for (in any reasonable reading), no.

> set.seed(1)
> library(mvtnorm)   ## you both omitted to mention that
> X <-  rmvnorm(n=10,mean=1:2,sigma=matrix(c(1,0.5,0.5,1),2,2))
> var(X)
           [,1]      [,2]
[1,] 0.4878773 0.1238040
[2,] 0.1238040 0.9508090

Fortunately there is a way to do it, and without even adding unstated 
packages to your R installation:

> library(MASS)
> set.seed(1)
> X <-  mvrnorm(n=10, mu=1:2, Sigma=matrix(c(1,0.5,0.5,1),2,2), emp=TRUE)
> var(X)
      [,1] [,2]
[1,]  1.0  0.5
[2,]  0.5  1.0


On Tue, 13 Feb 2007, Robin Hankin wrote:

> Hi
>
> give rmvnorm() any symmetric positive definite matrix and it should
> work:
>
>
>
> > rmvnorm(n=10,mean=1:2,sigma=matrix(c(1,0.5,0.5,1),2,2))
>          [,1]   [,2]
> [1,] -0.1118  2.514
> [2,]  1.8667  1.628
> [3,]  3.2477  2.263
> [4,]  1.0166  2.381
> [5,] -0.0888 -0.132
> [6,] -0.9249  0.610
> [7,]  1.5046  3.578
> [8,]  0.8530  0.802
> [9,]  2.2940  2.240
> [10,]  1.1660  2.528
> >
>
>
>
> HTH
>
> rksh
>
>
> On 13 Feb 2007, at 14:14, 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
>>
>> 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.
>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
>  tel  023-8059-7743
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list