On Wednesday 06 February 2008 (01:35:15), [EMAIL PROTECTED] wrote: > how to generate correlated data which is correlated in three variables??
# Your correlation matrix S <- rbind( c(1,.3,.3), c(.3,1,.3), c(.3,.3,1) ) # Three independent normal variates x1 <- rnorm(1000) x2 <- rnorm(1000) x3 <- rnorm(1000) # Using the cholesky decomposition of S Y <- cbind(x1,x2,x3)%*%chol(S) # Three correlated normal variates y1 <- Y[,1] y2 <- Y[,2] y3 <- Y[,3] # Check cor(Y) There may be more elegant and general solutions and you can also use package "mvtnorm" to get correlated normal (or t) variates. But the principle comes down to this. Hope that helps, Martin ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.