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]]
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/
On Fri, Mar 4, 2011 at 10: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? >The rollapply function in zoo works by column by default: Lines <- "Date 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" library(zoo) z <- read.zoo(textConnection(Lines), header = TRUE) f <- function(xIn) prod(1 + xIn) rollapply(z, 3, f, na.pad = TRUE, align = "right") The last line produces:> rollapply(z, 3, f, na.pad = TRUE, align = "right")MS.US AAPL.US CA.FP 1996-01-31 NA NA NA 1996-02-29 NA NA NA 1996-03-29 1.2180769 0.7706111 1.2413304 1996-04-30 1.0069263 0.8824211 1.2288505 1996-05-31 1.1023246 0.9499636 1.2423194 1996-06-28 0.9978200 0.8549096 1.1729945 1996-07-31 0.9334854 0.9025271 1.0178307 1996-08-30 0.8438773 0.9283418 0.9042406 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com