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

Gabor Grothendieck ggrothendieck at gmail.com
Mon Apr 23 14:09:00 CEST 2012


On Mon, Apr 23, 2012 at 7:42 AM, Bernd Dittmann
<bd10stats at googlemail.com> wrote:
> 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.
>

The width, 250, in the example above is larger than the number of rows
in the data.set, 75:

> dim(data.set)
[1] 75  2

Admittedly this should be checked so that you get a better error message.

Also note that rollapplyr is available.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-SIG-Finance mailing list