# Re: [R] Generating correlated data from uniform distribution

From: Jim Brennan <jfbrennan_at_rogers.com>
Date: Sat 02 Jul 2005 - 07:46:25 EST

> dat<-matrix(runif(2000),2,1000)
> rho<-.77
> R<-matrix(c(1,rho,rho,1),2,2)
> dat2<-t(ch)%*%dat
> cor(dat2[1,],dat2[2,])
[1] 0.7513892
> dat<-matrix(runif(20000),2,10000)
> rho<-.28
> R<-matrix(c(1,rho,rho,1),2,2)
> ch<-chol(R)
> dat2<-t(ch)%*%dat
> cor(dat2[1,],dat2[2,])
[1] 0.2681669
> dat<-matrix(runif(200000),2,100000)
> rho<-.28
> R<-matrix(c(1,rho,rho,1),2,2)
> ch<-chol(R)
> dat2<-t(ch)%*%dat
> cor(dat2[1,],dat2[2,])
[1] 0.2814035
>

See ?choleski

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Menghui Chen Sent: July 1, 2005 4:49 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Generating correlated data from uniform distribution

Dear R users,

I want to generate two random variables (X1, X2) from uniform distribution (-0.5, 0.5) with a specified correlation coefficient r. Does anyone know how to do it in R?

Many thanks!

Menghui

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