base-apply: Apply functions over time windows

applyR Documentation

Apply functions over time windows

Description

Applies a function to a "timeSeries" object over regular or irregular time windows, possibly overlapping.

Usage

## S4 method for signature 'timeSeries'
apply(X, MARGIN, FUN, ..., simplify = TRUE)

fapply(x, from, to, FUN, ...)

applySeries(x, from = NULL, to = NULL, by = c("monthly", "quarterly"), 
            FUN = colMeans, units = NULL, format = x@format, 
            zone = x@FinCenter, FinCenter = x@FinCenter, 
            recordIDs = data.frame(), title = x@title, 
            documentation = x@documentation, ...)

rollDailySeries(x, period = "7d", FUN, ...)

Arguments

x,X

an object of class timeSeries.

MARGIN

a vector giving the subscripts which the function will be applied over, see base R's apply.

FUN

the function to be applied. For the function applySeries the default setting is FUN = colMeans.

simplify

simplify the result?

from, to

starting date and end date as "timeDate" objects. Note, to must be time ordered after from. If from and to are missing in function fapply they are set by default to from=start(x), and to=end(x).

by

a character value either "monthly" or "quarterly" used in the function applySeries. The default value is "monthly". Only operative when both arguments from and to have their default values NULL. In this case the function FUN will be applied to monthly or quarterly periods.

units

an optional character string, which allows to overwrite the current column names of a timeSeries object. By default NULL which means that the column names are selected automatically.

format

the format specification of the input character vector in POSIX notation.

zone

the time zone or financial center where the data were recorded.

FinCenter

a character value with the the location of the financial center named as "continent/city", or "city".

recordIDs

a data frame which can be used for record identification information. Note, this is not yet handled by the apply functions, an empty data.frame will be returned.

title

an optional title string, if not specified the input's data name is deparsed.

documentation

optional documentation string, or a vector of character strings.

period

a character string specifying the rollling period composed by the length of the period and its unit, e.g. "7d" represents one week.

...

arguments passed to other methods.

Details

The "timeSeries" method for apply extracts the core data (a matrix) from X and calls apply, passing on all the remaining arguments. If the result is suitable, it converts it to "timeSeries", otherwise returns it as is. ‘Suitable’ here means that it is a matrix or a vector (which is converted to a matrix) and the number of observations is the same as X.

Like apply applies a function to the margins of an array, the function fapply applies a function to the time stamps or signal counts of a financial (therefore the “f” in front of the function name) time series of class "timeSeries".

applySeries takes a "timeSeries" object as input and applies FUN to windows of x. The windows are specified by from and to, which need to have the same length. Then from[i], to[i] specifies the i-th window. If time(x) is a "timeDate" object, then from and to are converted to "timeDate" (if they are not already such objects), otherwise they are converted to integers.

An alternative way to specify the window(s) on which applySeries operates is with argument by. It is used only if from and to are missing or NULL. by = "monthly" or by = "quarterly" applies FUN to the data for each year-month or year-quarter, respectively. By year-month we mean that there are separate windows for the months in different years.

The resulting time stamps are the time stamps of the to vector. The periods can be regular or irregular, and they can even overlap.

If from = start(x) and to = end(x), then the function behaves like apply on the column margin.

fapply is the same as applySeries (in fact, the former calls the latter), except that the defaults for from and to are start(x) and end(x), respectively. (GNB: in addition, fapply throws error if x is a ‘signal series’.)

rollDailySeries rolls a daily 'timeSeries' on a given period.

Value

for rollDailySeries, an object of class "timeSeries" with rolling values, computed from the function FUN.

Examples

## Percentual Returns of Swiss Bond Index and Performance Index - 
   LPP <- 100 * LPP2005REC[, c("SBI", "SPI")]
   head(LPP, 20)
   
## Aggregate Quarterly Returns -
   applySeries(LPP, by = "quarterly", FUN = colSums)
   
## Aggregate Quarterly every last Friday in Quarter -
   oneDay <- 24*3600
   from <- unique(timeFirstDayInQuarter(time(LPP))) - oneDay
   from <- timeLastNdayInMonth(from, nday = 5)
   to <- unique(timeLastDayInQuarter(time(LPP)))
   to <- timeLastNdayInMonth(to, nday = 5)
   data.frame(from = as.character(from), to = as.character(to))

   applySeries(LPP, from, to, FUN = colSums)
## Alternative Use - 
   fapply(LPP, from, to, FUN = colSums)
   
## Count Trading Days per Month - 
   colCounts <- function(x) rep(NROW(x), times = NCOL(x))
   applySeries(LPP, FUN = colCounts, by = "monthly")


## TODO: examples for rollDailySeries()

timeSeries documentation built on Aug. 26, 2023, 5:08 p.m.