[R] Generating correlated data from uniform distribution

Spencer Graves spencer.graves at pdf.com
Sat Jul 2 00:26:02 CEST 2005


	  How about tetrachoric correlations?  Generate correlated normal 
observations, then convert to uniform using pnorm:

rho <- 0.9
Cor <- array(c(1, rho, rho, 1), dim=c(2,2))

library(mvtnorm)

set.seed(1)
Y <- rmvnorm(10000, sigma=Cor)

X <- pnorm(Y)-0.5
plot(X)
hist(X[,1])
hist(X[,2])
cor(X)

	  Enjoy.
	  spencer graves

Tony Plate wrote:

> 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
>>
> 
> 
> ______________________________________________
> 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

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915




More information about the R-help mailing list