[R] apply.rolling() to a multi column timeSeries

From: William Mok <wwl_mok_at_yahoo.co.uk>
Date: Fri, 04 Mar 2011 15:47:22 +0000 (GMT)


Hello there,

I am trying to compute the 3 months return momentum with the timeSeries x.ts, which is just a subset of simple returns from a much bigger series,

> class(x.ts)

[1] "timeSeries"
attr(,"package")
[1] "timeSeries"

> dim(x.ts)

[1] 20 3

> x.ts[1:8,]

GMT

                 MS.US      AAPL.US       CA.FP
1996-01-31  0.15159065 -0.133391894  0.10602243
1996-02-29 -0.00692633 -0.004488850  0.03986648
1996-03-29  0.06511157 -0.106763636  0.07930919
1996-04-30 -0.04803468 -0.007653477  0.09490285
1996-05-31  0.08715949  0.071709879  0.05126406
1996-06-28 -0.03586503 -0.196141479   0.01908068
1996-07-31 -0.10941283 0.047619048 -0.04993095 1996-08-30 -0.01720023 0.102363636 -0.06605725

Then, I ran the following,

f <- function(xIn) {prod(1 + xIn)}
tmp.ts <- apply.rolling(x.ts[,, drop = FALSE], FUN=f, width=3) xMom.ts <- tmp.ts - 1

where,

> xMom.ts[1:8,]

GMT

                  calcs
1996-01-31           NA
1996-02-29           NA
1996-03-29  0.218076872
1996-04-30  0.006926330
1996-05-31  0.102324581
1996-06-28 -0.002179951

1996-07-31 -0.066514593
1996-08-30 -0.156122673

It seems that apply.rolling() only executed for the first column "MS.US" but not

column 2 nor 3.

Q: Apart from looping through the column index manually via a for loop, which is

not ideal in R, is there any other way to execute the function for every column

in this setup?

Many thx.

Will

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 04 Mar 2011 - 16:05:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 05 Mar 2011 - 01:50:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive