roll_zscores | R Documentation |
Calculate a vector of z-scores of the residuals of rolling regressions at the end points of the predictor matrix.
roll_zscores(
respv,
predm,
startp = 0L,
endd = 0L,
step = 1L,
lookb = 1L,
stub = 0L
)
respv |
A single-column time series or a vector of response data. |
predm |
A time series or a matrix of predictor 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 |
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 default is |
The function roll_zscores()
calculates a vector of z-scores
of the residuals of rolling regressions at the end points of the
time series predm
.
The function roll_zscores()
performs a loop over the end points,
and at each end point it subsets the time series predm
over a
look-back interval equal to lookb
number of end points.
It passes the subset time series to the function calc_lm()
, which
calculates the regression data.
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 predm
using the function calc_endpoints()
, with the number of time
periods between the end points equal to step
time periods.
For example, the rolling variance at 25
day end points, with a
75
day look-back, can be calculated using the parameters
step = 25
and lookb = 3
.
A column vector of the same length as the number of rows of
predm
.
## Not run:
# Calculate historical returns
retp <- na.omit(rutils::etfenv$returns[, c("XLF", "VTI", "IEF")])
# Response equals XLF returns
respv <- retp[, 1]
# Predictor matrix equals VTI and IEF returns
predm <- retp[, -1]
# Calculate Z-scores from rolling time series regression using RcppArmadillo
lookb <- 11
zscores <- HighFreq::roll_zscores(respv=respv, predm=predm, lookb)
# Calculate z-scores in R from rolling multivariate regression using lm()
zscoresr <- sapply(1:NROW(predm), function(ro_w) {
if (ro_w == 1) return(0)
startpoint <- max(1, ro_w-lookb+1)
responsi <- response[startpoint:ro_w]
predicti <- predictor[startpoint:ro_w, ]
regmod <- lm(responsi ~ predicti)
residuals <- regmod$residuals
residuals[NROW(residuals)]/sd(residuals)
}) # end sapply
# Compare the outputs of both functions
all.equal(zscores[-(1:lookb)], zscoresr[-(1:lookb)],
check.attributes=FALSE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.