lag_vec: Apply a lag to a single-column _time series_ or a _vector_...

View source: R/RcppExports.R

lag_vecR Documentation

Apply a lag to a single-column time series or a vector using RcppArmadillo.

Description

Apply a lag to a single-column time series or a vector using RcppArmadillo.

Usage

lag_vec(timeser, lagg = 1L, pad_zeros = TRUE)

Arguments

timeser

A single-column time series or a vector.

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 lag_vec() applies a lag to the input time series timeser by shifting its elements by the number equal to the argument lagg. For positive lagg values, the elements are shifted forward in time (down), and for negative lagg values they are shifted backward (up).

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

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

Value

A column vector with the same number of elements as the input time series.

Examples

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

## End(Not run)  # end dontrun


algoquant/HighFreq documentation built on June 10, 2025, 3:54 p.m.