Try this:
x <- rexp(100, 1/3)
xp <- scale(x)
cor.mat <- rbind( c(1, 0.8, 0.7), c(0.8, 1, 0.3), c(0.7, 0.3, 1) )
x23 <- matrix( rnorm( 100 * 2, 1/3 ), ncol=2 )
x23 <- cbind(xp, scale(x23))
x23 <- x23 %*% solve(chol(var(x23))) ## skip if you don't want exact
cor's
x.new <- x23 %*% chol(cor.mat)
x.new[,1] <- x.new[,1]*attr(xp, 'scaled:scale') + attr(xp,
'scaled:center')
all.equal( x.new[,1], x )
cor(x.new)
pairs(x.new)
you can generate the data from other distributions than the normal, and you can
add and multiply constants in the columns of x.new to give different
means/sd's. This will set the correlations, but the exact distributions of
the columns is not guaranteed.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of carol white
> Sent: Thursday, November 12, 2009 9:10 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] simulated correlated vectors
>
> Hi,
> Having a vector x of float type without any assumption of its
> distribution (mean, variance etc), how to generate another vector that
> is correlated with x? Extensibility: how to simulate more than two
> correlated vectors (again float type and no preference for their
> distribution and without having vector x)?
>
> Cheers,
>
> Carol
>
> ______________________________________________
> R-help at 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.