Even Simpler Multivariate Correlated Simulations

So after yesterday’s post on Simple Simulation using Copulas I got a very nice email that basically begged the question, “Dude, why are you making this so hard?” The author pointed out that if what I really want is a Gaussian correlation structure for Gaussian distributions then I could simply use the mvrnorm() function from the MASS package. Well I did a quick

?mvrnorm

and, I’ll be damned, he’s right! The advantage of using a copula is the ability to simulate correlation structures where the correlation is different for different levels of values. So that gives the flexibility to make the tails of the distributions more correlated, for example. But my example yesterday was purposefully simple… so simple that a copula was not even needed.

After creating my sample data all I really needed to do was this:

myDraws <- mvrnorm(1e5, mu=mean(ourData), Sigma=cov(ourData))

So I  took my example from yesterday and updated it using the mvrnorm() code and, as is my custom, put up a Github gist. The code is embedded below as well. I added a little ggplot2 code at the end that will create a facet plot of the 4 distributions showing the shape of the distributions of both the starting data and the simulated data. The plot in the upper left of this post is the ggplot output.

EDIT: The email hipping me to this was sent by Dirk Eddelbuettel who’s been very helpful to me more times than I can count. I had omitted his name initially. However after confirming with Dirk, he told me it was OK to mention him by name in this post.

4 Comments

  1. lee says:

    Not sure how this relates to the mvrnorm function, but I’ve been using the mvtnorm package for years in applying gaussion and T copulas. It has all they standard r,d,q,p functions for both multivariate normal and T distributions. I’ve found it a pretty good time saver.

  2. JD Long says:

    Lee, I had not used the mvtnorm package before but just pulled the vignette and it looks really interesting. Thanks for pointing that out!

  3. lee says:

    The function names are a bit confusing since the ‘mvrnorm’ you pointed out is in the MASS library, while ‘rmvnorm’ is in the ‘mvtnorm’ library!

  4. efrique says:

    begged the question

    I expect you mean “asked the question”. Begging the question is a logical fallacy where the proposition is assumed in the premise.

    http://en.wikipedia.org/wiki/Begging_the_question

Leave a Reply