On generating uncorrelated random variables

Suppose we are simulating data under common factor model, using linear combination

\mathbf{ X= \Lambda F + E. }

Often times, the measurement errors  \mathbf{ E } are assumed to be uncorrelated with each other. When generating uncorrelated random normal variables,

matrix(rnorm(N*k),N,k)

and

mvrnorm(N,rep(0,k),diag(1,k))

are not the same. Multivariate sampling should be used to ensure the orthogonality assumption is met.

can both be used interchangeably. The eigenvalue distributions are as follows:

mvrtest

library(MASS)

N = 100
k = 10
n.trials = 1000

set.seed(1)

E1.ev = matrix(,n.trials,k)
E2.ev = matrix(,n.trials,k)

for(i in 1:n.trials){
 E1 = matrix(rnorm(N*k),N,k)
 E2 = mvrnorm(N, rep(0,k), diag(1,k))
 E1.ev[i,] = eigen(cor(E1))$values
 E2.ev[i,] = eigen(cor(E2))$values
}

E1.ev.m = apply(E1.ev, 2, mean)
E1.ev.u = apply(E1.ev, 2, quantile, probs = .975)
E1.ev.l = apply(E1.ev, 2, quantile, probs = .025)

E2.ev.m = apply(E2.ev, 2, mean)
E2.ev.u = apply(E2.ev, 2, quantile, probs = .975)
E2.ev.l = apply(E2.ev, 2, quantile, probs = .025)

col.w = '#999999'
col.b = '#333333'
col.1 = '#ff0080c0'
col.2 = '#0080ffc0'

par(bg = '#333333', fg = col.w)
plot(rep(0,k), type='n', ylim = c(0,2), ylab = 'Eigenvalue',
     col.axis = col.w, col.lab = col.w)
lines(E1.ev.m, col = col.1, lwd = 4)
lines(E1.ev.u, col = col.1, lwd = 2)
lines(E1.ev.l, col = col.1, lwd = 2)
points(E1.ev.m, pch = 21, col = col.1, bg = 'white', lwd = 2)

lines(E2.ev.m, col = col.2, lwd = 4)
lines(E2.ev.u, col = col.2, lwd = 2)
lines(E2.ev.l, col = col.2, lwd = 2)
points(E2.ev.m, pch = 21, col = col.2, bg = 'white', lwd = 2)

legend(6, 1.8, legend = c("matrix(rnorm)","mvrnorm"), 
               lty = 1,
               lwd = 2,
               col = c(col.1, col.2),
               bty = 'n')
Advertisements