[R-SIG-Finance] rollapply - error message - wrong sign in 'by' argument

Bernd Dittmann bd10stats at googlemail.com
Mon Apr 23 13:42:50 CEST 2012


Hi group,

Having upgraded R and zoo & tseries, I am puzzled why the following
rollapply() script gives me this error:

"Error in seq.default(start.at, NROW(data), by = by) :
  wrong sign in 'by' argument"

Here's a worked example:

require(tseries)

# some price series
rr <- get.hist.quote(instrument="RR.L", start="2012-01-01", quote="AdjClose)
bp <- get.hist.quote(instrument="BP.L", start="2012-01-01", quote="AdjClose)

# convert to log returns:
rr.r <- diff(log(rr))
bp.r <- diff(log(bp))

data.set <- merge(rr.r, bp.r)

I am trying to calculate the correlation between rr.r and bp.r on a
rolling basis. For which I have defined the following function:


ROLCOR <- function(x){
    # normalized returns
    x1.norm <- (x[, 1] - mean(x[, 1])) / sd(x[, 1])
    x2.norm <- (x[, 2] - mean(x[, 2])) / sd(x[, 2])

    # 95 pct confidence interval
    x2 <- as.numeric(coef(lm(x1.norm ~ 0 + x2.norm))[1])
    x1 <- as.numeric(confint(lm(x1.norm ~ 0 + x2.norm), level = 0.95))[1]
    x3 <- as.numeric(confint(lm(x1.norm ~ 0 + x2.norm), level = 0.95))[2]

    # p-value
    pval <- as.numeric(coef(summary(lm(x1.norm ~ 0 + x2.norm)))[4])

    result <- c(x1, x2, x3, pval)
    return(result)
}


Now, applying function ROLCOR over the whole set, it works just fine:

ROLCOR(data.set)
[1] 2.805971e-01 4.833686e-01 6.861401e-01 9.736237e-06


However, when running ROLCOR via rollapply(...), things get funny:

rollapply(data.set, width = 250, ROLCOR, by.column = FALSE, align = "right")

I am getting this error message:

"Error in seq.default(start.at, NROW(data), by = by) :
  wrong sign in 'by' argument"

What confuses me is that it worked before I updated R from 2.14.2 to
2.15. Even if I set "by = 1", it does not resolve things.

What am I doing wrong here? I am sure it's something trivial but I
sadly cannot see the forest before its trees.

Much obliged & kind regards,

Bernd


Last but not least, my session Info:

R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] tseries_0.10-28 zoo_1.7-7       quadprog_1.5-4

loaded via a namespace (and not attached):
[1] grid_2.15.0    lattice_0.20-6 tools_2.15.0



More information about the R-SIG-Finance mailing list