[R] Generating correlated data from uniform distribution
Jim Brennan
jfbrennan at rogers.com
Fri Jul 1 23:46:25 CEST 2005
> 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 at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Menghui Chen
Sent: July 1, 2005 4:49 PM
To: r-help at 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 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
More information about the R-help
mailing list