Now Let's switch to a case with 4 variables among which the first and second as well as the third and fourth are correlated. In other words, the four variables are assumed to follow a pentavariatequadrivariate Gaussian $(y_{1}, y_{2}, y_{3}, y_{4})' \sim N((0, 0, 0, 0)', S)$:
r1 <- 0.2; r2 <- 0.8 # correlation value to be recovered
ns <- 2000 # number of samples
S <- matrix(c(1,r1,0,0, # correlation structure of pentavariatequadrivariate data
r1,1,0,0, # the first and second variables are correlated
0,0,1,r2, # the third and fourth variables are correlated
0,0,r2,1), nrow=4,ncol=4)
# simulated data
dat <- data.frame(f = c(rep(paste0('P',1:ns), 2), rep(paste0('T',1:ns), 2)),
R=c(rep('P',2*ns), rep('T',2*ns)),
y = c(mvrnorm(n=ns, mu=rep(0,4), Sigma=S)))