generate two sets of random numbers that are correlated

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

generate two sets of random numbers that are correlated

Kathie
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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate two sets of random numbers that are correlated

Michael Weylandt
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate two sets of random numbers that are correlated

Ista Zahn-2
In reply to this post by Kathie
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate two sets of random numbers that are correlated

Kathie
In reply to this post by Michael Weylandt
almost forgot. In fact, I want to generate correlated Poisson random vectors. Thank you anyway
Reply | Threaded
Open this post in threaded view
|

Re: generate two sets of random numbers that are correlated

Duncan Murdoch-2
On 11/08/2011 12:01 PM, Kathie wrote:
> almost forgot. In fact, I want to generate correlated Poisson random vectors.

Saying you want two random variables to be correlated doesn't specify
the joint distribution, so there will be a lot of solutions.  Here's
one, for the case where both variables have the same mean mu, and you
want a positive correlation.

We know that the sum of independent Poissons is Poisson, so we'll
generate 3 variables: X with mean nu, and Y & Z with mean mu-nu, and return
A = X+Y and B = X+Z.  If nu=0 then A and B are independent, and if
nu=mu, they have correlation 1, so you must be able to solve for a value
where they have any desired correlation in between.

If the means aren't the same, this method will still work up to a point,
but you won't be able to get really high correlations.

If you want negative correlations it's harder, but you could use the
following trick:  Generate U ~ Unif(0, 1).  Calculate A by the inverse
CDF method from U.  Compute V to be equal to U if U < a or U > 1-a, and
equal to 1-U otherwise.  Calculate B by the inverse CDF method on V.

Then both U and V will have Poisson distributions (and you can choose
the means as you like), and there will be some range of achievable
correlations which will be quite close to [-1, 1].  The joint
distribution will be very weird, but you didn't say that was a problem...

Some R code:

U <- runif(10000)
A <- qpois(U, 5)
a <- 0.115
V <- ifelse(U < a | U > 1-a, U, 1-U)
B <- qpois(V, 5)
cor(A, B)

This gives a correlation around 0.4.


Duncan Murdoch

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate two sets of random numbers that are correlated

jones-2
In reply to this post by Kathie
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.
>
> ______________________________________________
> [hidden email] 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
[hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate two sets of random numbers that are correlated

Duncan Murdoch-2
In reply to this post by Duncan Murdoch-2
On 11/08/2011 1:33 PM, Duncan Murdoch wrote:

> On 11/08/2011 12:01 PM, Kathie wrote:
> >  almost forgot. In fact, I want to generate correlated Poisson random vectors.
>
> Saying you want two random variables to be correlated doesn't specify
> the joint distribution, so there will be a lot of solutions.  Here's
> one, for the case where both variables have the same mean mu, and you
> want a positive correlation.
>
> We know that the sum of independent Poissons is Poisson, so we'll
> generate 3 variables: X with mean nu, and Y&  Z with mean mu-nu, and return
> A = X+Y and B = X+Z.  If nu=0 then A and B are independent, and if
> nu=mu, they have correlation 1, so you must be able to solve for a value
> where they have any desired correlation in between.
>
> If the means aren't the same, this method will still work up to a point,
> but you won't be able to get really high correlations.
>
> If you want negative correlations it's harder, but you could use the
> following trick:  Generate U ~ Unif(0, 1).  Calculate A by the inverse
> CDF method from U.  Compute V to be equal to U if U<  a or U>  1-a, and
> equal to 1-U otherwise.  Calculate B by the inverse CDF method on V.
>
> Then both U and V will have Poisson distributions (and you can choose

I meant A and B in the line above...

> the means as you like), and there will be some range of achievable
> correlations which will be quite close to [-1, 1].  The joint
> distribution will be very weird, but you didn't say that was a problem...
>
> Some R code:
>
> U<- runif(10000)
> A<- qpois(U, 5)
> a<- 0.115
> V<- ifelse(U<  a | U>  1-a, U, 1-U)
> B<- qpois(V, 5)
> cor(A, B)
>
> This gives a correlation around 0.4.
>
>
> Duncan Murdoch

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: generate two sets of random numbers that are correlated

Michael Dewey
In reply to this post by Kathie
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
[hidden email]
http://www.aghmed.fsnet.co.uk/home.html

______________________________________________
[hidden email] 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.