[R] Generating correlated data from uniform distribution

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Jul 2 10:52:33 CEST 2005

"Jim Brennan" <jfbrennan at rogers.com> writes:

> OK now I am skeptical especially when you say in a weird way:-)
> This may be OK but look at plot(x,y) and I am suspicious. Is it still
> alright with this kind of relationship?
> N <- 10000
> rho <- .6
> x <- runif(N, -.5,.5)
> y <- x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))

Well, the covariance is (everything has mean zero, of course)

E(XY) = (1+rho)/2*EX^2 + (1-rho)/2*E(X*-X) = rho*EX^2 

The marginal distribution of Y is a mixture of two identical uniforms
(X and -X) so is uniform and in particular has the same variance as X.

In summary,  EXY/sqrt(EX^2EY^2) == rho

So as I said, it satisfies the formal requirements. X and Y are
uniformly distributed and their correlation is rho. 

If for nothing else, I suppose that this example is good for
demonstrating that independence and uncorrelatedness is not the same

   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907

More information about the R-help mailing list