I am trying to quickly solve equations (forms x% *% res = y) for a large array in R CRAN.
I have data x and y and you want to calculate res. How can this be done best, i.e. Quickly? Many thanks!
Here is an example and some approaches: (does it seem that the “solve” is the fastest?)
p = 20
nmkt= 3000
res = matrix(0,p,nmkt)
x = array(rnorm(p*p*nmkt),c(p,p,nmkt))
for(i in 1:nmkt){ x[, , i]= crossprod(x[, , i])+diag(p)*0.01}
y = matrix(rnorm(p*nmkt),nmkt,p)
R=100
system.time(for(r in 1:R){ for(i in 1:nmkt){res[,i] = qr.solve(x[, , i], y[i,], tol = 1e-7)}})
system.time(for(r in 1:R){ for(i in 1:nmkt){res[,i] = solve(x[, , i], y[i,], tol = 1e-7)}})
system.time(for(r in 1:R){ for(i in 1:nmkt){res[,i] = crossprod( chol2inv(chol( x[, , i] )) , y[i,] )}})
Is looping through an array a good solution?
source
share