# Simulating Random Multivariate Correlated Data (Continuous Variables)

**Statistical Research » R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is a repost of an example that I posted last year but at the time I only had the PDF document (written in ). I’m reposting it directly into WordPress and I’m including the graphs.

From time-to-time a researcher needs to develop a script or an application to collect and analyze data. They may also need to test their application under a variety of scenarios prior to data collection. However, because the data has not been collected yet it is necessary to create test data. Creating continuous data is relatively simple and is fairly straight forward using the Cholesky (pronounced kol-eh-ski) decomposition. This approach takes an original X variable (or matrix) and uses the Cholesky transformation to create a new, correlated, Y variable. To make things simple and straight forward this example will generate data from the a random normal distribution N(0,1).

The reason this approach is so useful is that that correlation structure can be specifically defined. The scripts can be used to create many different variables with different correlation structures. The method to transform the data into correlated variables is seen below using the correlation matrix R.

<img src='http://s.wordpress.com/latex.php?latex=R%20%3D%20%5Cleft%28%20%5Cbegin%7Bsmallmatrix%7D%201%260.8%260.2%5C%5C%200.8%261%260.7%5C%5C0.2%260.7%261%20%5Cend%7Bsmallmatrix%7D%20%5Cright%29&bg=ffffff&%23038;fg=000000&%23038;s=0' alt='R = \left( \begin{smallmatrix} 1&0.8&0.2\\ 0.8&1&0.7\\0.2&0.7&1 \end{smallmatrix} \right)' title='R = \left( \begin{smallmatrix} 1&0.8&0.2\\ 0.8&1&0.7\\0.2&0.7&1 \end{smallmatrix} \right)' class='latex' />

Once the correlation matrix is set the researcher takes the Cholesky decomposition of the correlation matrix. Multiplying the Cholesky decomposition of the correlation matrix by the data matrix the resulting matrix is a transformed dataset with the specified correlation.

The R code from below will generate a correlation matrix of:

<img src='http://s.wordpress.com/latex.php?latex=R%20%3D%20%5Cleft%28%20%5Cbegin%7Bsmallmatrix%7D%201%260.7997999%260.1998661%5C%5C%200.7997999%261%260.7000217%5C%5C0.1998661%260.7000217%261%20%5Cend%7Bsmallmatrix%7D%20%5Cright%29&bg=ffffff&%23038;fg=000000&%23038;s=0' alt='R = \left( \begin{smallmatrix} 1&0.7997999&0.1998661\\ 0.7997999&1&0.7000217\\0.1998661&0.7000217&1 \end{smallmatrix} \right)' title='R = \left( \begin{smallmatrix} 1&0.7997999&0.1998661\\ 0.7997999&1&0.7000217\\0.1998661&0.7000217&1 \end{smallmatrix} \right)' class='latex' />

R = matrix(cbind(1,.80,.2, .80,1,.7, .2,.7,1),nrow=3) U = t(chol(R)) nvars = dim(U)[1] numobs = 100000 set.seed(1) random.normal = matrix(rnorm(nvars*numobs,0,1), nrow=nvars, ncol=numobs); X = U %*% random.normal newX = t(X) raw = as.data.frame(newX) orig.raw = as.data.frame(t(random.normal)) names(raw) = c("response","predictor1","predictor2") cor(raw) plot(head(raw, 100)) plot(head(orig.raw,100))

**leave a comment**for the author, please follow the link and comment on their blog:

**Statistical Research » R**.

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.