[R] Ruofei Mo - How can I generate correlated data with non-normal distribution?
Ben Bolker
bbolker at gmail.com
Thu Mar 3 00:46:11 CET 2016
Ruofei Mo【莫若飞】 <911mruofei <at> tongji.edu.cn> writes:
>
> 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 ###
>
I don't know that there's a closed-form solution to this problem.
Here's an attempt to do it by brute force. By eyeball, you need to
set the nominal rho to about -0.92 to get a realized rho of -0.9.
simfun <- function(rho,n=10000) {
cormat <- matrix(c(1, rho, rho, 1), ncol = 2)
dd <- setNames(data.frame(MASS::mvrnorm(1000, mu=c(0,0), Sigma=cormat)),
c("a","trans"))
dd <- transform(dd,
b=pnorm(trans,mean(trans),sd(trans)))
dd[,c("a","b")]
}
cvec <- seq(-0.999,-0.85,length=51)
res <- expand.grid(rho=cvec,rep=1:10)
set.seed(101)
res$cor <- sapply(res$rho,
function(r) cor(simfun(rho=r,n1e6))[1,2])
par(las=1,bty="l")
plot(cor~rho,data=res)
abline(a=0,b=1,col=2)
abline(h=-0.9,col=4)
abline(v=-0.92,col=4)
More information about the R-help
mailing list