[R] Ruofei Mo - How can I generate correlated data with non-normal distribution?
Ruofei Mo【莫若飞】
911mruofei at tongji.edu.cn
Wed Mar 2 22:06:18 CET 2016
Hi, All,
I have a question about how to generate correlated data with non-normal
distribution? Basic, I have a variable a that follows a normal distribution,
a ~ N(0,1), then I want to generate another variable b that follows a
uniform distribution, b ~ U(0, 1). Most importantly, I want the correlation
between a and b to be fixed at -.9, cor(a,b) = -.90
I tried the following code,
### Correlation matrix rmvnorm() function ###
Cormat <- matrix(c(1, -.9, -.9, 1), ncol = 2) # Here, I want to create 2
variables that have correlation -.9
### Theta-Transform-Guessing ###
DataSet <- data.frame(rmvnorm(1000, mean=c(0, 0), sigma=Cormat))
Names(DataSet) <- c("a", "trans")
### Using trans to be transformed into guessing parameters ###
DataSet$b <- pnorm(DataSet$trans, mean=mean(DataSet$trans),
sd=sd(DataSet$trans)) # Here, I used the pnorm() function to transform one
variable to a U(0, 1)
However, the correlation is changed. Can anyone give me some suggestion that
how can I generate the data?
Thanks,
Ruofei
[[alternative HTML version deleted]]
More information about the R-help
mailing list