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
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 appreciatedlibrary(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
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
you probably want to use mvrnorm() from package MASS, e.g.,
library(MASS)
mu <- c(-3, 0, 3)
Sigma <- rbind(c(5,3,2), c(3,4,1), c(2,1,3))
x <- mvrnorm(1000, mu, Sigma, empirical = TRUE)
colMeans(x)
var(x)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Rauf Ahmad" <rahmad at gwdg.de>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, February 13, 2007 3:14 PM
Subject: [R] Generating MVN Data
> 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Dear All many thanks for your kind help and quick response. I actually used the same code (but as rmvnorm in library mvtnorm) to generate data. But I was not sure since I thought I might have to do some decomposition of the matrix and then generate the data. But now it is confirmed. with many thanks again and regards M. R. Ahmad 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
Reasonably Related Threads
- test for whether dataset comes from a known MVN
- Conditional Distribution of MVN variates
- Simulating from a Multivariate Normal Distribution Using a Correlation Matrix
- Error using function MVN in package MCLUST: Fortran symbol name not in DLL for package
- Truncated MVN RNG?