|
I am trying to create a looping function that takes 252 rows of a matrix from 1, then the next 252 rows of a matrix from 1+1, so on and so forth until nrow of the matrix.
I am using the for + if loop combo but I seem to be stuck on the if portion. I'm trying to code if so that if the row is less than the last row in matrix ret, it will keep running, else break. Here is what I have so far: for(i in 1:i+252) { + if(ret[<=nrow(ret),]) Any help would be appreciated very very much. Thank you! |
|
Perhaps an easier coding that shows what I want would be this:
> for(i in 1:nrow(ret)) { + retT=ret[1+i:252+i,]} Thank you so much for your time and help! |
|
On Jun 24, 2012, at 11:37 AM, tbowlo wrote: > Perhaps an easier coding that shows what I want would be this: > >> for(i in 1:nrow(ret)) { > + retT=ret[1+i:252+i,]} You should be thinking about what will happen in that indexing operation when the loop pushes 'i' above nrow(ret)-251 (You may want to look at the 'embed' function, once that is, after you have correctly sorted out your understanding of the "dimensions" of this extraction.) > > Thank you so much for your time and help! > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with-a-repeating-process-tp4634336p4634339.html > Sent from the R help mailing list archive at Nabble.com. And you should learn that rhelp is NOT on Nabble and that the list rules say that you should be posting context for your postings ...even when (or perhaps especially when) you are amending earlier requests for help. > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by tbowlo
On Sun, Jun 24, 2012 at 08:37:23AM -0700, tbowlo wrote:
> Perhaps an easier coding that shows what I want would be this: > > > for(i in 1:nrow(ret)) { > + retT=ret[1+i:252+i,]} Hi. Using your previous description, i think, you mean (1+i):(252+i). Note that this is quite different from 1+i:252+i, which is equivalent to 1 + (i:252) + i. i <- 10 identical(1+i:252+i, 1 + (i:252) + i) [1] TRUE Hope this helps. Petr Savicky. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
That certainly helps Petr. In terms of the overall question, perhaps it is better to show the code and reexplain what exactly I am trying to do:
1. I have logetf values in a [3300,9] matrix 2. I am trying to find the w value in the function: sum(abs(retT-w*prcomp(ret[(1+i):(252+i),]$rotation[,1]) 3. retT are time T log etf value, prcomp(ret[(1+i):(252+i),]$rotation[,1] is the Principal component value for 252 days prior to time T. 4. I want to utilize the DEoptim() function in a loop from time T (the beginning T value is logetf value on the 253rd day) until the last day, which would correspond with nrow(ret) [or around 3300]. DEoptim will find the w value that minimizes that function. #etf dataset logetf=log(etf) ret=as.matrix(logetf, nrow=dim(logetf)[1], ncol=dim(logetf)[2]) #principal component using eigenvalues library(DEoptim) library(zoo) retT=ret[c(253:nrow(ret)),] retQ=ret[c(1:252),] pr=prcomp(retQ) pr1=pr$rotation[,1] pr1=matrix(pr1,nrow=1, ncol=9) #w1 Values for(i in 252:nrow(ret)){ fn<-function(w) {sum(abs(retT-w*prcomp(ret[(1+i):(252+i),]$rotation[,1])))}} lower<-c(-1000000) upper<- -lower set.seed(1234) record=DEoptim(fn, lower, upper) If there is any more information I can provide to make it more clear, please let me know. My coding skills is really subpar for this project but I need to finish it asap. I REALLY appreciate anyone's help! |
| Powered by Nabble | Edit this page |
