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. Michael Weylandt
2011-Aug-11 15:23 UTC
[R] generate two sets of random numbers that are correlated
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.lord2000@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]]
Ista Zahn
2011-Aug-11 16:01 UTC
[R] generate two sets of random numbers that are correlated
If you want random numbers sampled from the normal distribution, you can use the mvrnorm function in MASS. Best, Ista On Thu, Aug 11, 2011 at 8:23 AM, Kathie <kathryn.lord2000 at 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 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
almost forgot. In fact, I want to generate correlated Poisson random vectors. Thank you anyway -- View this message in context: http://r.789695.n4.nabble.com/generate-two-sets-of-random-numbers-that-are-correlated-tp3736161p3736287.html Sent from the R help mailing list archive at Nabble.com.
Albyn Jones
2011-Aug-11 17:38 UTC
[R] generate two sets of random numbers that are correlated
Here is a simple method z1 <- rpois(n,mu1) z2 <- rpois(n,mu1) z3 <- rpois(n,mu2) Y <- z1 + z3 X <- z2 + z3 Cov(X,Y) = mu2, so Cor(X,Y) = mu2/(mu1+mu2) albyn On Thu, Aug 11, 2011 at 09:01:50AM -0700, Kathie wrote:> almost forgot. In fact, I want to generate correlated Poisson random vectors. > Thank you anyway > > -- > View this message in context: http://r.789695.n4.nabble.com/generate-two-sets-of-random-numbers-that-are-correlated-tp3736161p3736287.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Albyn Jones Reed College jones at reed.edu
Michael Dewey
2011-Aug-14 13:53 UTC
[R] generate two sets of random numbers that are correlated
At 17:01 11/08/2011, Kathie wrote:>almost forgot. In fact, I want to generate correlated Poisson random vectors.Kathie Typing generate random poisson into the R Site Search gives a number of hits. Does any of them do what you require?>Thank you anyway > >-- >View this message in context: >http://r.789695.n4.nabble.com/generate-two-sets-of-random-numbers-that-are-correlated-tp3736161p3736287.html >Sent from the R help mailing list archive at Nabble.com.Michael Dewey info at aghmed.fsnet.co.uk http://www.aghmed.fsnet.co.uk/home.html