lag_xts: Apply a time lag to an _xts_ time series.

Description Usage Arguments Details Value Examples

View source: R/rutils.R

Description

Apply a time lag to an xts time series.

Usage

1
lag_xts(x_ts, lagg = 1, pad_zeros = TRUE, ...)

Arguments

x_ts

An xts time series.

lagg

An integer equal to the number of time periods of lag (default is 1).

pad_zeros

A Boolean argument: Should the output be padded with zeros? (The default is pad_zeros = TRUE.)

...

Additional arguments to function xts::lag_xts().

Details

Applies a time lag to an xts time series and pads with the first and last elements instead of NAs.

A positive lag argument lagg means elements from lagg periods in the past are moved to the present. A negative lag argument lagg moves elements from the future to the present. If lagg = 0, then lag_xts() returns the input time series unchanged.

To avoid leading or trailing NA values, the output xts is padded with zeroes, or with elements from either the first or the last row.

For the lag of asset returns, they should be padded with zeros, to avoid look-ahead bias. For the lag of prices, they should be padded with the first or last prices, not with zeros.

The function lag_xts() is just a wrapper for function lag.xts() from package xts, but it pads with the first and last elements instead of NAs.

The function lag_it() has incorporated the functionality of lag_xts(), so that lag_xts() will be retired in future package versions.

Value

An xts time series with the same dimensions and the same time index as the input x_ts time series.

Examples

1
2
# Lag by 10 periods
rutils::lag_xts(rutils::etf_env$VTI, lag=10)

algoquant/rutils documentation built on Feb. 26, 2021, 1:30 a.m.