# Re: [R] generate bi-variate normal data

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Thu 06 Jul 2006 - 03:53:12 EST

If (X, Y) ~ N(mx, my, sx^2, sy^2, r) (r being the correlation, not covariance), then

Y|X ~ N(my + r * (sy / sx) * (X - mx), sy^2 * (1 - r^2))

You can use this to work out what you need to do.

Best,
Andy

```	From: Shin, David [mailto:david.shin@pearson.com]
Sent: Wednesday, July 05, 2006 1:14 PM
To: Liaw, Andy; 'r-help@stat.math.ethz.ch'
Subject: RE: [R] generate bi-variate normal data [Broadcast]

Thanks for Andy's comment and help. I should have used a better
```
example for my question. Below is my exact question and I will appreciate a lot for any insights.

I generate a bi-variate normal distribution with mean = c(0, 0.2) and variance covariance matrix = matrix(c(1, .025, .025, .0025), nrow = 2):

> x <- rmvnorm(10, c(0, 0.2), matrix(c(1, .025, .025, .0025), nrow =
2))

> x

[,1] [,2]

[1,] 0.1595351 0.1715898

[2,] -0.5177577 0.1839222

[3,] -0.8794011 0.1896593

[4,] 1.0584185 0.2208470

[5,] 0.1960055 0.2199169

[6,] 0.6450406 0.1773001

[7,] -2.2160986 0.1810803

[8,] 0.2131569 0.1223121

[9,] -0.3598349 0.2402232

[10,] -0.3905455 0.1787059

> x.sum

 0.3311249 -0.3338355 -0.6897418 1.2792655 0.4159225 0.8223407 -2.0350183 0.3354690 -0.1196116 -0.2118395

if I call x[,1] as theta.year1 and x[,2] as growth.year1 then the mean of x.sum is 0+0.2 = -.2 and the standard deviation of x.sum is sqrt(1+.0025+2*.5*1*.05) = 1.0259

<<...OLE_Obj...>>

assume the correlation is again 0.5, I would like to generate another bi-variate normal distribution with the fixed first column that equals to x.sum. If the mean and SD of the second column is 0.2 and 0.05, respectively, the mean of this bi-variate normal distribution is c(0.2, 0.2) and the variance-covariance matrix is: matrix(c(1.0259^2, 0.5*1.0259*0.05, 0.5*1.0259*0.05, 0.0025), 2)

Thanks again for helping me.

David

```		-----Original Message-----
From: Liaw, Andy [mailto:andy_liaw@merck.com]
Sent: Wednesday, July 05, 2006 11:50 AM
To: Shin, David; 'r-help@stat.math.ethz.ch'
Subject: RE: [R] generate bi-variate normal data

From: Shin, David

>

> Dear all,

>

> I would like to generate bi-variate normal data given that

> the first column of the data is known. for example:

> I first generate a set of data using the command, x <-

> rmvnorm(10, c(0, 0), matrix(c(1, 0, 0, 1), 2))

>

> then I would like to sum up the two columns of x:

> x.sum <- apply(x, 1, sum)

>

> now with x.sum I would like to generate another column of

> data, say y, that makes cbind(x.sum, y) follow a bi-variate

> normal distribution with mean = c(0, 0) and sigma =

> matrix(c(1, 0, 0, 1),2)

x.sum as you described would be distributed as normal with mean=0
```
and

variance=2 (so you might as well just use x.sum <- rnorm(10, 0, sqrt(2))),

so I don't see how you can get to the second step where you want x.sum to

have variance=1. Also, since the covariances are 0, you could just generate

the columns separately using rnorm() and cbind() them together.

It might be helpful for you to get some basic understanding of math stat. I

only say that because most likely there are other steps to whatever task you

are doing (people are unlikely to be generating random numbers just for

kicks), and there's no telling what other things you are doing

inefficiently, or even erroneously.

Andy

>

> David s.

>

> **************************************************************

> **************

> This email may contain confidential material.\ If you were

>

> ______________________________________________

> R-help@stat.math.ethz.ch mailing list

>

>

```--

Notice:  This e-mail message, together with any attachments,
contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse
Station, New Jersey, USA 08889), and/or its affiliates (which may be known
outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in
Japan, as Banyu) that may be confidential, proprietary copyrighted and/or
legally privileged. It is intended solely for the use of the individual or
entity named on this message.  If you are not the intended recipient, and
e-mail and then delete it from your system.

----------------------------------------------------------------------------
--

****************************************************************************

This email may contain confidential
material. If you were not an intended recipient,
Please notify the sender and delete all copies.
We may monitor email to and from our network.

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help