This should be pretty fast if you get to the math level of linear regression. If X is an independent variable and Y is a dependent variable. The coefficients are determined by the expression
Beta = inv(t(X) %*% X) %*% (t(X) %*% Y)
I'm a little confused about which variable you want to be dependent and which is independent, but hopefully solving a similar problem below will help you.
In the example below, I take 1000 variables instead of the original 5 and do not enter any NA.
require(xts) data <- matrix(sample(1:10000, 1500000, replace=T), 1500, 1000, byrow = TRUE)
Now we can calculate the odds using the Joshua TTR package.
library(TTR) loop.begin.time <- Sys.time() in.dep.var <- data[,1] xx <- TTR::runSum(in.dep.var*in.dep.var, obs) coeffs <- do.call(cbind, lapply(data, function(z) { xy <- TTR::runSum(z * in.dep.var, obs) xy/xx })) loop.end.time <- Sys.time() print(loop.end.time - loop.begin.time)
Time difference 3.934461 sec.
res.array = array(NA, dim=c(NC, NR, obs)) for(z in seq(obs)) { res.array[,,z] = coredata(data - lag.xts(coeffs, z-1) * as.numeric(in.dep.var)) } res.sd <- apply(res.array, c(1,2), function(z) z / sd(z))
If I have not made any errors in res.sd indexing, you should give standardized leftovers. Feel free to fix this solution to fix the errors.