[R] problem with nested loop for regression

jim holtman jholtman at gmail.com
Tue Aug 5 02:35:51 CEST 2008


Exactly what problem are you having?  There is nothing wrong with
nested for loops, so what is leading you to believe you have a
problem?  I ran your program and it seems to terminate.  Most of the
time seems to have been spent in the following statement:

                  if(!all(is.na(sel_col))) {Preg[,k]<-coef(lm(tt~sel_col))}

Given that you appear to be executing this statement about 9000 times,
it is not surprising.  So can you provide more details?

  0  51.2 root
  1.   44.5 coef
  2. .   44.1 lm
  3. . .   15.7 eval
  4. . . .   15.6 eval
  5. . . . |   15.6 model.frame
  6. . . . | .   15.3 model.frame.default
  7. . . . | . .    6.0 sapply
  8. . . . | . . .    3.8 lapply
  9. . . . | . . . .    3.0 FUN
 10. . . . | . . . . |    1.1 %in%
 11. . . . | . . . . | .    1.0 match
 12. . . . | . . . . | . .    0.8 is.factor
 13. . . . | . . . . | . . .    0.7 inherits
 10. . . . | . . . . |    1.0 .deparseOpts
 11. . . . | . . . . | .    0.3 pmatch
  9. . . . | . . . .    0.5 as.list
  8. . . . | . . .    1.7 unique
  9. . . . | . . . .    0.7 unique.default
  9. . . . | . . . .    0.6 unlist
 10. . . . | . . . . |    0.4 lapply
  8. . . . | . . .    0.3 unlist
  7. . . . | . .    4.0 na.omit
  8. . . . | . . .    3.8 na.omit.data.frame
  9. . . . | . . . .    3.0 [
 10. . . . | . . . . |    2.9 [.data.frame
 11. . . . | . . . . | .    0.6 duplicated
 12. . . . | . . . . | . .    0.3 duplicated.default
 11. . . . | . . . . | .    0.3 [
 12. . . . | . . . . | . .    0.2 [.ts
 11. . . . | . . . . | .    0.3 [[

On Mon, Aug 4, 2008 at 12:58 PM, rcoder <mpdotbook at gmail.com> wrote:
>
> Hi,
>
> I guess my question is really more about the nested for loop construct and
> whether it is doing what I intend it to do in my code in the previous post.
> I would be grateful if anyone who has used nested loops could let me know if
> I am doing something wrong.
>
> Thanks,
>
> rcoder
>
>
>
> rcoder wrote:
>>
>> Hi everyone,
>>
>> I'm experiencing difficulty getting the results I want when I use a nested
>> for loop. I have a data set to which I perform some calculations, and then
>> try to apply a regression over a rolling window. The code runs, but the
>> regression results I am getting (intercept and slope) are simply the same,
>> repeated again and again in the results matrix. The regression does not
>> seem to be obeying the instructions of the nested loop, which intend it to
>> calculate regression coefficients over a data one row at a time. I have
>> been struggling with the code for many days now, testing various parts,
>> and I cannot seem to get the nested loop to work as I want it to. I would
>> be very grateful for any suggestions. Below is a brief version of my code:
>>
>> #Code start
>> library(zoo)
>> seed.set(1)
>> Pmat<-matrix(rnorm(1000), nrow=100, ncol=100)
>> maxcol<-ncol(Pmat)
>> maxrow<-nrow(Pmat)
>> startrow<-10
>> period<-10
>> wind<-2                               #roll window
>> subdiv<-period/wind
>> rollstart<-11                                 #start roll at period+1
>> #converting Pmat into ts for rollapply() to work...
>> PmatTS<-ts(Pmat)
>> Preg<-matrix(NA,ncol=maxcol,nrow=2*maxrow)
>> PmatWt<-matrix(NA, nrow=subdiv,ncol=maxcol)
>> mult_col<-matrix(1:5, nrow=5, ncol=1)
>> #rolling calculations...
>> for (i in (rollstart):maxrow)
>>       {
>> #extract the relevant timeframe...
>> dslice<-PmatTS[(i-period):i,]
>> dslicets<-ts(dslice)
>> #operating on sliced data...
>> Pmin<-rollapply(dslicets, wind, by=wind, min, na.rm=F)
>> Pmax<-rollapply(dslicets, wind, by=wind, max, na.rm=F)
>> Pmult<-Pmin*Pmax                        #calculating product
>> tt<-time(Pmult)
>> for (j in 1:5)                                #1st nested loop
>>       {
>> PmatWt[j,]<-Pmult[j,]*mult_col[j,]
>>       }
>> #rolling regression analysis...
>> for (k in 1:maxcol)                   #2nd nested loop
>>             {
>>       sel_col<-PmatWt[,k]
>>       if(!all(is.na(sel_col))) {Preg[,k]<-coef(lm(tt~sel_col))}
>>             }
>>       }
>> #Code End
>>
>> Thanks,
>>
>> rcoder
>>
>
> --
> View this message in context: http://www.nabble.com/problem-with-nested-loop-for-regression-tp18792841p18815273.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list