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]]
_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should
go.