Well, without saying what sort of random numbers you mean, it's a little
hard, but here's one straightforward way to do it for 2 normally distributed
rvs.

X1 = rnorm(100)
X2 = rnorm(100)

Y = X1
Z = 0.4*X1+sqrt(1-0.4)*X2

then

cor(Y,Z) ~ 0.4

In function terms:

CorrNorm <- function(n=100, rho = 0.4) {
     X1 = rnorm(n); X2 = rnorm(n)
     Z = cbind(X1, rho*X1+sqrt(1-rho^2)*X2)
     return(Z)
}

Hope this helps,

Michael Weylandt

PS -- Depending on what you may want/need/intend check out the rmnorm()
function for a multivariate random normal.

On Thu, Aug 11, 2011 at 8:23 AM, Kathie <kathryn.lord2...@gmail.com> wrote:

> Dear R users
>
> I'd like to generate two sets of random numbers with a fixed correlation
> coefficient, say .4, using R.
>
> Any suggestion will be greatly appreciated.
>
> Regards,
>
> Kathryn Lord
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/generate-two-sets-of-random-numbers-that-are-correlated-tp3735695p3735695.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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