- Thread starter consuli
- Start date

lemme give you an example using the cholesky decomposition. say the correlation matrix i want in the population is for 4 variables and looks like:

Code:

```
> R <- matrix(rep(0.5,16),4,4)
> diag(R) <- 1
> R
[,1] [,2] [,3] [,4]
[1,] 1.0 0.5 0.5 0.5
[2,] 0.5 1.0 0.5 0.5
[3,] 0.5 0.5 1.0 0.5
[4,] 0.5 0.5 0.5 1.0
```

first you generate 4 normal variables (standardized just to make things simpler) and assemble them in a matrix. i used a sample size of N=1000 for no particular reason

Code:

```
X <- cbind(rnorm(1000),rnorm(1000),rnorm(1000),rnorm(1000))
colnames(X) <- c("X1", "X2", "X3", "X4")
```

Code:

```
cholR <- chol(R)
data1 <- X%*%cholR
```

Code:

```
> cor(data1)
[,1] [,2] [,3] [,4]
[1,] 1.0000000 0.4939773 0.5189865 0.5142373
[2,] 0.4939773 1.0000000 0.4923293 0.4996839
[3,] 0.5189865 0.4923293 1.0000000 0.5221356
[4,] 0.5142373 0.4996839 0.5221356 1.0000000
>
```

Code:

```
> library(MASS)
> mu <- c(0,0,0,0) # vector of means
> data2 <- mvrnorm(1000, mu, R)
> cor(data2)
[,1] [,2] [,3] [,4]
[1,] 1.0000000 0.5149381 0.5311652 0.4995711
[2,] 0.5149381 1.0000000 0.5297210 0.5313051
[3,] 0.5311652 0.5297210 1.0000000 0.5254646
[4,] 0.4995711 0.5313051 0.5254646 1.0000000
>
```