R365: Day 47 – fapply() in {timeSeries}

I need to be able to average out a value over a set time period. There are a couple of options, I mostly was considering period.apply() in {xts} because most of my data is in xts format. However the indexing for period.apply() seems to not be super flexible. While puttering around stackoverflow articles, I ran into this site that described basically all I needed to do.

fapply() sets a time series, a beginning and end date, and a function to apply. Bam. Exactly what I needed.

I edited some of the base example code to fit my needs:

library(timeSeries)
 library(ggplot2)
 ## Percentual Returns of Swiss Bond Index and Performance Index -
 LPP <- 100 * LPP2005REC[, c("SBI", "SPI")]
 head(LPP, 20)
## Aggregate Quarterly Returns -
##plot(applySeries(LPP, by = "monthly", FUN = colSums), main= "SBI and SPI Index Over Time")

Rplot
Next I tried to create my own time series and use the flexibility of the function

> x=timeSeries(runif(100), seq(Sys.Date(), length.out = 100, by = "days"))
> plot(x)
Rplot01
> fapply(x,from=Sys.timeDate()+10,to=Sys.timeDate()+15, FUN = mean)

 TS.1
[1,] 0.6262633

Error in applySeries(x = x, from = from, to = to, FUN = FUN, ...) : 
 outsite of range

What is going on here? After looking through Google for about 20 minutes with the aggravating misspelling of “outsite of range”, I could not find a solution. Eventually I saw lower down in the package vignette that dates were quoted, made into strings. Note also that dates have to be in “standard unambiguous format”: Year-Month-Day. And so…

> x=timeSeries(runif(100), seq(Sys.Date(), length.out = 100, by = "days"))
> plot(x)
> fapply(x,Sys.timeDate()+10,to=Sys.timeDate()+15, FUN = mean) 
TS.1 [1,] 0.6262633

After working with the code a bit further, I wanted to be able to get the data from multiple time intervals without having to make a bunch of different calls. Luckily, fapply() is pretty flexible and can be coerced into accepting multiple calls for multiple rows:

LPP <- 100 * LPP2005REC[, c("SBI", "SPI")]
from=c("2005-11-11","2005-11-17")
to = c("2005-12-11","2005-12-17")
fapply(LPP,from=from,to=to, FUN = colMeans)
GMT
              SBI         SPI
2005-12-11 0.032217205 0.1924305
2005-12-17 0.008718791 0.1292479

But what if we just want to know the mean from exactly a week later after the start date? Its annoying to re-enter the dates over and over again. Saying

LPP <- 100 * LPP2005REC[, c("SBI", "SPI")]
from=c("2005-11-11","2005-11-17")
to = from + 7
fapply(LPP,from=from,to=to, FUN = colMeans)

Gives us the error

Error in from + 7 : non-numeric argument to binary operator

BUT, if we adjust the from to say as.Date() we get …

LPP <- 100 * LPP2005REC[, c("SBI", "SPI")]
from=as.Date(c("2005-11-11","2005-11-17"))
to = from + 7
fapply(LPP,from=from,to=to, FUN = colMeans)
GMT
 SBI SPI
2005-11-18 0.015421400 0.3817752
2005-11-24 0.002559783 0.4332508
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s