1

I have lists named dat and res with 33 datasets each. Iam running global minimum variance portfolios with a for loop like this:

for(i in dat) {
N <- ncol(i)
Amat <- t(rbind( rep(1,N), diag(rep(1,N)), diag(rep(-1,N))))
bvec <- c(1 , rep(0,N), rep(-0.15,N))
Dmat <- cov.shrink(i) #from tawny package 
dvec <- rep(0,N)
mvw <- solve.QP( Dmat, dvec, Amat, bvec, meq = 1 )$solution #from quadprog package
names(mvw) = colnames(i)
}

Its working, but saves only the last iteration of course. I dont know how to save them all. I need to save all the 33 mvw results in a list. How can I do it?

After that, i have to evaluate the results of mvw with the equivalent resdatasets:

mvwreturns = res%*%matrix(mvw,ncol=1)

This command give me the portfolio returs evaluated for each period. Each mvw data matches with one res dataset. How can I do this? Iam wondering if I have to run another for loop.

Any help is appreciated. Sorry for my poor english.

2 Answers 2

1

You can do the following:

Initiate an empty list mvw before the loop. Then add each dataset as a new element in the list:

mvw <- NULL #initiate list
counter <- 1
for(i in dat ) { 
N <- ncol(i)
Amat <- t(rbind( rep(1,N), diag(rep(1,N)), diag(rep(-1,N))))
bvec <- c(1 , rep(0,N), rep(-0.15,N))
Dmat <- cov.shrink(i) #from tawny package 
dvec <- rep(0,N)
mvw[[counter]] <- solve.QP( Dmat, dvec, Amat, bvec, meq = 1 )$solution) #from quadprog package
#for each iteration mvw will be stored as the i-th element in a list
names(mvw[[counter]]) = names(i)
counter <- counter + 1
}

Then just do another small loop, same way as before, to do what you want:

mvwreturns <- NULL
for ( i in 1:length(mvw) {
      mvwreturns[i] = res[i]%*%matrix(mvw[i],ncol=1)
}

Sorry, I do not have any data to run your script but you get the idea.

Hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Man! Thanks a lot for teaching me These stuff! I really liked that! And again , sorry for my poor Inglês and most of all sorry for your time!
That is absolutely fine :). Don't worry about it, it's my pleasure. It makes me happy to help. And I think your English is great, I was able to get everything. Have fun!
Thank you! I still have 2 or 3 steps to accomplish yet! I will try hard, but if I get stuck I think I´ll need some extra help!! LOL
0

Instead of using a for-loop, check out mapply. Save the code you want to run over each entry into a function that returns mvw, push dat and res into a mapply call with said function, and it should split out a list of the result set for each element from dat and res.

1 Comment

Thank you for your answer. I just didnt get what you are suggesting. I dont have such experience in R. Tks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.