[R] Generating uniformly distributed correlated data.

Erich Neuwirth erich.neuwirth at univie.ac.at
Mon Feb 21 13:03:53 CET 2011


hw<-function(r){
	(3-sqrt(1+8*r))/4
}


x<-runif(1000)
y<-(x+runif(1000,-hw(0.5),hw(0.5))) %% 1


x and y will have correlation 0.5 and will be uniformly distributed
on the unit interval.
Replacing 0.5 by any nonnegative number r between 0 and 1 will
create correlated uniformly distributed random numbers with correlation r.

plot(x,y) will show the construction of the joint distribution of these
random numbers.
The rest is simple algebra.






On 2/20/2011 3:17 AM, Søren Faurby wrote:
> I wish to generate a vector of uniformly distributed data with a defined
> correlation to another vector
> 
> The only function I have been able to find doing something similar is
> corgen from the library ecodist.
> 
> The following code generates data with the desired correlation to the
> vector x but the resulting vector y is normal and not uniform distributed
> 
> library(ecodist)
> x <- runif(10^5)
> y <- corgen(x=x, r=.5)$y
> 
> Do anyone know a similar function generating uniform distributed data or
> a way of transforming y to the desired distribution while keeping the
> correlation between x and y
> 
> Kind regards, Soren
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list