generate random numbers from a multivariate distribution with specified correlation matrix

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

generate random numbers from a multivariate distribution with specified correlation matrix

rusers.sh
Hi all,
  rmvnorm()can be used to generate the random numbers from a multivariate
normal distribution with specified means and covariance matrix, but i want
to specify the correlation matrix instead of covariance matrix for the
multivariate
normal distribution.
Does anybody know how to generate the random numbers from a multivariate
normal distribution with specified correlation matrix? What about
other non-normal
distribution?
Thanks a lot.

--
-----------------
Jane Chang
Queen's

        [[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 random numbers from a multivariate distribution with specified correlation matrix

bbolker
rusers.sh <rusers.sh <at> gmail.com> writes:

>   rmvnorm()can be used to generate the random numbers from a multivariate
> normal distribution with specified means and covariance matrix, but i want
> to specify the correlation matrix instead of covariance matrix for the
> multivariate
> normal distribution.
> Does anybody know how to generate the random numbers from a multivariate
> normal distribution with specified correlation matrix? What about
> other non-normal
> distribution?

  What do you want the variances to be?  If you don't mind that they're
all equal to 1, then using your correlation matrix as the Sigma argument
to the mvrnorm() [sic] function in MASS should work fine.  They have to
be defined as *something* ....
  If you want multivariate distributions with non-normal marginal
distributions, consider the 'copula' package, but be prepared to do
some reading -- this is a fairly big/deep topic.

  good luck.

______________________________________________
[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 random numbers from a multivariate distribution with specified correlation matrix

rusers.sh
Hi,
  If you see the link http://www.stata.com/help.cgi?drawnorm, and you can
see an example,
#draw a sample of 1000 observations from a bivariate standard
normal distribution, with correlation 0.5.
#drawnorm x y, n(1000) corr(0.5)
 This is what Stata software did. What i hope to do in R should be similar
as that.
  It will be better to only need us to specify the correlation matrix, mean
values and possible variances. One of my aim is to simulate random fields.
  Thanks.


2010/8/23 Ben Bolker <[hidden email]>

> rusers.sh <rusers.sh <at> gmail.com> writes:
>
> >   rmvnorm()can be used to generate the random numbers from a multivariate
> > normal distribution with specified means and covariance matrix, but i
> want
> > to specify the correlation matrix instead of covariance matrix for the
> > multivariate
> > normal distribution.
> > Does anybody know how to generate the random numbers from a multivariate
> > normal distribution with specified correlation matrix? What about
> > other non-normal
> > distribution?
>
>   What do you want the variances to be?  If you don't mind that they're
> all equal to 1, then using your correlation matrix as the Sigma argument
> to the mvrnorm() [sic] function in MASS should work fine.  They have to
> be defined as *something* ....
>  If you want multivariate distributions with non-normal marginal
> distributions, consider the 'copula' package, but be prepared to do
> some reading -- this is a fairly big/deep topic.
>
>  good luck.
>
> ______________________________________________
> [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.
>



--
-----------------
Jane Chang
Queen's

        [[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 random numbers from a multivariate distribution with specified correlation matrix

David Winsemius

On Aug 23, 2010, at 11:05 PM, rusers.sh wrote:

> Hi,
>  If you see the link http://www.stata.com/help.cgi?drawnorm, and you  
> can
> see an example,
> #draw a sample of 1000 observations from a bivariate standard
> normal distribution, with correlation 0.5.
> #drawnorm x y, n(1000) corr(0.5)
> This is what Stata software did. What i hope to do in R should be  
> similar
> as that.
>  It will be better to only need us to specify the correlation  
> matrix, mean
> values and possible variances. One of my aim is to simulate random  
> fields.
>  Thanks.

?cov2cor

--
David.

>
>
> 2010/8/23 Ben Bolker <[hidden email]>
>
>> rusers.sh <rusers.sh <at> gmail.com> writes:
>>
>>>  rmvnorm()can be used to generate the random numbers from a  
>>> multivariate
>>> normal distribution with specified means and covariance matrix,  
>>> but i
>> want
>>> to specify the correlation matrix instead of covariance matrix for  
>>> the
>>> multivariate
>>> normal distribution.
>>> Does anybody know how to generate the random numbers from a  
>>> multivariate
>>> normal distribution with specified correlation matrix? What about
>>> other non-normal
>>> distribution?
>>
>>  What do you want the variances to be?  If you don't mind that  
>> they're
>> all equal to 1, then using your correlation matrix as the Sigma  
>> argument
>> to the mvrnorm() [sic] function in MASS should work fine.  They  
>> have to
>> be defined as *something* ....
>> If you want multivariate distributions with non-normal marginal
>> distributions, consider the 'copula' package, but be prepared to do
>> some reading -- this is a fairly big/deep topic.
>>
>> good luck.
>>
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[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 random numbers from a multivariate distribution with specified correlation matrix

djmuseR
In reply to this post by rusers.sh
Hi Jane:

On Mon, Aug 23, 2010 at 8:05 PM, rusers.sh <[hidden email]> wrote:

> Hi,
>   If you see the link http://www.stata.com/help.cgi?drawnorm, and you can
> see an example,
> #draw a sample of 1000 observations from a bivariate standard
> normal distribution, with correlation 0.5.
> #drawnorm x y, n(1000) corr(0.5)
>  This is what Stata software did. What i hope to do in R should be similar
> as that.
>

Using an example adapted from package mvtnorm:

library(mvtnorm)
sigma <- matrix(c(1, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 1), ncol = 3)
sigma
     [,1] [,2] [,3]
[1,]  1.0  0.5  0.5
[2,]  0.5  1.0  0.5
[3,]  0.5  0.5  1.0
x <- rmvnorm(n = 1000, mean = c(1, 5, 10), sigma = sigma)
head(x, 2)
           [,1]     [,2]      [,3]
[1,]  1.1830181 6.730525 10.687912
[2,]  2.2911587 5.978146  9.493432

cov(x)
          [,1]      [,2]      [,3]
[1,] 0.9725893 0.4894247 0.4902096
[2,] 0.4894247 0.9782143 0.4572949
[3,] 0.4902096 0.4572949 0.9656340
colMeans(x)
[1]  0.9901327  5.0008999 10.0162695

# Same example using mvrnorm() from MASS:

library(MASS)
x2 <- mvrnorm(n = 1000, mu = c(1, 5, 10), Sigma = sigma)
head(x2, 2)
           [,1]     [,2]      [,3]
[1,] -0.1559149 3.449327  7.967966
[2,] -0.7961951 4.636752  8.580032
cov(x2)
          [,1]      [,2]      [,3]
[1,] 1.0786150 0.4719868 0.5082440
[2,] 0.4719868 0.9608204 0.4819515
[3,] 0.5082440 0.4819515 1.0264072
colMeans(x2)
[1]  1.042077  5.011792 10.025397

Package mvtnorm also has a function to obtain samples from multivariate-t
distributions (rmvt). See the help pages of these functions for examples and
further details.

For simulating random fields, there are two packages of which I'm aware:
RandomFields and FieldSim. It might also be worth checking out the Spatial
Task View @ CRAN to see if anything else is available to help you.

HTH,
Dennis

  It will be better to only need us to specify the correlation matrix, mean

> values and possible variances. One of my aim is to simulate random fields.
>   Thanks.
>
>
> 2010/8/23 Ben Bolker <[hidden email]>
>
> rusers.sh <rusers.sh <at> gmail.com> writes:
>>
>> >   rmvnorm()can be used to generate the random numbers from a
>> multivariate
>> > normal distribution with specified means and covariance matrix, but i
>> want
>> > to specify the correlation matrix instead of covariance matrix for the
>> > multivariate
>> > normal distribution.
>> > Does anybody know how to generate the random numbers from a multivariate
>> > normal distribution with specified correlation matrix? What about
>> > other non-normal
>> > distribution?
>>
>>   What do you want the variances to be?  If you don't mind that they're
>> all equal to 1, then using your correlation matrix as the Sigma argument
>> to the mvrnorm() [sic] function in MASS should work fine.  They have to
>> be defined as *something* ....
>>  If you want multivariate distributions with non-normal marginal
>> distributions, consider the 'copula' package, but be prepared to do
>> some reading -- this is a fairly big/deep topic.
>>
>>  good luck.
>>
>> ______________________________________________
>> [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.
>>
>
>
>
> --
> -----------------
> Jane Chang
> Queen's
>

        [[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 random numbers from a multivariate distribution with specified correlation matrix

rusers.sh
In reply to this post by rusers.sh
Great. It is more clearer for me. Thanks all.

2010/8/24 Michael Dewey <[hidden email]>

> At 02:40 24/08/2010, rusers.sh wrote:
>
>> Hi all,
>>  rmvnorm()can be used to generate the random numbers from a multivariate
>> normal distribution with specified means and covariance matrix, but i want
>> to specify the correlation matrix instead of covariance matrix for the
>> multivariate
>> normal distribution.
>>
>
> Jane, perhaps I misunderstand you but the correlation matrix is a
> covariance matrix
>
>
>  Does anybody know how to generate the random numbers from a multivariate
>> normal distribution with specified correlation matrix? What about
>> other non-normal
>> distribution?
>>
>
> There is corcounts for correlated count variables and a couple of packages
> for correlated binary variables, search the packages list for correlated for
> details. I have not used any of these so can offer no recommendations.
>
>  Thanks a lot.
>>
>> --
>> -----------------
>> Jane Chang
>> Queen's
>>
>>        [[alternative HTML version deleted]]
>>
>
> Michael Dewey
> [hidden email]
> http://www.aghmed.fsnet.co.uk/home.html
>
>


--
-----------------
Jane Chang
Queen's

        [[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 random numbers from a multivariate distribution with specified correlation matrix

rusers.sh
In reply to this post by rusers.sh
BTW, can you recommend a book on statistical simulations? I want to know
more on how to generate random numbers from distributions, how to generate
the theoretical models,...
Thanks a lot.

2010/8/24 Michael Dewey <[hidden email]>

> At 02:40 24/08/2010, rusers.sh wrote:
>
>> Hi all,
>>  rmvnorm()can be used to generate the random numbers from a multivariate
>> normal distribution with specified means and covariance matrix, but i want
>> to specify the correlation matrix instead of covariance matrix for the
>> multivariate
>> normal distribution.
>>
>
> Jane, perhaps I misunderstand you but the correlation matrix is a
> covariance matrix
>
>
>  Does anybody know how to generate the random numbers from a multivariate
>> normal distribution with specified correlation matrix? What about
>> other non-normal
>> distribution?
>>
>
> There is corcounts for correlated count variables and a couple of packages
> for correlated binary variables, search the packages list for correlated for
> details. I have not used any of these so can offer no recommendations.
>
>  Thanks a lot.
>>
>> --
>> -----------------
>> Jane Chang
>> Queen's
>>
>>        [[alternative HTML version deleted]]
>>
>
> Michael Dewey
> [hidden email]
> http://www.aghmed.fsnet.co.uk/home.html
>
>


--
-----------------
Jane Chang
Queen's

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