diff_vec | R Documentation |
Calculate the differences between the neighboring elements of a single-column time series or a vector.
diff_vec(tseries, lagg = 1L, pad_zeros = TRUE)
tseries |
A single-column time series or a vector. |
lagg |
An integer equal to the number of time periods to
lag when calculating the differences (the default is |
pad_zeros |
Boolean argument: Should the output
vector be padded (extended) with zeros, in order to return a
vector of the same length as the input? (the default is
|
The function diff_vec()
calculates the differences between the
input time series or vector and its lagged version.
The argument lagg
specifies the number of lags. For example, if
lagg=3
then the differences will be taken between each element
minus the element three time periods before it (in the past). The default
is lagg = 1
.
The argument pad_zeros
specifies whether the output vector
should be padded (extended) with zeros at the front, in order to
return a vector of the same length as the input. The default is
pad_zeros = TRUE
. The padding operation can be time-consuming,
because it requires the copying the data in memory.
The function diff_vec()
is implemented in RcppArmadillo
C++
code, which makes it several times faster than R
code.
A column vector containing the differences between the elements of the input vector.
## Not run:
# Create a vector of random returns
retp <- rnorm(1e6)
# Compare diff_vec() with rutils::diffit()
all.equal(drop(HighFreq::diff_vec(retp, lagg=3, pad=TRUE)),
rutils::diffit(retp, lagg=3))
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::diff_vec(retp, lagg=3, pad=TRUE),
Rcode=rutils::diffit(retp, lagg=3),
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.