roll_regres | R Documentation |
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.
roll_regres(formula, data, width, contrasts = NULL, do_compute = character(), grp = NULL, do_downdates = TRUE, min_obs = NULL)
formula |
as |
data |
an optional |
width |
integer with the width of the moving window. Only used if
|
contrasts |
list passed to |
do_compute |
character vector with elements |
grp |
integer vector to be used if you e.g., want to run the regression
over weekly blocks of data. See "Details" in |
do_downdates |
logical which is |
min_obs |
positive integer with minimum number of observation that are
required in a window. Useful if there are gaps in |
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.
List with vector and matrices with the computed output. See the
do_compute
argument.
roll_regres.fit
for method that avoids the call to
e.g., model.frame
.
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.