diffit: Calculate the row differences of a _time series_ or a...

View source: R/RcppExports.R

diffitR Documentation

Calculate the row differences of a time series or a matrix using RcppArmadillo.

Description

Calculate the row differences of a time series or a matrix using RcppArmadillo.

Usage

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

Arguments

tseries

A time series or a matrix.

lagg

An integer equal to the number of rows (time periods) to lag when calculating the differences (the default is lagg = 1).

pad_zeros

Boolean argument: Should the output matrix be padded (extended) with zero values, in order to return a matrix with the same number of rows as the input? (the default is pad_zeros = TRUE)

Details

The function diffit() calculates the differences between the rows of the input matrix tseries and its lagged version.

The argument lagg specifies the number of lags applied to the rows of the lagged version of tseries. For positive lagg values, the lagged version of tseries has its rows shifted forward (down) by the number equal to lagg rows. For negative lagg values, the lagged version of tseries has its rows shifted backward (up) by the number equal to -lagg rows. For example, if lagg=3 then the lagged version will have its rows shifted down by 3 rows, and the differences will be taken between each row minus the row three time periods before it (in the past). The default is lagg = 1.

The argument pad_zeros specifies whether the output matrix should be padded (extended) with zero values in order to return a matrix with the same number of rows as the input tseries. The default is pad_zeros = TRUE. If pad_zeros = FALSE then the return matrix has a smaller number of rows than the input tseries. The padding operation can be time-consuming, because it requires the copying the data in memory.

The function diffit() is implemented in RcppArmadillo C++ code, which makes it several times faster than R code.

Value

A matrix containing the differences between the rows of the input matrix tseries.

Examples

## Not run: 
# Create a matrix of random data
datav <- matrix(sample(15), nc=3)
# Calculate differences with lagged rows
HighFreq::diffit(datav, lagg=2)
# Calculate differences with advanced rows
HighFreq::diffit(datav, lagg=-2)
# Compare HighFreq::diffit() with rutils::diffit()
all.equal(HighFreq::diffit(datav, lagg=2), 
  rutils::diffit(datav, lagg=2), 
  check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::diffit(datav, lagg=2),
  Rcode=rutils::diffit(datav, lagg=2),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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