# Generating skewed correlated data in R

I am interested in generating correlated, skewed data in order to evaluate the application of a statistical approach to a data set I am analyzing. Specifically, I have two correlated variables, and I can determine the correlation coefficient ($R^2$) between the two, as well as the shape, scale and location of each skewed distribution using the sn.em command within the sn package for R.

I can generate a skewed distribution using the rsnorm function within the VGAM package. However, I cannot tell it to generate a second vector that is correlated to the first, also skewed, and has its own specific set of shape, scale and location parameters as calculated from the data.

Any advice on how to do this would be greatly appreciated, and thank you in advance!

-

## migrated from stackoverflow.comOct 2 '13 at 4:00

This question came from our site for professional and enthusiast programmers.

There is a skewnormal package called sn that does this. See this for a bit of an explanation: azzalini.stat.unipd.it/SN/faq-r.html The 1999 paper has more: azzalini.stat.unipd.it/SN/paper98e.pdf . You will need to check the parameterization matches what you expect. I was a bit surprised. –  Seth Sep 18 '13 at 15:51

You can get the marginal distributions you want easily enough, but you may not also be able to get the populations to be linearly correlated as you wish - the two desires are not generally compatible.

However, one thing you can do is specify a monotonic measure of correlation (like Spearman's rho or Kendall's tau); these survive monotonic transformation intact. As such, you can use copulas to generate two uniform marginals with the desire amount of association, and then transform the margins to exactly what you desire.

By judicious choice of copula family you may be able to get the linear correlation close to the desired one.

-

An approach that I've had surprising success with (in Mathematica -- I don't know R) takes any set of pairs $(x_1,y_1),\ldots,(x_n,y_n)$ and tries to create a specified correlation by re-pairing the data. That is, iterating over all $n(n-1)/2$ pairs $(i,j)$, swap $y_i$ and $y_j$ if it will bring the correlation closer to the specified value. In general, it seems to work best when the $n(n-1)/2$ absolute differences among $x_1,\ldots,x_n$ contain few ties, and similarly for $y_1,\ldots,y_n$. (And, of course, when the specified correlation is not ruled out by the marginal distributions.)

-

Let r be the correlation value you want

r=.3


Simulate two random variables from the same density, they should be independent. If not you should insure their correlation is 0 (maybe a cholesky decomposition?)

x=rsnorm(1000,0,2,4)
y=rsnorm(1000,0,2,4)

cor(x,y)
[1] 0.007983692


Use this formula to get the correlated second variable y1

y1=x*r+y*sqrt(1-r^2)

cor(x,y1)
[1] 0.3006946


This will only work for you if (1) the two input random variables' have the same distribution and (2) you only care about one of the output random variables' distribution.

-
Hi, Thank you for the response. The two variables do not come from the same distribution. They have different means, and as a result different parameters for shape, scale and location of their respective skewed distributions. I just attempted using your script anyways, with the following distributions (parameters taken from my data): library(VGAM) #generate distributions x=rsnorm(220,shape= 10.6852380,scale= 1.2881360,location=0.2788605 ) y=rsnorm(220,shape= 19.695530,scale= 28.453448,location= 3.316043 ) #specify r2 r= 0.6835 #generate new y values y1=x*r+y*sqrt(1-r^2) summary(lm(y1~x)) –  colin Sep 17 '13 at 23:13
This answer will not work for your parameters. If you have data why don't you use a resampling/bootstrap? –  Seth Sep 18 '13 at 0:33
I want to simulate data to do a monte carlo simulation to show the structure of my analysis and the groups my data were sampled from are not generating spurious results. I am confident in the analysis, but the reviewer is not. I don't think resampling could achieve this. –  colin Sep 18 '13 at 1:45
While I don't know your reviewer, I would say that resampling makes fewer assumption when compared to simulation from parameters. –  Seth Sep 26 '13 at 19:17