# [R] can't find 'predict.loess' in a loop

Thu Dec 22 09:39:13 CET 2011

```
Season's Greetings.

I am using 'predict' in a loop, but it is behaving unexpectedly, so I am more explicit and use 'predict.loess', and R says it can't find it. Even if I explicitly load the stats library, which is already in the search path.

Any ideas?
This is odd, because if I use RB & LB from a different dataset, it works fine...

Stuart

*********************************************************************
*************************
Original problem:

> j<-0                      # start an iteration counter
> while(j<30)               # (max 30 iterations)
+
+ {
+
+   lenRB<-length(RB)       # number of rows in original RB
+   lenLagRB<-(j+1):lenRB   # row numbers for 'lagged' RB
+   lenLagRB0<-1:(lenRB-j)  # row numbers for 'unlagged' RB
+   RBj<-RB[lenLagRB]       # create a 'lagged' RB of this length
+   LBj<-LB[lenLagRB]       # create a 'lagged' LB of this length
+   RB0<-RB[lenLagRB0]      # create an 'unlagged', shortened RB of this length
+   LB0<-LB[lenLagRB0]      # create an 'unlagged' LB of this length
+
+   cor.j<-cor(RBj,LB0)     # check RB lagged & LB have low correlation
+ if(abs(cor.j)<0.01)     # if so...
+
+   {
+
+ RELBj<-100*(RBj-LB0)/(RBj+LB0)  # ...generate index for lagged pair
+ RELB0<-100*(RB0-LB0)/(RB0+LB0)  # generate index for unlagged pair
+
+   Lag.lo <-loess(RBj~RELBj,span=sp, family=("symmetric"), degree=2) # close approx of lowess!
+   Real.lo<-loess(RB0~RELB0,span=sp, family=("symmetric"), degree=2)
+   Lag.pr <-predict(Lag.lo, data.frame(x=seq (-80,80,1)))  # predict for uniform index scale
+   Real.pr<-predict(Real.lo,data.frame(x=seq (-80,80,1)))
+
+   Diff.pr<-Real.pr-Lag.pr                                   # difference plot
+   lines (Diff.pr~seq(-80,80,1),pch=1,cex=0.25,col=1)
+
+ }
+
+ j<-j+1                   # increment counter and repeat
+ }
Error in model.frame.default(formula = Diff.pr ~ seq(-80, 80, 1), na.action = NULL) :
variable lengths differ (found for 'seq(-80, 80, 1)')
1: 'newdata' had 161 rows but variable(s) found have 250 rows
2: 'newdata' had 161 rows but variable(s) found have 250 rows

****************************
Make it more explicit I want to use predict.loess:

> j<-0                      # start an iteration counter
> while(j<30)               # (max 30 iterations)
+
+ {
+
+   lenRB<-length(RB)       # number of rows in original RB
+   lenLagRB<-(j+1):lenRB   # row numbers for 'lagged' RB
+   lenLagRB0<-1:(lenRB-j)  # row numbers for 'unlagged' RB
+   RBj<-RB[lenLagRB]       # create a 'lagged' RB of this length
+   LBj<-LB[lenLagRB]       # create a 'lagged' LB of this length
+   RB0<-RB[lenLagRB0]      # create an 'unlagged', shortened RB of this length
+   LB0<-LB[lenLagRB0]      # create an 'unlagged' LB of this length
+
+   cor.j<-cor(RBj,LB0)     # check RB lagged & LB have low correlation
+ if(abs(cor.j)<0.01)     # if so...
+
+   {
+
+ RELBj<-100*(RBj-LB0)/(RBj+LB0)  # ...generate index for lagged pair
+ RELB0<-100*(RB0-LB0)/(RB0+LB0)  # generate index for unlagged pair
+
+   Lag.lo <-loess(RBj~RELBj,span=sp, family=("symmetric"), degree=2) # close approx of lowess!
+   Real.lo<-loess(RB0~RELB0,span=sp, family=("symmetric"), degree=2)
+   Lag.pr <-predict.loess(Lag.lo, data.frame(x=seq (-80,80,1)))  # predict for uniform index scale
+   Real.pr<-predict.loess(Real.lo,data.frame(x=seq (-80,80,1)))
+
+   Diff.pr<-Real.pr-Lag.pr                                   # difference plot
+   lines (Diff.pr~seq(-80,80,1),pch=1,cex=0.25,col=1)
+
+ }
+
+ j<-j+1                   # increment counter and repeat
+ }
Error: could not find function "predict.loess"

****************

> search()
[1] ".GlobalEnv"        "nshd"
[3] "package:foreign"   "package:stats"
[5] "package:graphics"  "package:grDevices"
[7] "package:utils"     "package:datasets"
[11] "package:base"
> ?predict.loess

... Confirms that predict.loess is in the path, and can be accessed.
Only not within the loop?
This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.   Please do not use, copy or disclose the information contained in this message or in any attachment.  Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.
```