lagit | R Documentation |
RcppArmadillo
.Apply a lag to the rows of a time series or a matrix using
RcppArmadillo
.
lagit(tseries, lagg = 1L, pad_zeros = TRUE)
tseries |
A time series or a matrix. |
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 lagit()
applies a lag to the input matrix by
shifting its rows by the number equal to the argument lagg
. For
positive lagg
values, the rows are shifted forward (down),
and for negative lagg
values they are shifted backward (up).
The output matrix is padded with either zeros (the default), or
with rows of data from tseries
, so that it has the same dimensions
as tseries
.
If the lagg
is positive, then the first row is copied and added
upfront.
If the lagg
is negative, then the last row 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 matrix with the same dimensions as the input argument
tseries
.
## Not run:
# Create a matrix of random returns
retp <- matrix(rnorm(5e6), nc=5)
# Compare lagit() with rutils::lagit()
all.equal(HighFreq::lagit(retp), rutils::lagit(retp))
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::lagit(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.