Re: [R] 3D correlation

From: Martin Elff <elff_at_sowi.uni-mannheim.de>
Date: Wed, 6 Feb 2008 10:53:38 +0100

On Wednesday 06 February 2008 (01:35:15), ramakg_at_cecs.pdx.edu 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_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. Received on Wed 06 Feb 2008 - 09:57:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Feb 2008 - 10:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive