[R] Generating correlated data from uniform distribution
Jim Brennan
jfbrennan at rogers.com
Sat Jul 2 00:32:59 CEST 2005
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 at acm.org]
Sent: July 1, 2005 6:01 PM
To: Jim Brennan
Cc: 'Menghui Chen'; r-help at 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
>
-- Tony Plate
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 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
>
> ______________________________________________
> 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