Re: [R] Generating correlated data from uniform distribution

From: Jim Brennan <jfbrennan_at_rogers.com>
Date: Sat 02 Jul 2005 - 08:32:59 EST


Yes you are right I guess this works only for normal data. Free advice sometimes comes with too little consideration :-) Sorry about that and thanks to Spencer for the correct way. -----Original Message-----
From: Tony Plate [mailto:tplate@acm.org] Sent: July 1, 2005 6:01 PM
To: Jim Brennan
Cc: 'Menghui Chen'; r-help@stat.math.ethz.ch Subject: Re: [R] Generating correlated data from uniform distribution

Isn't this a little trickier with non-normal variables? It sounds like Menghui Chen wants variables that have uniform marginal distribution, and a specified correlation.

When I look at histograms (or just the quantiles) of the rows of dat2 in your example, I see something for dat2[2,] that does not look much like it comes from a uniform distribution.

 > dat<-matrix(runif(2000),2,1000)
 > rho<-.77
 > R<-matrix(c(1,rho,rho,1),2,2)
 > ch<-chol(R)
 > dat2<-t(ch)%*%dat
 > cor(dat2[1,],dat2[2,])

[1] 0.7513892
 > hist(dat2[1,])
 > hist(dat2[2,])
 >
 > quantile(dat2[1,])
          0%         25%         50%         75%        100%
0.000655829 0.246216035 0.507075912 0.745158441 0.999916418  > quantile(dat2[2,])

        0% 25% 50% 75% 100% 0.0393046 0.4980066 0.7150426 0.9208855 1.3864704  >

Jim Brennan wrote:
> dat<-matrix(runif(2000),2,1000)
> rho<-.77
> R<-matrix(c(1,rho,rho,1),2,2)
> ch<-chol(R)
> 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
>
https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help@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 >

R-help@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 Received on Sat Jul 02 08:35:39 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:11 EST