Hi list:

Here is one approach that will generate two uniformly distributed 
variables with a correlation of  0.5 --

 >   N <- 1000
 >   x <- sample(1:9,N,replace=TRUE)
 >   uni <- runif(N)
 >   y <- (uni<0.5)*x+(uni>0.5)*sort(x)
 >   (y.freq <- table(y) )
y
   1   2   3   4   5   6   7   8   9
123 114 113 105 126 102 110  95 112
 >   chisq.test(y.freq)

         Chi-squared test for given probabilities

data:  y.freq
X-squared = 6.812, df = 8, p-value = 0.557

 >   cor(x,y)
[1] 0.5418051

It seems obvious that the correlation can be adjusted by changing 0.5 to 
r and 1-r, respectively in the assigment --

      y <- (uni<0.5)*x+(uni>0.5)*sort(x)

It is worth reflecting about whether this algorithm reflects the 
real-world process you wish to simulate.

Larry Hotchkiss


On 2/21/2011 6:00 AM, r-help-requ...@r-project.org wrote:
> Generating uniformly distributed correlated data.

Message: 1
Date: Sun, 20 Feb 2011 12:36:43 +0100
From: "Enrico Schumann"<enricoschum...@yahoo.de>
To: "=?iso-8859-1?Q?'S=C3=B8ren_Faurby'?="
        <soren.fau...@biology.au.dk>
Cc:r-help@r-project.org
Subject: Re: [R] Generating uniformly distributed correlated data.
Message-ID:<7C892818271C43E08DCFD8277CBD01CB@EnricosPC>
Content-Type: text/plain;       charset="iso-8859-1"


maybe this helps
http://comisef.wikidot.com/tutorial:correlateduniformvariates

regards
enrico


> >  -----Urspr?ngliche Nachricht-----
> >  Von:r-help-boun...@r-project.org
> >  [mailto:r-help-boun...@r-project.org] Im Auftrag von S??ren Faurby
> >  Gesendet: Sonntag, 20. Februar 2011 03:18
> >  An:r-help@r-project.org
> >  Betreff: [R] Generating uniformly distributed correlated data.
> >  
> >  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(105)
> >  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@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.

------------------------------


        [[alternative HTML version deleted]]

______________________________________________
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