On Sat, Feb 19, 2011 at 6:17 PM, Søren Faurby
<soren.fau...@biology.au.dk> 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

Hi Soren,

I'm not aware of such functions, but you can try the following code:

# generate some x
n = 100
x = runif(n)
r = 0.5;

y = r * scale(x) + sqrt(1-r^2) * scale(runif(n));

cor(x,y)

The result is not exactly 0.5 because cor(x, runif(n)) is not exactly
zero, but on average you get 0.5 (try to run it 1000 times and
calculate the mean and standard error of the obtained values).

Note that the correlation will be on average r no matter what the
distribution of x is, but the distribution of y will be uniform only
if the distribution of x is uniform.

Peter

>
> Kind regards, Soren
>
> ______________________________________________
> R-help@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.
>

______________________________________________
R-help@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.

Reply via email to