diffit | R Documentation |
Calculate the row differences of a time series or a matrix using RcppArmadillo.
diffit(tseries, lagg = 1L, pad_zeros = TRUE)
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
|
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
|
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.
A matrix containing the differences between the rows of the
input matrix tseries
.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.