roll_sumep | R Documentation |
Calculate the rolling sums at the end points of a time series or a matrix.
roll_sumep(tseries, startp = 0L, endd = 0L, step = 1L, lookb = 1L, stub = 0L)
tseries |
A time series or a matrix. |
startp |
An integer vector of start points (the default
is |
endd |
An integer vector of end points (the default is
|
step |
The number of time periods between the end points (the
default is |
lookb |
The number of end points in the look-back interval
(the default is |
stub |
An integer value equal to the first end point for calculating the end points. |
The function roll_sumep()
calculates the rolling sums at the end
points of the time series tseries
.
The function roll_sumep()
is implemented in RcppArmadillo
C++
code, which makes it several times faster than R
code.
A matrix with the same number of columns as the input time
series tseries
, and the number of rows equal to the number of end
points.
## Not run:
# Calculate historical returns
retp <- na.omit(rutils::etfenv$returns[, c("VTI", "IEF")])
# Define end points at 25 day intervals
endd <- HighFreq::calc_endpoints(NROW(retp), step=25)
# Define start points as 75 day lag of end points
startp <- HighFreq::calc_startpoints(endd, lookb=3)
# Calculate rolling sums using Rcpp
sumc <- HighFreq::roll_sumep(retp, startp=startp, endd=endd)
# Calculate rolling sums using R code
sumr <- sapply(1:NROW(endd), function(ep) {
colSums(retp[(startp[ep]+1):(endd[ep]+1), ])
}) # end sapply
sumr <- t(sumr)
all.equal(sumc, sumr, check.attributes=FALSE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.