How to generate correlated random numbers
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.