-3

I'm trying to avoid using a for loop if possible. This is the whole code, the data of the matrix are just percentages.

x.r = read.zoo("Matrix.csv", header=F, sep = ",", format = "%m/%d/%Y")
yrs = 5
niter = nrow(x.r)-yrs*52+1
N = ncol(x.r)
x.r.w = matrix(0,nrow=niter,ncol=N)
for (i in 1:niter) {
     x.r.sub = x.r[i:(i+yrs*52-1),]
     covar = cov.shrink(x.r.sub)
     zeros = array(0, dim = c(N,1))
     aMat  = t(array(1, dim = c(1,N)))
     res = solve.QP(covar, zeros, aMat, bvec=1, meq = 1)
     x.r.w[i,] = res$solution
     }

I have uploaded a portion of the data in Matrix.csv in this link

Matrix.csv

nopeva
  • 1,583
  • 5
  • 22
  • 38
  • 2
    See [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Can you post a portion of your data and the variables you're using? Namely `x.r`, `niter`, `yrs`, `N`, and `x.r.w`. Also, you don't need `next` its implicit in a `for loop`. – Justin Jan 09 '13 at 15:54
  • @Justin thanks for your response. It's time series data and I run the code for each 52 observations subset within a bigger array. I'm just new to R and don't know if what I want is possible. – nopeva Jan 09 '13 at 16:12
  • 1
    Everything is possible in R. The more information you provide about your data and problem, the better the answer will be. – Justin Jan 09 '13 at 16:20
  • @Justin I've posted the code. I can upload the file if you need it. – nopeva Jan 09 '13 at 16:44
  • I suppose that again you feel that have not enough information so thanks for your time anyway although I don't understand your position to be honest. – nopeva Jan 09 '13 at 22:09
  • I haven't down voted you, but the reason is that I cannot run your code. if you provide a portion of `Matrix.csv` or `dput(head(x.r))` then we can run your code and try to help. – Justin Jan 09 '13 at 22:16
  • @Justin the file is uploaded maybe you can have a look. I think that can vectorize some lines individually but am not too sure how to make it work with all lines together. – nopeva Jan 09 '13 at 22:57

1 Answers1

3

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.

Community
  • 1
  • 1
Justin
  • 42,475
  • 9
  • 93
  • 111
  • The package that includes cov.shrink is corpcor. Thanks for your reply anyway. The errors you get might be related to my windows config region because I don't know why R sometimes recognizes sep =";" as sep="," in my pc. – nopeva Jan 10 '13 at 16:35