Apply Functions Over Time Series Periods
Applies a function to a 'timeSeries' object over time peridos of arbitrary positons and lengths.
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, ...)
an object of class
- from, to
starting date and end date as timeDate objects. Note,
tomust be time ordered after
toare missing in function
fapplythey are set by default to
the function to be applied. For the function
applySeriesthe default setting is
a character value either
"quarterly"used in the function
applySeries. The default value is
"monthly". Only operative when both arguments
tohave their default values
NULL. In this case the function
FUNwill be applied to monthly or quarterly periods.
an optional character string, which allows to overwrite the
current column names of a
timeSeriesobject. By default
NULLwhich means that the column names are selected automatically.
- the format specification of the input character vector in POSIX notation.
- the time zone or financial center where the data were recorded.
- a character value with the the location of the financial center named as "continent/city", or "city".
- 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.
- an optional title string, if not specified the inputs data name is deparsed.
- optional documentation string, or a vector of character strings.
- arguments passed to other methods.
apply applies a function to the margins of an array, the
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
fapply inputs a
timeSeries object, and
to are missing, they take the start
and end time stamps of the series as default falues. The function
then behaves like
apply on the column margin.
Note, the function
fapply can be used repetitive in the following
to are two
timeDate vectors of
equal length then for each period spanned by the elelemts of the
two vectors the function
FUN will be applied to each period.
The resulting time stamps, are the time stamps of the
vector. Note, the periods can be regular or irregelar, and they can
fapply calls the more general function
applySeries which also offers, to create automatical monthly
and quarterly periods.
## 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) ## Count Trading Days per Month - colCounts <- function(x) rep(NROW(x), times = NCOL(x)) applySeries(LPP, FUN = colCounts, by = "monthly") ## Alternative Use - fapply(LPP, from, to, FUN = colSums)