How to generate correlated random numbers

September 1, 2010

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

We've covered how to generate random numbers in R before, but what if you want to go beyond generating one random number at a time? What if you want to generate two, or three or more random numbers, and what's more, you want them to be correlated?

JD Long lays out the way in a couple of posts at his Cerebral Mastication blog. If you want to generate bivariate (or trivariate, or more) random multivariate Normal variates, it's pretty easy, as JD points out. Just use the mvrnorm function from the MASS package in R, specify the covariance matrix, and you're all set.

But what if the random variables need to follow some other marginal distribution than Normal? This is a common task for simulations, where you may know the distributions you want for each variable, but need a way to specify the covariance structure. This is where copulas come in handy, as explained in JD's second post, complete with R code implementing a simulation. 

Cerebral Mastication: Even Simpler Multivariate Correlated Simulations

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)