Package 'rolleigen'

Title: Rolling Eigenanalysis
Description: Fast and efficient computation of rolling and expanding eigenanalysis for time-series data. The 'rolleigen' package decomposes the covariance matrix of the explanatory variables into eigenvalues and eigenvectors to perform principal component analysis (Pearson, 1901, <doi:10.1080/14786440109462720>; Hotelling, 1933, <doi:10.1037/h0071325>) and principal component regression (Massy, 1965, <doi:10.1080/01621459.1965.10480787>) over rolling and expanding windows. For each window, the eigenvalues and eigenvectors are computed from the covariance matrix and, optionally, matched to the previous window to track the directions of greatest variation in the data. A subset of leading components is then used to fit a regression that mitigates collinearity in the explanatory variables. Use cases include dimensionality reduction, factor extraction, and regression on collinear explanatory variables. The package supports rolling and expanding windows, weights, and handling of missing values via the min_obs, complete_obs, and na_restore arguments. The implementation uses the online and offline algorithms from the 'roll' package to compute rolling and expanding cross-products efficiently, with parallelism across columns and windows provided by 'RcppParallel'.
Authors: Jason Foster [aut, cre]
Maintainer: Jason Foster <[email protected]>
License: GPL (>= 2)
Version: 1.0.2
Built: 2026-11-19 13:39:16 UTC
Source: https://github.com/jasonjfoster/rolleigen

Help Index


Rolling Eigenanalysis

Description

Fast and efficient computation of rolling and expanding eigenanalysis for time-series data. The 'rolleigen' package decomposes the covariance matrix of the explanatory variables into eigenvalues and eigenvectors to perform principal component analysis (Pearson, 1901, <doi:10.1080/14786440109462720>; Hotelling, 1933, <doi:10.1037/h0071325>) and principal component regression (Massy, 1965, <doi:10.1080/01621459.1965.10480787>) over rolling and expanding windows. For each window, the eigenvalues and eigenvectors are computed from the covariance matrix and, optionally, matched to the previous window to track the directions of greatest variation in the data. A subset of leading components is then used to fit a regression that mitigates collinearity in the explanatory variables. Use cases include dimensionality reduction, factor extraction, and regression on collinear explanatory variables. The package supports rolling and expanding windows, weights, and handling of missing values via the min_obs, complete_obs, and na_restore arguments. The implementation uses the online and offline algorithms from the 'roll' package to compute rolling and expanding cross-products efficiently, with parallelism across columns and windows provided by 'RcppParallel'.

Details

rolleigen is a package that provides fast and efficient computation of rolling and expanding eigenanalysis for time-series data.

Author(s)

Jason Foster [aut, cre]

References

Hotelling, H. (1933). "Analysis of a Complex of Statistical Variables Into Principal Components." Journal of Educational Psychology, 24(6), 417-441. doi:10.1037/h0071325

Massy, W.F. (1965). "Principal Components Regression in Exploratory Statistical Research." Journal of the American Statistical Association, 60(309), 234-256. doi:10.1080/01621459.1965.10480787

Pearson, K. (1901). "On Lines and Planes of Closest Fit to Systems of Points in Space." Philosophical Magazine, 2(11), 559-572. doi:10.1080/14786440109462720


Rolling Eigenvalues and Eigenvectors

Description

A function for computing the rolling and expanding eigenvalues and eigenvectors of time-series data.

Usage

roll_eigen(x, width, weights = rep(1, width), center = TRUE,
  scale = FALSE, order = TRUE, min_obs = width, complete_obs = TRUE,
  na_restore = FALSE, online = TRUE)

Arguments

x

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

width

integer. Window size.

weights

vector. Weights for each observation within a window.

center

logical. If TRUE then the weighted mean of each variable is used, if FALSE then zero is used.

scale

logical. If TRUE then the weighted standard deviation of each variable is used, if FALSE then no scaling is done.

order

logical. Change sign and order of the components.

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.

Details

The eigenvalues and eigenvectors of the weighted covariance matrix of x, or the weighted correlation matrix if scale is TRUE, are computed within each window and returned in decreasing order, consistent with base::eigen. If order is TRUE then the components of each window are matched, in sign and order, to the previous window by the largest absolute cosine similarity, which assumes full-rank windows.

The online algorithm is used for equal or exponential decay weights, otherwise the offline algorithm is used with a warning if online is TRUE.

Value

A list containing the following components:

values

An object of the same class and dimension as x with the rolling and expanding eigenvalues.

vectors

A cube with each slice the rolling and expanding eigenvectors.

Examples

n <- 15
m <- 3
x <- matrix(rnorm(n * m), nrow = n, ncol = m)
weights <- 0.9 ^ (n:1)

# rolling eigenvalues and eigenvectors with complete windows
roll_eigen(x, width = 5)

# rolling eigenvalues and eigenvectors with partial windows
roll_eigen(x, width = 5, min_obs = 1)

# expanding eigenvalues and eigenvectors with partial windows
roll_eigen(x, width = n, min_obs = 1)

# expanding eigenvalues and eigenvectors with partial windows and weights
roll_eigen(x, width = n, min_obs = 1, weights = weights)

Rolling Principal Component Regressions

Description

A function for computing the rolling and expanding principal component regressions of time-series data.

Usage

roll_pcr(x, y, width, n_comps = NCOL(x), weights = rep(1, width),
  intercept = TRUE, center = TRUE, scale = FALSE, 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.

n_comps

integer. Number of principal components.

weights

vector. Weights for each observation within a window.

intercept

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

center

logical. If TRUE then the weighted mean of each variable is used, if FALSE then zero is used.

scale

logical. If TRUE then the weighted standard deviation of each variable is used, if FALSE then no scaling is done.

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.

Details

The eigenvectors of the weighted covariance matrix of x are computed within each window and ordered from the largest to smallest eigenvalue, then y is regressed on the leading n_comps components and the coefficients are mapped back to the original variables.

intercept = FALSE, center = FALSE, scale = TRUE and complete_obs = FALSE are not supported, so a warning is given and the arguments are ignored. The online algorithm is used for equal or exponential decay weights, otherwise the offline algorithm is used with a warning if online is TRUE.

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.

Examples

n <- 15
m <- 3
x <- matrix(rnorm(n * m), nrow = n, ncol = m)
y <- rnorm(n)
weights <- 0.9 ^ (n:1)

# rolling regressions with complete windows
roll_pcr(x, y, width = 5, n_comps = 1)

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

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

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