# [R-sig-Finance] fMultivar rollMax question

Spencer Graves spencer.graves at pdf.com
Sat Jul 8 04:35:54 CEST 2006

```	  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:
> I am using fMultivar under R 2.2.1 on a Debian linux box. Could
> someone, please, explain to me why there are two trailing NAs in the
> last statement in the code beow?
>
>
>> library(fMultivar)
>> x <- 1:20
>> rollMax(x, n=3)
>  [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>> rollMax(x, n=2)
>  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>> rollMax(x, n=1)
>  [1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 NA NA
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance

```