Learn R Programming

roll (version 1.1.2)

roll_lm: Rolling Linear Models

Description

A function for computing rolling 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

matrix or xts object. Rows are observations and columns are the independent variables.

y

matrix or xts object. 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 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 r-squareds for each y. An object is the same class as x.

std.error

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

Examples

Run this code
# NOT RUN {
n_vars <- 3
n_obs <- 15
x <- matrix(rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars)
y <- matrix(rnorm(n_obs), nrow = n_obs, ncol = 1)

# rolling regressions
result <- roll_lm(x, y, 5)

# rolling regressions with exponential decay
weights <- 0.9 ^ (5:1)
result <- roll_lm(x, y, 5, weights)
# }

Run the code above in your browser using DataLab