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

View source: R/rutils.R

lagxtsR Documentation

Apply a time lag to an xts time series.

Description

Apply a time lag to an xts time series.

Usage

lagxts(xtsv, lagg = 1, pad_zeros = TRUE, ...)

Arguments

xtsv

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::lagxts().

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 lagxts() 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 lagxts() 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 lagit() has incorporated the functionality of lagxts(), so that lagxts() will be retired in future package versions.

Value

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

Examples

# Lag by 10 periods
rutils::lagxts(rutils::etfenv$VTI, lag=10)

algoquant/rutils documentation built on April 18, 2024, 12:05 a.m.