Arnau Mir Torres
2008-Jul-05 16:13 UTC
[R] help about random generation of a Normal distribution of several variables
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). Thanks in advance, Arnau.
Arnau Mir
2008-Jul-05 16:21 UTC
[R] help about random generation of a Normal distribution of several variables
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). Thanks in advance, Arnau.
Peng Jiang
2008-Jul-05 16:41 UTC
[R] help about random generation of a Normal distribution of several variables
Hi, Arnau, mvrnorm() in MASS library is what you need. ? mvrnorm to see the detail but first you need to load the MASS library, i.e,library(MASS) regards/ On 2008-7-6, at ??12:21, 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). > > Thanks in advance, > > Arnau. > > ______________________________________________ > 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.----------------------------------------------- Peng Jiang ?? ,Ph.D. Candidate Antai College of Economics & Management ???????? Department of Mathematics ??? Shanghai Jiaotong University (Minhang Campus) 800 Dongchuan Road 200240 Shanghai P. R. China
Gavin Simpson
2008-Jul-05 16:43 UTC
[R] help about random generation of a Normal distribution of several variables
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
Peng Jiang
2008-Jul-06 03:46 UTC
[R] help about random generation of a Normal distribution of several variables
Hi , Arnau
Did you ever check your mailbox? your question was answered last
night Beijing time. :)
Just read the following .
-------------------------
There is no need to load the MASS library, since the code for
mvrnorm therein is compact and self-contained:
mvrnorm <- function (n=1, mu, Sigma, tol=1e-06, empirical=FALSE)
{
p <- length(mu)
if(!all(dim(Sigma) == c(p, p)))
stop("incompatible arguments")
eS <- eigen(Sigma, symmetric = TRUE, EISPACK = TRUE)
ev <- eS$values
if(!all(ev >= -tol * abs(ev[1])))
stop("'Sigma' is not positive definite")
X <- matrix(rnorm(p * n), n)
if(empirical) {
X <- scale(X, TRUE, FALSE)
X <- X %*% svd(X, nu = 0)$v
X <- scale(X, FALSE, TRUE)
}
X <- drop(mu) +
eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X)
nm <- names(mu)
if(is.null(nm) && !is.null(dn <- dimnames(Sigma)))
nm <- dn[[1]]
dimnames(X) <- list(nm, NULL)
if(n == 1)
drop(X)
else t(X)
}
Define that function as above, then proceed along the lines suggested
by Gavin Simpson below.
Ted.
On 05-Jul-08 16:43:46, Gavin Simpson wrote:> 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
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding@manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 05-Jul-08 Time: 18:09:23
------------------------------ XFMail ------------------------------
______________________________________________
R-help@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.
On 2008-7-6, at 上午12:13, Arnau Mir Torres 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).
>
> Thanks in advance,
>
> Arnau.
>
> ______________________________________________
> R-help@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.
[[alternative HTML version deleted]]
Bill.Venables at csiro.au
2008-Jul-06 05:23 UTC
[R] help about random generation of a Normal distribution ofseveral variables
> library(MASS) > set.seed(22134) > Y <- mvrnorm(100, mu = c(0,1), Sigma = matrix(c(2,1,1,3),2,2)) > colMeans(Y)[1] 0.06461359 1.08436419> var(Y)[,1] [,2] [1,] 1.683963 1.300363 [2,] 1.300363 3.280612>Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Arnau Mir Torres Sent: Sunday, 6 July 2008 2:14 AM To: r-help at r-project.org Subject: [R] help about random generation of a Normal distribution ofseveral variables 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). Thanks in advance, Arnau. ______________________________________________ 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.