I am trying to generate Correlated Uniform Random Variables with given mean, standard and correlation structure. I have looked through various posts in this topic including this(Can I use the Cholesky-method for generating correlated random variables with given mean?) and seems to can't get through.
Here is how I am proceeding.
set.seed(12)
corr_raw=matrix(0.7, nrow = 4,ncol=4) # Correlations
diag(corr_raw)=c(1,1,1,1)
colnames(corr_raw)<-c("A","B","C","D")
rownames(corr_raw)<-colnames(corr_raw)
Cov=(std_dev%*%t(std_dev))*corr_raw # covariance matrix
#mean and standard deviation
mu<-c(1,3,2,4)
std_dev<-0.4*mu #assumed std_dev = 0.4*mean
#Cholesky Decomposition matrix L
L<-chol(Cov, pivot =T)
Z<-matrix(nrow = 0,ncol=10000)
for (i in colnames(Cov))
{
#stddev(Unif)=(B-A)/sqrt(12)=1; and mean(Unif)=(B+A)/2=0; A=-sqrt(3) and B=+sqrt(3)
Z<- rbind(Z,runif(10000,-1.732,1.732))
}
x=mu+L%*%Z #transform to get the desired mean and variance
corr<-cor(t(x)) #find correlation of the generated random variable x
However, the corr(x) and corr_raw does not match. What am I doing wrong ?