roll_mean | R Documentation |
Calculate a matrix of mean (location) estimates over a rolling look-back interval attached at the end points of a time series or a matrix.
roll_mean(
tseries,
lookb = 1L,
startp = 0L,
endd = 0L,
step = 1L,
stub = 0L,
method = "moment",
confl = 0.75
)
lookb |
The number of end points in the look-back interval
(the default is |
tseries |
A time series or a matrix of data. |
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 |
stub |
An integer value equal to the first end point for
calculating the end points (the default is |
method |
A character string representing the type of mean
measure of (the default is |
The function roll_mean()
calculates a matrix of mean
(location) estimates over rolling look-back intervals attached at the end
points of the time series tseries
.
The function roll_mean()
performs a loop over the end points, and at
each end point it subsets the time series tseries
over a look-back
interval equal to lookb
number of end points.
It passes the subset time series to the function calc_mean()
, which
calculates the mean (location).
See the function calc_mean()
for a description of the mean methods.
If the arguments endd
and startp
are not given then it
first calculates a vector of end points separated by step
time
periods. It calculates the end points along the rows of tseries
using the function calc_endpoints()
, with the number of time
periods between the end points equal to step
time periods.
For example, the rolling mean at 25
day end points, with a
75
day look-back, can be calculated using the parameters
step = 25
and lookb = 3
.
The function roll_mean()
with the parameter step = 1
performs the same calculation as the function roll_mean()
from
package
RcppRoll,
but it's several times faster because it uses C++
RcppArmadillo
code.
The function roll_mean()
is implemented in RcppArmadillo
RcppArmadillo
C++
code, which makes it several times faster
than R
code.
If only a simple rolling mean is required (not the median) then other
functions like roll_sum()
or roll_vec()
may be even faster.
A matrix of mean (location) estimates 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:
# Define time series of returns using package rutils
retp <- na.omit(rutils::etfenv$returns$VTI)
# Calculate the rolling means at 25 day end points, with a 75 day look-back
meanv <- HighFreq::roll_mean(retp, lookb=3, step=25)
# Compare the mean estimates over 11-period look-back intervals
all.equal(HighFreq::roll_mean(retp, lookb=11)[-(1:10), ],
drop(RcppRoll::roll_mean(retp, n=11)), check.attributes=FALSE)
# Define end points and start points
endd <- HighFreq::calc_endpoints(NROW(retp), step=25)
startp <- HighFreq::calc_startpoints(endd, lookb=3)
# Calculate the rolling means using RcppArmadillo
meanv <- HighFreq::roll_mean(retp, startp=startp, endd=endd)
# Calculate the rolling medians using RcppArmadillo
medianscpp <- HighFreq::roll_mean(retp, startp=startp, endd=endd, method="nonparametric")
# Calculate the rolling medians using R
medians = sapply(1:NROW(endd), function(i) {
median(retp[startp[i]:endd[i] + 1])
}) # end sapply
all.equal(medians, drop(medianscpp))
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::roll_mean(retp, startp=startp, endd=endd, method="nonparametric"),
Rcode=sapply(1:NROW(endd), function(i) {median(retp[startp[i]:endd[i] + 1])}),
times=10))[, c(1, 4, 5)]
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.