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

Joshua Wiley jwiley.psych at gmail.com
Sat Mar 5 01:29:08 CET 2011


Hi Will,

Time series are not my strength, but it seems like this is a case
where it is easiest to use rollapply() directly rather than the
wrapper in PerformanceAnalytics.  Here is an example using one of the
provided datasets.  The first element of the output list is created
using regular apply() to go through the columns and apply.rolling(),
the second is made uing just rollapply().

############################
require(PerformanceAnalytics)
data(managers)

f <- function(xIn) {prod(1 + xIn)}

out <- list(AppRoll = apply(managers[,c(1, 3, 4), drop = FALSE], 2,
  FUN = function(x) {apply.rolling(x, FUN= f, width=36)})[36:132, ],
  RollApp = rollapply(data = managers[, c(1,3,4), drop = FALSE],
  width = 36, FUN = f, align = "right"))

head(out$AppRoll)
head(out$RollApp)
############################

Note that rollapply() is in package "zoo".

HTH,

Josh

On Fri, Mar 4, 2011 at 7:47 AM, William Mok <wwl_mok at yahoo.co.uk> wrote:
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list