Skip to content
Prev 927 / 15274 Next

fMultivar rollMax question

This is known in some circles as an "infelicity" in the code.  A 
corrected version of rollFun, called by rollMax, appears below.

	  In brief, the problem occurs because the author of rollFun (Diethelm 
Wuertz, I believe) did not envision n<2, and rollFun contains "for(i in 
2:n)".  My proposed solution is simply to insert a new statement as the 
first line of "rollFun":

   if(n<=1)return(FUN(x))

	  I'm copying Prof. Wuertz on this email, so he can take appropriate 
action.  Until that change works its way into code on your computer via, 
e.g., "update.packages()", you can copy the revised function into any 
script that uses rollMax, rollMin, rollMean, rollVar, or rollFun.

	  Hope this helps.
	  Spencer Graves
####################
rollFun <-
function (x, n, trim = TRUE, na.rm = FALSE, FUN, ...)
{
   if(n<=1)return(FUN(x))
   #
     x.orig = x
     if (is.timeSeries(x))
         TS = TRUE
     else TS = FALSE
     if (TS) {
         positions = x.orig at positions
         x = x.orig at Data[, 1]
     }
     else {
         x = as.vector(x.orig)
         names(x) = NULL
     }
     if (na.rm) {
         if (TS)
             positions = positions[!is.na(x)]
         x = as.vector(na.omit(x))
     }
     start = 1
     end = length(x) - n + 1
     m = x[start:end]
     for (i in 2:n) {
         start = start + 1
         end = end + 1
         m = cbind(m, x[start:end])
     }
     ans = apply(m, MARGIN = 1, FUN = FUN, ...)
     if (!trim)
         ans = c(rep(NA, (n - 1)), ans)
     if (trim & TS)
         positions = positions[-(1:(n - 1))]
     if (TS) {
         ans = timeSeries(as.matrix(ans), positions, units = x.orig at units,
             FinCenter = x.orig at FinCenter)
     }
     ans
}

####################
Omar Lakkis wrote: