lag_vec | R Documentation |
RcppArmadillo
.Apply a lag to a single-column time series or a vector
using RcppArmadillo
.
lag_vec(tseries, lagg = 1L, pad_zeros = TRUE)
tseries |
A single-column time series or a vector. |
lagg |
An integer equal to the number of periods to lag.
(The default is |
pad_zeros |
Boolean argument: Should the output be padded
with zeros? (The default is |
The function lag_vec()
applies a lag to the input time
series tseries
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 tseries
, so that it has the same number of element
as tseries
.
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 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.
A column vector with the same number of elements as the input time series.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.