toPeriod: Convert a Date time series to a series of another frequency

Description Usage Arguments Value Examples

View source: R/toPeriod.R

Description

This finds the End of Periods (EOP)s. This is a (very) smart wrapper over seq.POSIXt, R CRAN Packages timeDate and RQuantLib.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
toPeriod(
  x,
  Period = "months",
  k = 1L,
  PeriodStart = NULL,
  NthNdayInMonth = NULL,
  NextWeek = F,
  FunEach = identity,
  FunAll = NC,
  KeepOrigValues = F,
  fillInterior = F,
  fillInteriorBy = NULL,
  Calendar = "UnitedStates/GovernmentBond",
  BusDayConv = 0L,
  alignTime = T
)

Arguments

x

xts object

Period

Period to convert to. Default is "months". See ? seq.POSIXt: "secs", "mins", "hours", "days", "weeks", "months", "quarters" or "years" This is the aggregation (summary).

k

Integer. Default is 1L. Number of k Periods.

PeriodStart

Date time. Default is NULL. Required. Date time of the beginning of the series. Must be convertible by S3 to a POSIXct by as.POSIXct(PeriodEnd). The user must be sure that his value is less than or equal to the earliest index value of x.

NthNdayInMonth

List. Default is NULL. The structure is list(nday = #nday, nth = #nth). From the PeriodStart (and going forward), make the End of Period (EOP) to be the nday(Mon through Sun : 1L through 7L ) that is the nth (1L through (4L or 5L)).

NextWeek

Logical. Default is FALSE. If TRUE, If the End of Period(EOP) is a Holiday(means Weekend or festive Holiday), then make the EOP to be "next week" on the same day. STILL TESTING.

FunEach

Function. Has one argument: x; that is the xts object of the period. Default is identity. Function to be applied per period. The value at the, per period, "last" positions is the "end of period"(EOP).

FunAll

Function. Default is econModel::NC. Has two argements: x; that is the xts object of all periods; EOPIndex is the index of EOP index values. This function is to be applied across all periods.

KeepOrigValues

Logical. Default is FALSE. Only the New Generated elements are kept and the original xts(x) elements are discarded from the output. Otherwise, the original xts(x) elements are kept (and the New Generated elements are kept).

fillInterior

Logical. Default is FALSE. Created sub(or super)-Period data points.

fillInteriorBy

String. Default is NULL. If fillInterior is TRUE, then this value must be provided. See seq.POSIXt: "secs", "mins", "hours", "days", "weeks", "months", "quarters" or "years". Note, this is applied JUST BEFORE the function FunAll is called.

Calendar

Default is "UnitedStates/GovernmentBond". Calendar to use. See the Details section of ?? RQuantLib::Calendars.

BusDayConv

Integer. Default is 0L. See https://www.quantlib.org/reference/group__datetime.html See ? RQuantLib::Enum, ? RQuantLib::adjust, and the parameter bcd(Business Day Convention). 0L means if the Day falls on a Holiday (Holiday includes weekends), then Following: the first business day after the given holiday becomes the (new) adjusted date.

alignTime

Logical. Default is TRUE. The EOP is slightly early. Put the time on the exact time data point.

Value

modified xts object

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Not run: 
x <- xts::xts(c(3,363), zoo::as.Date(c(3,363)))
toPeriod(x, Period = "weeks", PeriodStart = zoo::as.Date(2))
toPeriod(x, Period = "weeks", k = 2L, PeriodStart = zoo::as.Date(2))
toPeriod(x, Period = "weeks", k = 2L, PeriodStart = zoo::as.Date(2),
  fillInterior = T, fillInteriorBy = "days")
toPeriod(x, Period = "months", k = 2L, PeriodStart = zoo::as.Date(2))
toPeriod(x, Period = "quarters", k = 2L, PeriodStart = zoo::as.Date(-20))

# E.g. "Stock Options Expiry Date"
toPeriod(x, Period = "months", PeriodStart = zoo::as.Date(0),  NthNdayInMonth = list(nday = 5, nth = 3))

# U.S. Bureau of Labor Statistics Unemployment Report
# Reports on the 1st Friday of the month, unless that Friday is a Holiday,
#   in that case, then reports on the Next Friday
# FRI JAN 01 1971 is a Holiday, so reports the next Friday, FRI JAN 08 1971
toPeriod(x, Period = "months", PeriodStart = zoo::as.Date(0), NthNdayInMonth = list(nday = 5, nth = 1), NextWeek = T)

## End(Not run)

AndreMikulec/econModel documentation built on June 30, 2021, 9:48 a.m.