lagit: Apply a lag to the rows of a _time series_ or a _matrix_...

View source: R/RcppExports.R

lagitR Documentation

Apply a lag to the rows of a time series or a matrix using RcppArmadillo.

Description

Apply a lag to the rows of a time series or a matrix using RcppArmadillo.

Usage

lagit(tseries, lagg = 1L, pad_zeros = TRUE)

Arguments

tseries

A time series or a matrix.

lagg

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

pad_zeros

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

Details

The function lagit() applies a lag to the input matrix by shifting its rows by the number equal to the argument lagg. For positive lagg values, the rows are shifted forward (down), and for negative lagg values they are shifted backward (up).

The output matrix is padded with either zeros (the default), or with rows of data from tseries, so that it has the same dimensions as tseries. If the lagg is positive, then the first row is copied and added upfront. If the lagg is negative, then the last row is copied and added to the end.

As a rule, if tseries contains returns data, then the output matrix should be padded with zeros, to avoid data snooping. If tseries contains prices, then the output matrix should be padded with the prices.

Value

A matrix with the same dimensions as the input argument tseries.

Examples

## Not run: 
# Create a matrix of random returns
retp <- matrix(rnorm(5e6), nc=5)
# Compare lagit() with rutils::lagit()
all.equal(HighFreq::lagit(retp), rutils::lagit(retp))
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::lagit(retp),
  Rcode=rutils::lagit(retp),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.