roll_reg: Perform a rolling regression and calculate a matrix of...

View source: R/RcppExports.R

roll_regR Documentation

Perform a rolling regression and calculate a matrix of regression coefficients, their t-values, and z-scores.

Description

Perform a rolling regression and calculate a matrix of regression coefficients, their t-values, and z-scores.

Usage

roll_reg(
  respv,
  predm,
  controlv,
  startp = 0L,
  endd = 0L,
  step = 1L,
  lookb = 1L,
  stub = 0L
)

Arguments

respv

A single-column time series or a vector of response data.

predm

A time series or a matrix of predictor data.

controlv

A list of model parameters (see Details).

startp

An integer vector of start points (the default is startp = 0).

endd

An integer vector of end points (the default is endd = 0).

step

The number of time periods between the end points (the default is step = 1).

lookb

The number of end points in the look-back interval (the default is lookb = 1).

stub

An integer value equal to the first end point for calculating the end points (the default is stub = 0).

Details

The function roll_reg() performs a rolling regression over the end points of the predictor matrix, and calculates a matrix of regression coefficients, their t-values, and z-scores.

The function roll_reg() 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.

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 regression at 25 day end points, with a 75 day look-back, can be calculated using the parameters step = 25 and lookb = 3.

It passes the subset time series to the function calc_reg(), which calculates the regression coefficients, their t-values, and the z-score. The function roll_reg() accepts a list of model parameters through the argument controlv, and passes it to the function calc_reg(). The list of model parameters can be created using the function param_reg(). See the function param_reg() for a description of the model parameters.

The number of columns of the return matrix depends on the number of columns of the predictor matrix (including the intercept column, if it's been added in R). The number of regression coefficients is equal to the number of columns of the predictor matrix. If the predictor matrix contains an intercept column then the first regression coefficient is equal to the intercept value \alpha.

The number of columns of the return matrix is equal to the number of regression coefficients, plus their t-values, plus the z-score column. The number of t-values is equal to the number of coefficients. If the number of columns of the predictor matrix is equal to n, then roll_reg() returns a matrix with 2n+1 columns: n regression coefficients, n corresponding t-values, and 1 z-score column.

Value

A matrix with the regression coefficients, their t-values, and z-scores, and with the same number of rows as predm a number of columns equal to 2n+1, where n is the number of columns of predm.

Examples

## Not run: 
# Calculate historical returns
predm <- na.omit(rutils::etfenv$returns[, c("XLP", "VTI")])
# Add unit intercept column to the predictor matrix
predm <- cbind(rep(1, NROW(predm)), predm)
# Define monthly end points and start points
endd <- xts::endpoints(predm, on="months")[-1]
lookb <- 12
startp <- c(rep(1, lookb), endd[1:(NROW(endd)-lookb)])
# Create a default list of regression parameters
controlv <- HighFreq::param_reg()
# Calculate rolling betas using RcppArmadillo
regroll <- HighFreq::roll_reg(respv=predm[, 2], predm=predm[, -2], endd=(endd-1), startp=(startp-1), controlv=controlv)
betas <- regroll[, 2]
# Calculate rolling betas in R
betar <- sapply(1:NROW(endd), FUN=function(ep) {
  datav <- predm[startp[ep]:endd[ep], ]
  # HighFreq::calc_reg(datav[, 2], datav[, -2], controlv)
  drop(cov(datav[, 2], datav[, 3])/var(datav[, 3]))
})  # end sapply
# Compare the outputs of both functions
all.equal(betas, betar, check.attributes=FALSE)

## End(Not run)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.