base-apply: Apply Functions Over Time Series Periods

Description Usage Arguments Details Examples

Description

Applies a function to a 'timeSeries' object over time peridos of arbitrary positons and lengths.

Usage

1
2
3
4
5
6
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, ...)

Arguments

x

an object of class timeSeries.

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).

FUN

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

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 inputs data name is deparsed.

documentation

optional documentation string, or a vector of character strings.

...

arguments passed to other methods.

Details

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'.

The function fapply inputs a timeSeries object, and if from 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 sense: If from and 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 to vector. Note, the periods can be regular or irregelar, and they can even overlap.

The function fapply calls the more general function applySeries which also offers, to create automatical monthly and quarterly periods.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 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)

Example output

Loading required package: timeDate
GMT
                  SBI        SPI
2005-11-01 -0.0612745  0.8414595
2005-11-02 -0.2762009  0.2519342
2005-11-03 -0.1153092  1.2707292
2005-11-04 -0.3235750 -0.0702757
2005-11-07  0.1310970  0.6205226
2005-11-08  0.0539312  0.0329260
2005-11-09 -0.2545021 -0.2378200
2005-11-10  0.1003358  0.0922087
2005-11-11  0.0616951  1.3334906
2005-11-14  0.0693615 -0.4693064
2005-11-15  0.0154071  0.1266865
2005-11-16  0.2999656 -0.7187498
2005-11-17 -0.1306436  0.7658103
2005-11-18 -0.2232573  1.2527202
2005-11-21  0.1155402  0.2659666
2005-11-22 -0.0230974  0.2142494
2005-11-23  0.0692761  0.3567128
2005-11-24  0.2075407 -0.2559544
2005-11-25 -0.0614487  0.3374818
2005-11-28 -0.0307385 -0.9816739
GMT
                  SBI       SPI
2005-12-31  0.5193189  7.295300
2006-03-31 -1.6590275  7.074801
2006-06-30 -1.1685058 -3.428832
2006-09-30  2.7589662  9.780371
2006-12-31 -0.0076177  5.359906
2007-03-31 -0.2595182  3.604943
2007-06-30 -0.1682858  2.047652
        from         to
1 2005-09-30 2006-01-06
2 2006-01-06 2006-03-31
3 2006-03-31 2006-06-30
4 2006-06-30 2006-10-06
5 2006-10-06 2007-01-05
6 2007-01-05 2007-04-06
7 2007-04-06 2007-07-06
GMT
                  SBI        SPI
2006-01-06  0.4049956  9.1654132
2006-03-31 -1.5828270  5.4274690
2006-06-30 -1.1452689 -3.5217575
2006-10-06  2.7803272 12.2731914
2007-01-05 -0.2667938  5.6541189
2007-04-06 -0.5571005  3.6277826
2007-07-06 -0.0382716  0.5285255
GMT
           SBI SPI
2005-11-30  22  22
2005-12-31  22  22
2006-01-31  22  22
2006-02-28  20  20
2006-03-31  23  23
2006-04-30  20  20
2006-05-31  23  23
2006-06-30  22  22
2006-07-31  21  21
2006-08-31  23  23
2006-09-30  21  21
2006-10-31  22  22
2006-11-30  22  22
2006-12-31  21  21
2007-01-31  23  23
2007-02-28  20  20
2007-03-31  22  22
2007-04-30   8   8
GMT
                  SBI        SPI
2006-01-06  0.4049956  9.1654132
2006-03-31 -1.5828270  5.4274690
2006-06-30 -1.1452689 -3.5217575
2006-10-06  2.7803272 12.2731914
2007-01-05 -0.2667938  5.6541189
2007-04-06 -0.5571005  3.6277826
2007-07-06 -0.0382716  0.5285255

timeSeries documentation built on Jan. 25, 2020, 1:07 a.m.