roll_lm: Rolling Linear Models

View source: R/roll.R

roll_lmR Documentation

Rolling Linear Models

Description

A function for computing the rolling and expanding linear models of time-series data.

Usage

roll_lm(x, y, width, weights = rep(1, width), intercept = TRUE,
  min_obs = width, complete_obs = TRUE, na_restore = FALSE,
  online = TRUE)

Arguments

x

vector or matrix. Rows are observations and columns are the independent variables.

y

vector or matrix. Rows are observations and columns are the dependent variables.

width

integer. Window size.

weights

vector. Weights for each observation within a window.

intercept

logical. Either TRUE to include or FALSE to remove the intercept.

min_obs

integer. Minimum number of observations required to have a value within a window, otherwise result is NA.

complete_obs

logical. If TRUE then rows containing any missing values are removed, if FALSE then pairwise is used.

na_restore

logical. Should missing values be restored?

online

logical. Process observations using an online algorithm.

Value

A list containing the following components:

coefficients

A list of objects with the rolling and expanding coefficients for each y. An object is the same class and dimension (with an added column for the intercept) as x.

r.squared

A list of objects with the rolling and expanding r-squareds for each y. An object is the same class as x.

std.error

A list of objects with the rolling and expanding standard errors for each y. An object is the same class and dimension (with an added column for the intercept) as x.

Examples

n <- 15
x <- rnorm(n)
y <- rnorm(n)
weights <- 0.9 ^ (n:1)

# rolling regressions with complete windows
roll_lm(x, y, width = 5)

# rolling regressions with partial windows
roll_lm(x, y, width = 5, min_obs = 1)

# expanding regressions with partial windows
roll_lm(x, y, width = n, min_obs = 1)

# expanding regressions with partial windows and weights
roll_lm(x, y, width = n, min_obs = 1, weights = weights)

roll documentation built on May 29, 2024, 6:02 a.m.