roll_regres: Fitting Rolling and Expanding Linear Models

View source: R/roll_regres.R

roll_regresR Documentation

Fitting Rolling and Expanding Linear Models

Description

Method for fast rolling and expanding regression models. I.e., linear models estimated over a moving window or expanding window of data. The function assumes that data is ordered.

Usage

roll_regres(formula, data, width, contrasts = NULL,
  do_compute = character(), grp = NULL, do_downdates = TRUE,
  min_obs = NULL)

Arguments

formula

as formula in lm.

data

an optional data.frame containing the variables in the model.

width

integer with the width of the moving window. Only used if do_downdates == TRUE.

contrasts

list passed to model.matrix.defaults contrasts.arg argument.

do_compute

character vector with elements "sigmas", "r.squareds", and/or "1_step_forecasts" for additional output to be computed. See "Details" in roll_regres.

grp

integer vector to be used if you e.g., want to run the regression over weekly blocks of data. See "Details" in roll_regres.

do_downdates

logical which is TRUE if you want a rolling window regressions. Otherwise, an expanding window is used.

min_obs

positive integer with minimum number of observation that are required in a window. Useful if there are gaps in grp or unequal number of observations for each grp.

Details

do_compute can contain "sigmas" if you want the estimated standard deviation of the residuals, "r.squareds" for the R^2 of the models, and "1_step_forecasts" for the out-of-sample forecast for the next periods value.

grp is a sorted integer vector if you want to make "block" updates. E.g., grp could be an integer vector with the week number. The width argument is relative to the grp argument if the grp argument is not NULL. The indices of grp should match with the other data objects.

See vignette("Comparisons", package = "rollRegres") for further examples.

Value

List with vector and matrices with the computed output. See the do_compute argument.

See Also

roll_regres.fit for method that avoids the call to e.g., model.frame.

Examples

# simulate data
set.seed(29132867)
n <- 50
p <- 2
X <- cbind(1, matrix(rnorm(p * n), ncol = p))
y <- drop(X %*% c(1, -1, 1)) + rnorm(n)
df <- data.frame(y, X[, -1])

# compute coefs
out <- roll_regres(y ~ X1 + X2, df, width = 45L)
tail(out$coefs)

# compute more output
out <- roll_regres(
 y ~ X1 + X2, df, width = 45L,
 do_compute = c("sigmas", "r.squareds", "1_step_forecasts"))
lapply(out, tail)


boennecd/rollRegres documentation built on May 11, 2022, 4:13 p.m.