| 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, ordered from largest to smallest to summarize 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.0 |
| Built: | 2026-06-08 00:11:06 UTC |
| Source: | https://github.com/jasonjfoster/rolleigen |
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, ordered from largest to smallest to summarize 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'.
rolleigen is a package that provides fast and efficient computation of rolling and expanding eigenanalysis for time-series data.
Jason Foster [aut, cre]
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
A function for computing the rolling and expanding eigenvalues and eigenvectors of time-series data.
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)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)
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 |
scale |
logical. If |
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 |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
A list containing the following components:
values |
An object of the same class and dimension as |
vectors |
A cube with each slice the rolling and expanding eigenvectors. |
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)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)
A function for computing the rolling and expanding principal component regressions of time-series data.
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)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)
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 |
center |
logical. If |
scale |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
A list containing the following components:
coefficients |
A list of objects with the rolling and expanding coefficients for each |
r.squared |
A list of objects with the rolling and expanding r-squareds for each |
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)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)