Lets start with your question. I actually downloaded your data, but the code you gave to read the file does not work. it errors with:
Error in read.zoo("Downloads/Matrix.csv", header = FALSE, sep = ";", format = "%m/%d/%Y", :
index has 155 bad entries at data rows: 2 3 6 7 ...
I also changed the sep=','
to sep=';'
since that is the data you provided. Next you assign niter
which, since x.r
is 256 rows, winds up being -3
. So, of course, x.r.w
cannot be created since nrow=niter
doesn't mean anything...
Finally, speeding up your code:
Move things that are constant outside of your for loop. Specifically:
zeros = array(0, dim = c(N,1))
aMat = t(array(1, dim = c(1,N)))
Since they don't depend on i
.
I did find the function solve.QP
but didn't find cov.shrink
. And I'm not familiar with either. Often solvers are slow and iterative and difficult to speed up. However, it sounds like this is something that could easily be split into multiple threads. Take a look at foreach
.
As far as avoiding for loops
, sometimes code is both clearer and as efficient in a for loop
as it would be in an apply
type function. For loops
don't have be eliminated, just used appropriately.
In the future, please create a completely reproducible example that contains your data, the packages you're using and code that works. This will lead you to deeper understanding of the problem and will get you much better, more accurate and more complete answers.