| Title: | Fast QR Decomposition and Update |
|---|---|
| Description: | Methods for computing QR factorizations and updating or downdating QR and triangular factors when rows or columns are added or removed. Includes compact, thin and complete factorizations, least-squares and ridge regression, and cross-validation. Selected dense operations use the BLAS and LAPACK libraries configured for R. Background references include Golub and Van Loan (2013, ISBN:9781421407944) and Bjorck (2015, <doi:10.1007/978-3-319-05089-8>). |
| Authors: | Mauro Bernardi [aut, cre], Claudio Busatto [aut], Manuela Cattelan [aut] |
| Maintainer: | Mauro Bernardi <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.2.0 |
| Built: | 2026-11-18 22:25:54 UTC |
| Source: | https://github.com/cran/fastQR |
qr provides the QR factorization of the matrix with . The QR factorization of the matrix returns the matrices and such that . See Golub and Van Loan (2013) for further details on the method.
qr(X, complete = NULL, type = NULL, nb = NULL)qr(X, complete = NULL, type = NULL, nb = NULL)
X |
a |
type |
either "givens" or "householder". |
nb |
integer terminal column-width threshold for recursive QR. NULL or zero selects the non-recursive algorithm. For reduced output use nb >= 2; complete output also supports nb = 1 (single-column terminal panels). |
complete |
logical expression of length 1. Indicates whether an arbitrary orthogonal completion of the |
When nb is positive and complete is TRUE, recursive Householder panel transformations are applied to the trailing design and accumulated in the full Q matrix. The returned matrices have dimensions n by n and n by p. No orthogonal completion of a separately computed thin basis is used. The recursive reduced-output algorithm is unchanged. Recursive complete QR supports tall and square inputs; for a wide input the existing non-recursive fallback is used with a warning.
For tall inputs with complete = FALSE, the non-recursive Householder and Givens methods form only the n by p Q factor. Their stored transformations are applied in reverse order to the first p columns of the identity, without constructing a complete n by n Q. Rounding in Q can differ from the complete-output path; the R factor uses the same transformations.
A named list containing
the Q matrix.
the R matrix.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## QR factorization via Givens rotation output <- qr(X, type = "givens", complete = TRUE) Q <- output$Q R <- output$R ## check round(Q %*% R - X, 5) max(abs(Q %*% R - X)) ## QR factorization via Householder rotation output <- qr(X, type = "householder", complete = TRUE) Q <- output$Q R <- output$R ## check round(Q %*% R - X, 5) max(abs(Q %*% R - X))## generate sample data set.seed(1234) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## QR factorization via Givens rotation output <- qr(X, type = "givens", complete = TRUE) Q <- output$Q R <- output$R ## check round(Q %*% R - X, 5) max(abs(Q %*% R - X)) ## QR factorization via Householder rotation output <- qr(X, type = "householder", complete = TRUE) Q <- output$Q R <- output$R ## check round(Q %*% R - X, 5) max(abs(Q %*% R - X))
Computes the coefficient vector solving the
least-squares problem ,
using a QR decomposition stored in compact (Householder) form.
qr_coef(qr, tau, y, pivot = NULL, rank = NULL)qr_coef(qr, tau, y, pivot = NULL, rank = NULL)
qr |
numeric matrix containing the QR decomposition of |
tau |
numeric vector of Householder coefficients. |
y |
numeric response vector of length |
pivot |
optional integer vector of length |
rank |
optional integer specifying the numerical rank of |
The coefficients are obtained by first computing
and then solving the resulting upper-triangular system involving
the matrix . The orthogonal matrix is never formed
explicitly.
a numeric vector of regression coefficients.
Wide designs (more columns than rows) are not supported by this model-fitting interface and raise an R error. This is a restriction of the interface, not of QR factorization itself.
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) coef1 <- fastQR::qr_coef(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation coef2 <- base::qr.coef(base::qr(X), y) max(abs(coef1 - coef2))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) coef1 <- fastQR::qr_coef(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation coef2 <- base::qr.coef(base::qr(X), y) max(abs(coef1 - coef2))
qr_fast provides the fast QR factorization of the matrix with . The full QR factorization of the matrix returns the matrices and the upper triangular matrix such that . See Golub and Van Loan (2013) for further details on the method.
qr_fast(X, tol = NULL, pivot = NULL)qr_fast(X, tol = NULL, pivot = NULL)
X |
a |
tol |
the tolerance for detecting linear dependencies in the columns of |
pivot |
a logical value indicating whether to pivot the columns of |
The QR decomposition plays an important role in many statistical techniques. In particular it can be used to solve the equation for given matrix and vectors and . It is useful for computing regression coefficients and in applying the Newton-Raphson algorithm.
A named list containing
a matrix with the same dimensions as . The upper triangle contains the of the decomposition and the lower triangle contains information on the of the decomposition (stored in compact form).
a vector of length ncol(x) which contains additional information on .
the rank of as computed by the decomposition.
information on the pivoting strategy used during the decomposition.
a boolean variable returning one if the pivoting has been performed and zero otherwise.
Wide designs are supported in compact form, with min(n,p) Householder coefficients. At least one row is required. This factorization does not imply that the regression routines support underdetermined models.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## reconstruct the reduced Q and R matrices ## reduced Q matrix Q1 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = qr_res$rank, complete = FALSE) Q1 ## check the Q matrix (orthogonality) max(abs(crossprod(Q1)-diag(1, p))) ## complete Q matrix Q2 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = NULL, complete = TRUE) Q2 ## check the Q matrix (orthogonality) max(abs(crossprod(Q2)-diag(1, n))) ## reduced R matrix R1 <- qr_R(qr = qr_res$qr, rank = NULL, complete = FALSE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R1 %*% P) - crossprod(X))) max(abs(crossprod(R1) - crossprod(X %*% t(P)))) ## complete R matrix R2 <- qr_R(qr = qr_res$qr, rank = NULL, complete = TRUE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R2 %*% P) - crossprod(X))) max(abs(crossprod(R2) - crossprod(X %*% t(P)))) ## check that X = Q %*% R max(abs(Q2 %*% R2 %*% P - X)) max(abs(Q1 %*% R1 %*% P - X)) ## create data: n > p set.seed(1234) n <- 120 p <- 75 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, pivot = FALSE) ## reconstruct the reduced Q and R matrices ## reduced Q matrix Q1 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = p, complete = FALSE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q1)-diag(1, p))) ## complete Q matrix Q2 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = NULL, complete = TRUE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q2)-diag(1, n))) ## reduced R matrix R1 <- qr_R(qr = qr_res$qr, rank = NULL, complete = FALSE) ## check that X^TX = R^TR max(abs(crossprod(R1) - crossprod(X))) ## complete R matrix R2 <- qr_R(qr = qr_res$qr, rank = NULL, complete = TRUE) ## check that X^TX = R^TR max(abs(crossprod(R2) - crossprod(X))) ## check that X^TX = R^TR max(abs(crossprod(R2) - crossprod(X))) max(abs(crossprod(R2) - crossprod(X))) max(abs(crossprod(R1) - crossprod(X))) # check that X = Q %*% R max(abs(Q2 %*% R2 - X)) max(abs(Q1 %*% R1 - X))## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## reconstruct the reduced Q and R matrices ## reduced Q matrix Q1 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = qr_res$rank, complete = FALSE) Q1 ## check the Q matrix (orthogonality) max(abs(crossprod(Q1)-diag(1, p))) ## complete Q matrix Q2 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = NULL, complete = TRUE) Q2 ## check the Q matrix (orthogonality) max(abs(crossprod(Q2)-diag(1, n))) ## reduced R matrix R1 <- qr_R(qr = qr_res$qr, rank = NULL, complete = FALSE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R1 %*% P) - crossprod(X))) max(abs(crossprod(R1) - crossprod(X %*% t(P)))) ## complete R matrix R2 <- qr_R(qr = qr_res$qr, rank = NULL, complete = TRUE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R2 %*% P) - crossprod(X))) max(abs(crossprod(R2) - crossprod(X %*% t(P)))) ## check that X = Q %*% R max(abs(Q2 %*% R2 %*% P - X)) max(abs(Q1 %*% R1 %*% P - X)) ## create data: n > p set.seed(1234) n <- 120 p <- 75 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, pivot = FALSE) ## reconstruct the reduced Q and R matrices ## reduced Q matrix Q1 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = p, complete = FALSE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q1)-diag(1, p))) ## complete Q matrix Q2 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = NULL, complete = TRUE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q2)-diag(1, n))) ## reduced R matrix R1 <- qr_R(qr = qr_res$qr, rank = NULL, complete = FALSE) ## check that X^TX = R^TR max(abs(crossprod(R1) - crossprod(X))) ## complete R matrix R2 <- qr_R(qr = qr_res$qr, rank = NULL, complete = TRUE) ## check that X^TX = R^TR max(abs(crossprod(R2) - crossprod(X))) ## check that X^TX = R^TR max(abs(crossprod(R2) - crossprod(X))) max(abs(crossprod(R2) - crossprod(X))) max(abs(crossprod(R1) - crossprod(X))) # check that X = Q %*% R max(abs(Q2 %*% R2 - X)) max(abs(Q1 %*% R1 - X))
Computes the fitted values for a linear
least-squares problem using a QR decomposition stored in compact
(Householder) form.
qr_fitted(qr, tau, y)qr_fitted(qr, tau, y)
qr |
Numeric matrix containing the QR decomposition of |
tau |
numeric vector of Householder coefficients. |
y |
numeric response vector of length |
The fitted values are computed as
without explicitly forming the orthogonal matrix . The
computation relies on the Householder reflectors stored in
qr and tau.
a numeric vector of fitted values .
Wide designs (more columns than rows) are not supported by this model-fitting interface and raise an R error. This is a restriction of the interface, not of QR factorization itself.
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) yhat1 <- fastQR::qr_fitted(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation yhat2 <- base::qr.fitted(base::qr(X), y) max(abs(yhat1 - yhat2))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) yhat1 <- fastQR::qr_fitted(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation yhat2 <- base::qr.fitted(base::qr(X), y) max(abs(yhat1 - yhat2))
Calls DGEQRF explicitly, followed by DORGQR only when an explicit orthogonal factor is requested. No numerical rank is estimated.
qr_lapack(X, output = c("thin", "full", "R", "compact"))qr_lapack(X, output = c("thin", "full", "R", "compact"))
X |
A nonempty finite real numeric matrix; tall, square and wide matrices are supported. |
output |
Representation to return: "thin", "full", "R", or "compact". |
Factor signs are not standardized. Rank-deficient inputs retain the same output dimensions. The input is not modified. The backend uses LAPACK and BLAS selected by R, without requiring the LAPACKE C interface.
For "thin", a list with Q of size n by min(n,p) and R of size min(n,p) by p. For "full", Q is n by n and R is n by p. For "R", the triangular or trapezoidal matrix R alone is returned. For "compact", a list containing the DGEQRF packed matrix qr and Householder coefficients tau is returned. This compact list is not a base R qr object or a fastQR::qr_fast object.
set.seed(1) X <- matrix(rnorm(40), 10, 4) f <- qr_lapack(X) max(abs(f$Q %*% f$R - X)) qr_lapack(X, output = "R")set.seed(1) X <- matrix(rnorm(40), 10, 4) f <- qr_lapack(X) max(abs(f$Q %*% f$R - X)) qr_lapack(X, output = "R")
qr_lm, or LS for linear regression models, solves the following optimization problem
for and witn , to obtain a coefficient vector . The design matrix
contains the observations for each regressor.
qr_lm(y, X, X_test = NULL)qr_lm(y, X, X_test = NULL)
y |
a vector of length- |
X |
an |
X_test |
an |
A named list containing
a length- vector containing the solution for the parameters .
a length- vector containing the standard errors for the estimated regression parameters .
a length- vector of fitted values, .
a length- vector of residuals, .
the squared L2-norm of the residuals,
the squared L2-norm of the response variable,
the upper triangular matrix of the QR decomposition.
the inverse of the upper triangular matrix of the QR decomposition .
the Gram matrix of the least squares problem.
the inverse of the Gram matrix of the least squares problem .
A vector equal to , the cross-product of the design matrix with the response vector .
An estimate of the error variance , computed as the residual sum of squares divided by the residual degrees of freedom
The residual degrees of freedom, given by , where is the number of observations and is the number of estimated parameters.
, coefficient of determination, measure of goodness-of-fit of the model.
predicted values for the test set, . It is only available if X_test is not NULL.
Wide designs (nrow(X) < ncol(X)) are not supported by this model-fitting interface and raise an R error.
## generate sample data ## create data: n > p set.seed(1234) n <- 12 n0 <- 3 p <- 7 X <- matrix(rnorm(n * p), n, p) b <- rep(1, p) sig2 <- 0.25 y <- X %*% b + sqrt(sig2) * rnorm(n) summary(lm(y~X)) ## test X_test <- matrix(rnorm(n0 * p), n0, p) ## lm qr_lm(y = y, X = X, X_test = X_test) qr_lm(y = y, X = X)## generate sample data ## create data: n > p set.seed(1234) n <- 12 n0 <- 3 p <- 7 X <- matrix(rnorm(n * p), n, p) b <- rep(1, p) sig2 <- 0.25 y <- X %*% b + sqrt(sig2) * rnorm(n) summary(lm(y~X)) ## test X_test <- matrix(rnorm(n0 * p), n0, p) ## lm qr_lm(y = y, X = X, X_test = X_test) qr_lm(y = y, X = X)
Computes the coefficient vector solving the
least-squares problem ,
using a QR decomposition computed internally.
qr_lse_coef(X, y)qr_lse_coef(X, y)
X |
numeric matrix of dimension |
y |
numeric response vector of length |
The QR decomposition of is computed internally. The coefficients
are obtained by first computing and then solving the
resulting upper-triangular system involving the matrix .
The orthogonal matrix is never formed explicitly.
This function is intended as a convenience wrapper for least-squares estimation when the explicit QR factors are not required.
a numeric vector of regression coefficients.
Wide designs (more columns than rows) are not supported by this model-fitting interface and raise an R error. This is a restriction of the interface, not of QR factorization itself.
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) coef1 <- fastQR::qr_lse_coef(X, y) ## reference computation coef2 <- base::qr.coef(base::qr(X), y) max(abs(coef1 - coef2))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) coef1 <- fastQR::qr_lse_coef(X, y) ## reference computation coef2 <- base::qr.coef(base::qr(X), y) max(abs(coef1 - coef2))
Computes the fitted values for a linear
least-squares problem using a QR decomposition computed internally.
qr_lse_fitted(X, y)qr_lse_fitted(X, y)
X |
numeric matrix of dimension |
y |
numeric response vector of length |
The QR decomposition of is computed internally. The fitted
values are obtained as
without explicitly forming the orthogonal matrix .
This function is intended as a convenience wrapper for least-squares computations when the explicit QR factors are not required.
a numeric vector of fitted values .
Wide designs (more columns than rows) are not supported by this model-fitting interface and raise an R error. This is a restriction of the interface, not of QR factorization itself.
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) yhat1 <- fastQR::qr_lse_fitted(X, y) ## reference computation yhat2 <- base::qr.fitted(base::qr(X), y) max(abs(yhat1 - yhat2))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) yhat1 <- fastQR::qr_lse_fitted(X, y) ## reference computation yhat2 <- base::qr.fitted(base::qr(X), y) max(abs(yhat1 - yhat2))
Computes the product , where is the orthogonal
matrix from the QR decomposition of the design matrix .
qr_lse_Qty(X, y)qr_lse_Qty(X, y)
X |
numeric matrix of dimension |
y |
numeric response vector of length |
The QR decomposition of is computed internally, and the
orthogonal matrix is never formed explicitly. The product
is evaluated efficiently using Householder reflectors.
This function is intended as a convenience wrapper for least-squares computations when the explicit QR factors are not required.
a numeric vector equal to .
The complete orthogonal transformation is applied directly, without forming Q. Tall, square and wide designs are supported; both input and output vectors have length nrow(X).
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) res1 <- fastQR::qr_lse_Qty(X, y) ## reference computation res2 <- crossprod(base::qr.Q(base::qr(X), complete = TRUE), y) max(abs(res1 - drop(res2)))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) res1 <- fastQR::qr_lse_Qty(X, y) ## reference computation res2 <- crossprod(base::qr.Q(base::qr(X), complete = TRUE), y) max(abs(res1 - drop(res2)))
Computes the product , where is the orthogonal
matrix from the QR decomposition of the design matrix .
qr_lse_Qy(X, y)qr_lse_Qy(X, y)
X |
numeric matrix of dimension |
y |
numeric response vector of length |
The QR decomposition of is computed internally, and the
orthogonal matrix is never formed explicitly. The product
is evaluated efficiently using Householder reflectors.
This function is intended as a convenience wrapper for least-squares computations when the explicit QR factors are not required.
a numeric vector equal to .
The complete orthogonal transformation is applied directly, without forming Q. Tall, square and wide designs are supported; both input and output vectors have length nrow(X).
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) res1 <- fastQR::qr_lse_Qy(X, y) ## reference computation res2 <- base::qr.Q(base::qr(X), complete = TRUE) %*% y max(abs(res1 - drop(res2)))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) res1 <- fastQR::qr_lse_Qy(X, y) ## reference computation res2 <- base::qr.Q(base::qr(X), complete = TRUE) %*% y max(abs(res1 - drop(res2)))
Computes the residual vector for a linear
least-squares problem using a QR decomposition computed internally.
qr_lse_resid(X, y)qr_lse_resid(X, y)
X |
numeric matrix of dimension |
y |
numeric response vector of length |
The QR decomposition of is computed internally. The residuals
are obtained as
without explicitly forming the orthogonal matrix .
This function is intended as a convenience wrapper for least-squares computations when the explicit QR factors are not required.
a numeric vector of residuals.
Wide designs (more columns than rows) are not supported by this model-fitting interface and raise an R error. This is a restriction of the interface, not of QR factorization itself.
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) r1 <- fastQR::qr_lse_resid(X, y) ## reference computation r2 <- base::qr.resid(base::qr(X), y) max(abs(r1 - r2))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) r1 <- fastQR::qr_lse_resid(X, y) ## reference computation r2 <- base::qr.resid(base::qr(X), y) max(abs(r1 - r2))
returns the permutation matrix for the QR decomposition.
qr_pivot2perm(pivot)qr_pivot2perm(pivot)
pivot |
a vector of dimension |
the perumutation matrix of dimension .
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the pivot matrix P <- qr_pivot2perm(qr_res$pivot)## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the pivot matrix P <- qr_pivot2perm(qr_res$pivot)
returns the matrix of the full QR decomposition. If , then only the reduced matrix is returned.
qr_Q(qr, tau, rank = NULL, complete = NULL)qr_Q(qr, tau, rank = NULL, complete = NULL)
qr |
object representing a QR decomposition. This will typically have come from a previous call to qr. |
tau |
a vector of length |
rank |
the rank of x as computed by the decomposition. |
complete |
logical flag (length 1). Indicates whether to compute the full |
returns part or all of , the order- orthogonal (unitary) transformation represented by qr. If the supplied rank is , has columns, including zero when , regardless of complete. Otherwise, it has columns when complete is TRUE and columns when complete is FALSE.
For wide compact factors, missing rank with complete = TRUE uses min(n,p). A supplied rank must not exceed min(n,p). The existing rank-dependent output rule still applies.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the full Q matrix Q1 <- qr_Q(qr_res$qr, qr_res$qraux, complete = TRUE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q1)-diag(1, n))) ## get the reduced Q matrix Q2 <- qr_Q(qr_res$qr, qr_res$qraux, qr_res$rank, complete = FALSE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q2)-diag(1, p)))## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the full Q matrix Q1 <- qr_Q(qr_res$qr, qr_res$qraux, complete = TRUE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q1)-diag(1, n))) ## get the reduced Q matrix Q2 <- qr_Q(qr_res$qr, qr_res$qraux, qr_res$rank, complete = FALSE) ## check the Q matrix (orthogonality) max(abs(crossprod(Q2)-diag(1, p)))
returns the full matrix.
qr_Q_full(qr, tau)qr_Q_full(qr, tau)
qr |
object representing a QR decomposition. This will typically have come from a previous call to qr. |
tau |
a vector of length |
returns the matrix .
Both tall and wide compact factors are supported. At least min(nrow(qr),ncol(qr)) Householder coefficients must be supplied.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## create data: n > p set.seed(1234) n <- 12 p <- 7 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## complete the reduced Q matrix Q <- fastQR::qr_Q_full(qr = qr_res$qr, tau = qr_res$qraux) ## check the Q matrix (orthogonality) max(abs(crossprod(Q)-diag(1, n)))## create data: n > p set.seed(1234) n <- 12 p <- 7 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## complete the reduced Q matrix Q <- fastQR::qr_Q_full(qr = qr_res$qr, tau = qr_res$qraux) ## check the Q matrix (orthogonality) max(abs(crossprod(Q)-diag(1, n)))
returns the full matrix.
qr_Q_reduced2full(Q)qr_Q_reduced2full(Q)
Q |
a |
The supplied columns must be finite and orthonormal. The maximum absolute entry of Q'Q - I must not exceed 100 * .Machine$double.eps * nrow(Q). The leading columns of the returned matrix preserve the supplied values exactly. For a matrix with zero columns the result is the identity matrix. The remaining columns are obtained from a Householder orthogonal completion and need not match the previous completion algorithm.
a orthogonal matrix .
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## create data: n > p set.seed(1234) n <- 12 p <- 7 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## reconstruct the reduced Q matrix Q1 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = qr_res$rank, complete = FALSE) ## complete the reduced Q matrix Q2 <- fastQR::qr_Q_reduced2full(Q = Q1) R <- fastQR::qr_R(qr = qr_res$qr, rank = NULL, complete = TRUE) X1 <- qr_X(Q = Q2, R = R, pivot = qr_res$pivot) max(abs(X - X1))## create data: n > p set.seed(1234) n <- 12 p <- 7 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## reconstruct the reduced Q matrix Q1 <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = qr_res$rank, complete = FALSE) ## complete the reduced Q matrix Q2 <- fastQR::qr_Q_reduced2full(Q = Q1) R <- fastQR::qr_R(qr = qr_res$qr, rank = NULL, complete = TRUE) X1 <- qr_X(Q = Q2, R = R, pivot = qr_res$pivot) max(abs(X - X1))
Computes , where is the orthogonal matrix from the
QR decomposition stored in compact (Householder) form.
qr_Qty(qr, tau, y)qr_Qty(qr, tau, y)
qr |
numeric matrix containing the QR decomposition in compact form
(as returned by |
tau |
numeric vector of Householder coefficients. |
y |
numeric vector of length |
The orthogonal matrix is not formed explicitly. The product
is computed efficiently using the Householder reflectors
stored in qr and tau.
a numeric vector equal to .
Tall, square and wide compact factors are supported. The response vector must have length nrow(qr).
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) res1 <- fastQR::qr_Qty(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation Q <- base::qr.Q(base::qr(X), complete = TRUE) res2 <- crossprod(Q, y) max(abs(res1 - drop(res2)))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) res1 <- fastQR::qr_Qty(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation Q <- base::qr.Q(base::qr(X), complete = TRUE) res2 <- crossprod(Q, y) max(abs(res1 - drop(res2)))
Computes , where is the orthogonal matrix from the
QR decomposition stored in compact (Householder) form.
qr_Qy(qr, tau, y)qr_Qy(qr, tau, y)
qr |
numeric matrix containing the QR decomposition in compact form
(as returned by |
tau |
numeric vector of Householder coefficients. |
y |
numeric vector of length |
The orthogonal matrix is not formed explicitly. The product
is computed efficiently using the Householder reflectors
stored in qr and tau.
a numeric vector equal to .
Tall, square and wide compact factors are supported. The response vector must have length nrow(qr).
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) res1 <- fastQR::qr_Qy(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation Q <- base::qr.Q(base::qr(X), complete = TRUE) res2 <- Q %*% y max(abs(res1 - drop(res2)))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) res1 <- fastQR::qr_Qy(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation Q <- base::qr.Q(base::qr(X), complete = TRUE) res2 <- Q %*% y max(abs(res1 - drop(res2)))
returns the matrix of the full QR decomposition. If , then only the reduced matrix is returned.
qr_R(qr, rank = NULL, pivot = NULL, complete = NULL)qr_R(qr, rank = NULL, pivot = NULL, complete = NULL)
qr |
object representing a QR decomposition. This will typically have come from a previous call to qr. |
rank |
the rank of x as computed by the decomposition. |
pivot |
a vector of length |
complete |
logical flag (length 1). Indicates whether the |
returns part or all of . If the supplied rank is , has rows, including zero when , regardless of complete. Otherwise, it has rows when complete is TRUE and rows when complete is FALSE.
For wide compact factors, the default rank is min(n,p), yielding an n by p factor. A supplied rank must not exceed min(n,p).
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the full R matrix R1 <- qr_R(qr_res$qr, complete = TRUE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R1 %*% P) - crossprod(X))) max(abs(crossprod(R1) - crossprod(X %*% t(P)))) ## get the reduced R matrix R2 <- qr_R(qr_res$qr, qr_res$rank, complete = FALSE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R2 %*% P) - crossprod(X))) max(abs(crossprod(R2) - crossprod(X %*% t(P))))## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the full R matrix R1 <- qr_R(qr_res$qr, complete = TRUE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R1 %*% P) - crossprod(X))) max(abs(crossprod(R1) - crossprod(X %*% t(P)))) ## get the reduced R matrix R2 <- qr_R(qr_res$qr, qr_res$rank, complete = FALSE) ## check that X^TX = R^TR ## get the permutation matrix P <- qr_pivot2perm(pivot = qr_res$pivot) max(abs(crossprod(R2 %*% P) - crossprod(X))) max(abs(crossprod(R2) - crossprod(X %*% t(P))))
Computes the residual vector for a linear
least-squares problem using a QR decomposition stored in compact
(Householder) form.
qr_resid(qr, tau, y)qr_resid(qr, tau, y)
qr |
numeric matrix containing the QR decomposition of |
tau |
numeric vector of Householder coefficients. |
y |
numeric response vector of length |
The residuals are computed as
without explicitly forming the orthogonal matrix . The
computation relies on the Householder reflectors stored in
qr and tau.
a numeric vector of residuals of dimension .
Wide designs (more columns than rows) are not supported by this model-fitting interface and raise an R error. This is a restriction of the interface, not of QR factorization itself.
set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) r1 <- fastQR::qr_resid(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation r2 <- base::qr.resid(base::qr(X), y) max(abs(r1 - r2))set.seed(1) n <- 10; p <- 4 X <- matrix(rnorm(n * p), n, p) y <- rnorm(n) qr_res <- fastQR::qr_fast(X) r1 <- fastQR::qr_resid(qr = qr_res$qr, tau = qr_res$qraux, y = y) ## reference computation r2 <- base::qr.resid(base::qr(X), y) max(abs(r1 - r2))
qr_thin provides the thin QR factorization of the matrix with . The thin QR factorization of the matrix returns the matrices and the upper triangular matrix such that . See Golub and Van Loan (2013) for further details on the method.
qr_thin(X)qr_thin(X)
X |
a |
A named list containing
the Q matrix.
the R matrix.
This thin-factor interface requires nrow(X) >= ncol(X); wide designs raise an R error. Use qr() or qr_fast() for wide factorizations. At least one row is required.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the thin QR factorization output <- qr_thin(X = X) Q <- output$Q R <- output$R ## check max(abs(Q %*% R - X))## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the thin QR factorization output <- qr_thin(X = X) Q <- output$Q R <- output$R ## check max(abs(Q %*% R - X))
from the Q and R matrices of the QR decomposition.returns the matrix.
qr_X(Q, R, pivot = NULL)qr_X(Q, R, pivot = NULL)
Q |
either the reduced |
R |
either the reduced |
pivot |
a vector of length |
returns the matrix .
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, pivot = TRUE) ## get the Q and R matrices Q <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = qr_res$rank, complete = TRUE) R <- qr_R(qr = qr_res$qr, rank = qr_res$rank, complete = TRUE) X1 <- qr_X(Q = Q, R = R, pivot = qr_res$pivot) max(abs(X1 - X)) ## get the full QR decomposition without pivot qr_res <- fastQR::qr_fast(X = X, pivot = FALSE) ## get the Q and R matrices Q <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = p, complete = FALSE) R <- qr_R(qr = qr_res$qr, rank = NULL, complete = FALSE) X1 <- qr_X(Q = Q, R = R, pivot = NULL) max(abs(X1 - X))## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p), n, p) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, tol = sqrt(.Machine$double.eps), pivot = TRUE) ## get the full QR decomposition with pivot qr_res <- fastQR::qr_fast(X = X, pivot = TRUE) ## get the Q and R matrices Q <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = qr_res$rank, complete = TRUE) R <- qr_R(qr = qr_res$qr, rank = qr_res$rank, complete = TRUE) X1 <- qr_X(Q = Q, R = R, pivot = qr_res$pivot) max(abs(X1 - X)) ## get the full QR decomposition without pivot qr_res <- fastQR::qr_fast(X = X, pivot = FALSE) ## get the Q and R matrices Q <- qr_Q(qr = qr_res$qr, tau = qr_res$qraux, rank = p, complete = FALSE) R <- qr_R(qr = qr_res$qr, rank = NULL, complete = FALSE) X1 <- qr_X(Q = Q, R = R, pivot = NULL) max(abs(X1 - X))
qrchol, provides the Cholesky decomposition of the symmetric and positive definite matrix , where is the input matrix.
qrchol(X, nb = NULL)qrchol(X, nb = NULL)
X |
an |
nb |
number of blocks for the recursive block QR decomposition, default is NULL. |
an upper triangular matrix of dimension which represents the Cholesky decomposition of .
Require nrow(X) >= ncol(X). Wide designs are rejected: this interface returns a square triangular factor.
qrdowndate provides the update of the QR factorization after the deletion of rows or columns to the matrix with . The QR factorization of the matrix returns the matrices and such that . The and matrices are factorized as and , with , such that and upper triangular matrix and . qrdowndate accepts in input the matrices and either the complete matrix or the reduced one, . See Golub and Van Loan (2013) for further details on the method.
qrdowndate(Q, R, k, m = NULL, type = NULL, fast = NULL, complete = NULL)qrdowndate(Q, R, k, m = NULL, type = NULL, fast = NULL, complete = NULL)
Q |
a complete |
R |
the complete |
k |
position where the columns or the rows are removed. |
m |
number of columns or rows to be removed. Default is |
type |
either 'row' of 'column', for deleting rows or columns. Default is 'column'. |
fast |
fast mode: disable to check whether the provided matrices are valid inputs. Default is FALSE. |
complete |
logical expression of length 1. Indicates whether an arbitrary orthogonal completion of the |
For column deletion with complete = FALSE, Q and R may independently be complete or thin factors. Only the first p columns of Q and the leading p by p block of R are used. The input matrices are unchanged and the output has dimensions n by (p-m) and (p-m) by (p-m). The reduced path avoids internal n by n factor allocations; double R matrices are read through non-owning views before the required thin blocks are copied. Integer inputs may require coercion to double. Structural dimension and index checks remain active in fast mode; finite-value checks on the used blocks are skipped. For complete output or row deletion, complete input factors are still required. For row deletion, require 1 <= m <= n-p and 1 <= k <= n-m+1. Deleting n-p rows is allowed and produces square factors. These bounds are checked even in fast mode. The reduced output can be supplied directly to another reduced column deletion.
A named list containing
the updated Q matrix.
the updated R matrix.
Wide input factors are not supported and are rejected even in fast mode.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## Remove one column ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the column to be deleted ## from X and update X k <- 2 X1 <- X[, -k] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = 1, type = "column", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Remove m columns ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the column to be deleted from X ## and update X m <- 2 k <- 2 X1 <- X[, -c(k,k+m-1)] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = 2, type = "column", fast = TRUE, complete = FALSE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Remove one row ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the row to be deleted from X and update X k <- 5 X1 <- X[-k,] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = 1, type = "row", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Remove m rows ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the rows to be deleted from X and update X k <- 5 m <- 2 X1 <- X[-c(k,k+1),] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = m, type = "row", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1))## Remove one column ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the column to be deleted ## from X and update X k <- 2 X1 <- X[, -k] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = 1, type = "column", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Remove m columns ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the column to be deleted from X ## and update X m <- 2 k <- 2 X1 <- X[, -c(k,k+m-1)] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = 2, type = "column", fast = TRUE, complete = FALSE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Remove one row ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the row to be deleted from X and update X k <- 5 X1 <- X[-k,] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = 1, type = "row", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Remove m rows ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R ## select the rows to be deleted from X and update X k <- 5 m <- 2 X1 <- X[-c(k,k+1),] ## downdate the QR decomposition out <- fastQR::qrdowndate(Q = Q, R = R, k = k, m = m, type = "row", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1))
qrls, or LS for linear regression models, solves the following optimization problem
for and , to obtain a coefficient vector . The design matrix
contains the observations for each regressor.
qrls(y, X, X_test = NULL, type = NULL)qrls(y, X, X_test = NULL, type = NULL)
y |
a vector of length- |
X |
an |
X_test |
an |
type |
either "QR" or "R". Specifies the type of decomposition to use: "QR" for the QR decomposition or "R" for the Cholesky factorization of |
A named list containing
a length- vector containing the solution for the parameters .
a length- vector of fitted values, .
a length- vector of residuals, .
the L2-norm of the residuals,
the L2-norm of the response variable.
matrix of the QR decomposition of the matrix .
matrix of the QR decomposition of the matrix .
, where matrix of the QR decomposition of the matrix .
, coefficient of determination, measure of goodness-of-fit of the model.
predicted values for the test set, . It is only available if X_test is not NULL.
Wide designs (more columns than rows) are not supported by this model-fitting interface and raise an R error. This is a restriction of the interface, not of QR factorization itself.
## generate sample data set.seed(10) n <- 30 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- rnorm(n, sd = 0.5) beta <- rep(0, p) beta[1:3] <- 1 beta[4:5] <- 2 y <- X %*% beta + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrls(y = y, X = X, X_test = X_test) output$coeff## generate sample data set.seed(10) n <- 30 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- rnorm(n, sd = 0.5) beta <- rep(0, p) beta[1:3] <- 1 beta[4:5] <- 2 y <- X %*% beta + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrls(y = y, X = X, X_test = X_test) output$coeff
qrmls, or LS for linear multivariate regression models, solves the following optimization problem
for and , to obtain a coefficient matrix . The design matrix
contains the observations for each regressor.
qrmls(Y, X, X_test = NULL, type = NULL)qrmls(Y, X, X_test = NULL, type = NULL)
Y |
a matrix of dimension |
X |
an |
X_test |
an |
type |
either "QR" or "R". Specifies the type of decomposition to use: "QR" for the QR decomposition or "R" for the Cholesky factorization of |
A named list containing
a matrix of dimension containing the solution for the parameters .
a matrix of dimension of fitted values, .
a matrix of dimension of residuals, .
the matrix .
a matrix of dimension containing the estimated residual variance-covariance matrix.
degrees of freedom.
matrix of the QR decomposition of the matrix .
.
, coefficient of determination, measure of goodness-of-fit of the model.
predicted values for the test set, . It is only available if X_test is not NULL.
## generate sample data set.seed(10) n <- 30 p <- 6 q <- 3 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- matrix(rnorm(n*q), n, q) B <- matrix(0, p, q) B[,1] <- rep(1, p) B[,2] <- rep(2, p) B[,3] <- rep(-1, p) Y <- X %*% B + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrmls(Y = Y, X = X, X_test = X_test, type = "QR") output$coeff## generate sample data set.seed(10) n <- 30 p <- 6 q <- 3 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- matrix(rnorm(n*q), n, q) B <- matrix(0, p, q) B[,1] <- rep(1, p) B[,2] <- rep(2, p) B[,3] <- rep(-1, p) Y <- X %*% B + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrmls(Y = Y, X = X, X_test = X_test, type = "QR") output$coeff
Fits the normalized ridge criterion
All columns, including any intercept column supplied by the user, are penalized. No centering, scaling or intercept insertion is performed internally.
qrmridge(Y, X, lambda, X_test = NULL, type = NULL)qrmridge(Y, X, lambda, X_test = NULL, type = NULL)
Y |
A finite response matrix with n rows and q columns. |
X |
A finite n by p design matrix with n >= 1 and p >= 1. Strictly positive penalties permit p > n. |
lambda |
A finite nonnegative penalty (one scalar). |
X_test |
Optional finite prediction matrix with p columns. Prediction rows need not exceed p. |
type |
Either "QR" (the default) or "R". QR factorizes the normalized penalized system; R uses the triangular factor of an augmented design and two triangular solves. |
Both backends solve
with a vector right-hand side for a univariate response. For type="QR", Q times R equals X transpose X divided by n plus lambda I. For type="R", R transpose R equals X transpose X plus n times lambda I; Q is not constructed or returned. The names XTX and XTy/XTY always refer to the unpenalized, unnormalized cross-products. Positive penalties allow rank-deficient designs. A zero penalty requires full column rank. Strictly positive penalties also allow more predictors than observations.
A named list with the following components:
Fitted coefficient matrix, p by q.
Fitted responses on the training data.
Observed minus fitted responses.
The unpenalized cross-product X transpose X.
The unpenalized cross-product X transpose Y.
Compatibility alias for XTY. There are no duplicated field names.
Residual cross-product divided by n-p, or NA when n<=p. This is a residual scaling diagnostic, not an unbiased ridge covariance estimator.
Legacy residual dimension: q*(n-p) when n>=p, and NA when p>n. This is not the effective degrees of freedom of the ridge fitted-value map.
Backend-specific triangular factor; see Details.
Present only for type="QR"; orthogonal factor of the normalized penalized system.
For type="QR", Q transpose times X transpose Y/n.
For type="R", the intermediate right-hand side after solving the transposed triangular system.
One minus the residual sum of squares divided by the centered total sum of squares, summing across responses in the multivariate case. NA when the total is zero.
Predictions at X_test, present only when X_test is supplied.
Compatibility alias for QXTY, present only for type="QR".
## generate sample data set.seed(10) n <- 30 p <- 6 q <- 3 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- matrix(rnorm(n*q), n, q) B <- matrix(0, p, q) B[,1] <- rep(1, p) B[,2] <- rep(2, p) B[,3] <- rep(-1, p) Y <- X %*% B + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrmridge(Y = Y, X = X, lambda = 1, X_test = X_test, type = "QR") output$coeff## generate sample data set.seed(10) n <- 30 p <- 6 q <- 3 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- matrix(rnorm(n*q), n, q) B <- matrix(0, p, q) B[,1] <- rep(1, p) B[,2] <- rep(2, p) B[,3] <- rep(-1, p) Y <- X %*% B + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrmridge(Y = Y, X = X, lambda = 1, X_test = X_test, type = "QR") output$coeff
Fits the normalized ridge criterion
All columns, including any intercept column supplied by the user, are penalized. No centering, scaling or intercept insertion is performed internally. The penalty is selected by cross-validation and the model is refitted on all observations.
qrmridge_cv(Y, X, lambda, k = NULL, seed = NULL, X_test = NULL, type = NULL)qrmridge_cv(Y, X, lambda, k = NULL, seed = NULL, X_test = NULL, type = NULL)
Y |
A finite response matrix with n rows and q columns. |
X |
A finite n by p design matrix with n >= 1 and p >= 1. Strictly positive penalties permit p > n. |
lambda |
A nonempty grid of finite nonnegative penalties. |
k |
Number of folds, between 2 and n. Zero requests leave-one-out CV. The default is min(10,n). When the grid includes zero, every training fold must have at least p rows. |
seed |
Integer seed used to generate reproducible folds; default 1234. |
X_test |
Optional finite prediction matrix with p columns. Prediction rows need not exceed p. |
type |
Either "QR" (the default) or "R". QR factorizes the normalized penalized system; R uses the triangular factor of an augmented design and two triangular solves. |
Both backends solve
with a vector right-hand side for a univariate response. For type="QR", Q times R equals X transpose X divided by n plus lambda I. For type="R", R transpose R equals X transpose X plus n times lambda I; Q is not constructed or returned. The names XTX and XTy/XTY always refer to the unpenalized, unnormalized cross-products. Positive penalties allow rank-deficient designs. A zero penalty requires full column rank. Strictly positive penalties also allow more predictors than observations. Each fold uses its own training size in the normalized criterion. Augmented factors are cached for distinct training sizes and updated by removing held-out rows. A factor is recomputed if a numerical downdate fails its cross-product check. The final fit uses the full sample size and the selected penalty. The seed argument resets R's random-number generator; foldid records the generated split.
A named list with the following components:
Fitted coefficient matrix, p by q.
Fitted responses on the training data.
Observed minus fitted responses.
The unpenalized cross-product X transpose X.
The unpenalized cross-product X transpose Y.
Compatibility alias for XTY. There are no duplicated field names.
Residual cross-product divided by n-p, or NA when n<=p. This is a residual scaling diagnostic, not an unbiased ridge covariance estimator.
Legacy residual dimension: q*(n-p) when n>=p, and NA when p>n. This is not the effective degrees of freedom of the ridge fitted-value map.
Backend-specific triangular factor; see Details.
Present only for type="QR"; orthogonal factor of the normalized penalized system.
For type="QR", Q transpose times X transpose Y/n.
For type="R", the intermediate right-hand side after solving the transposed triangular system.
One minus the residual sum of squares divided by the centered total sum of squares, summing across responses in the multivariate case. NA when the total is zero.
Predictions at X_test, present only when X_test is supplied.
Compatibility alias for QXTY, present only for type="QR".
The supplied penalty grid.
Mean held-out squared errors, averaged equally across folds. Each fold error averages across observations and responses.
Smallest value of mse.
Zero-based index of the first minimum, preserving the existing indexing convention.
Selected penalty. Ties are resolved by the first supplied grid entry.
Integer fold labels in the original observation order.
## generate sample data set.seed(10) n <- 30 p <- 6 q <- 3 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- matrix(rnorm(n*q), n, q) B <- matrix(0, p, q) B[,1] <- rep(1, p) B[,2] <- rep(2, p) B[,3] <- rep(-1, p) Y <- X %*% B + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrmridge_cv(Y = Y, X = X, lambda = c(1,2), k = 5, seed = 12, X_test = X_test, type = "QR") output$coeff## generate sample data set.seed(10) n <- 30 p <- 6 q <- 3 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- matrix(rnorm(n*q), n, q) B <- matrix(0, p, q) B[,1] <- rep(1, p) B[,2] <- rep(2, p) B[,3] <- rep(-1, p) Y <- X %*% B + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrmridge_cv(Y = Y, X = X, lambda = c(1,2), k = 5, seed = 12, X_test = X_test, type = "QR") output$coeff
Fits the normalized ridge criterion
All columns, including any intercept column supplied by the user, are penalized. No centering, scaling or intercept insertion is performed internally.
qrridge(y, X, lambda, X_test = NULL, type = NULL)qrridge(y, X, lambda, X_test = NULL, type = NULL)
y |
A finite response vector of length n. |
X |
A finite n by p design matrix with n >= 1 and p >= 1. Strictly positive penalties permit p > n. |
lambda |
A finite nonnegative penalty (one scalar). |
X_test |
Optional finite prediction matrix with p columns. Prediction rows need not exceed p. |
type |
Either "QR" (the default) or "R". QR factorizes the normalized penalized system; R uses the triangular factor of an augmented design and two triangular solves. |
Both backends solve
with a vector right-hand side for a univariate response. For type="QR", Q times R equals X transpose X divided by n plus lambda I. For type="R", R transpose R equals X transpose X plus n times lambda I; Q is not constructed or returned. The names XTX and XTy/XTY always refer to the unpenalized, unnormalized cross-products. Positive penalties allow rank-deficient designs. A zero penalty requires full column rank. Strictly positive penalties also allow more predictors than observations.
A named list with the following components:
Fitted coefficient vector of length p.
Fitted responses on the training data.
Observed minus fitted responses.
The unpenalized cross-product X transpose X.
The unpenalized cross-product X transpose y.
Sum of squared residuals.
Centered response sum of squares.
Sum of squared residuals divided by n-p, or NA when n<=p. This is a residual scaling diagnostic, not an unbiased ridge variance estimator.
Legacy residual dimension: n-p when n>=p, and NA when p>n. This is not the effective degrees of freedom of the ridge fitted-value map.
Backend-specific triangular factor; see Details.
Present only for type="QR"; orthogonal factor of the normalized penalized system.
For type="QR", Q transpose times X transpose y/n.
For type="R", the intermediate right-hand side after solving the transposed triangular system.
One minus the residual sum of squares divided by the centered total sum of squares, summing across responses in the multivariate case. NA when the total is zero.
Predictions at X_test, present only when X_test is supplied.
## generate sample data set.seed(10) n <- 30 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- rnorm(n, sd = 0.5) beta <- rep(0, p) beta[1:3] <- 1 beta[4:5] <- 2 y <- X %*% beta + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrridge(y = y, X = X, lambda = 0.2, X_test = X_test) output$coeff## generate sample data set.seed(10) n <- 30 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- rnorm(n, sd = 0.5) beta <- rep(0, p) beta[1:3] <- 1 beta[4:5] <- 2 y <- X %*% beta + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrridge(y = y, X = X, lambda = 0.2, X_test = X_test) output$coeff
Fits the normalized ridge criterion
All columns, including any intercept column supplied by the user, are penalized. No centering, scaling or intercept insertion is performed internally. The penalty is selected by cross-validation and the model is refitted on all observations.
qrridge_cv(y, X, lambda, k = NULL, seed = NULL, X_test = NULL, type = NULL)qrridge_cv(y, X, lambda, k = NULL, seed = NULL, X_test = NULL, type = NULL)
y |
A finite response vector of length n. |
X |
A finite n by p design matrix with n >= 1 and p >= 1. Strictly positive penalties permit p > n. |
lambda |
A nonempty grid of finite nonnegative penalties. |
k |
Number of folds, between 2 and n. Zero requests leave-one-out CV. The default is min(10,n). When the grid includes zero, every training fold must have at least p rows. |
seed |
Integer seed used to generate reproducible folds; default 1234. |
X_test |
Optional finite prediction matrix with p columns. Prediction rows need not exceed p. |
type |
Either "QR" (the default) or "R". QR factorizes the normalized penalized system; R uses the triangular factor of an augmented design and two triangular solves. |
Both backends solve
with a vector right-hand side for a univariate response. For type="QR", Q times R equals X transpose X divided by n plus lambda I. For type="R", R transpose R equals X transpose X plus n times lambda I; Q is not constructed or returned. The names XTX and XTy/XTY always refer to the unpenalized, unnormalized cross-products. Positive penalties allow rank-deficient designs. A zero penalty requires full column rank. Strictly positive penalties also allow more predictors than observations. Each fold uses its own training size in the normalized criterion. Augmented factors are cached for distinct training sizes and updated by removing held-out rows. A factor is recomputed if a numerical downdate fails its cross-product check. The final fit uses the full sample size and the selected penalty. The seed argument resets R's random-number generator; foldid records the generated split.
A named list with the following components:
Fitted coefficient vector of length p.
Fitted responses on the training data.
Observed minus fitted responses.
The unpenalized cross-product X transpose X.
The unpenalized cross-product X transpose y.
Sum of squared residuals.
Centered response sum of squares.
Sum of squared residuals divided by n-p, or NA when n<=p. This is a residual scaling diagnostic, not an unbiased ridge variance estimator.
Legacy residual dimension: n-p when n>=p, and NA when p>n. This is not the effective degrees of freedom of the ridge fitted-value map.
Backend-specific triangular factor; see Details.
Present only for type="QR"; orthogonal factor of the normalized penalized system.
For type="QR", Q transpose times X transpose y/n.
For type="R", the intermediate right-hand side after solving the transposed triangular system.
One minus the residual sum of squares divided by the centered total sum of squares, summing across responses in the multivariate case. NA when the total is zero.
Predictions at X_test, present only when X_test is supplied.
The supplied penalty grid.
Mean held-out squared errors, averaged equally across folds. Each fold error averages across observations and responses.
Smallest value of mse.
Zero-based index of the first minimum, preserving the existing indexing convention.
Selected penalty. Ties are resolved by the first supplied grid entry.
Integer fold labels in the original observation order.
## generate sample data set.seed(10) n <- 30 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- rnorm(n) beta <- rep(1, p) y <- X %*% beta + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrridge_cv(y = y, X = X, lambda = c(1,2), k = 5, seed = 12, X_test = X_test, type = "QR") output$coeff## generate sample data set.seed(10) n <- 30 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) X[,1] <- 1 eps <- rnorm(n) beta <- rep(1, p) y <- X %*% beta + eps X_test <- matrix(rnorm(5 * p, 1), 5, p) output <- fastQR::qrridge_cv(y = y, X = X, lambda = c(1,2), k = 5, seed = 12, X_test = X_test, type = "QR") output$coeff
solves systems of equations , for and , via the QR decomposition.
qrsolve(A, b, type = NULL, nb = NULL)qrsolve(A, b, type = NULL, nb = NULL)
A |
an |
b |
a vector of dimension |
type |
either "QR" or "R". Specifies the type of decomposition to use: "QR" for the QR decomposition or "R" for the Cholesky factorization of |
nb |
number of blocks for the recursive block QR decomposition, default is NULL. |
x a vector of dimension that satisfies .
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## generate sample data set.seed(1234) n <- 10 p <- 4 A <- matrix(rnorm(n * p, 1), n, p) b <- rnorm(n) ## solve the system of linear equations using qr x1 <- fastQR::qrsolve(A = A, b = b) x1 ## solve the system of linear equations using rb qr x2 <- fastQR::qrsolve(A = A, b = b, nb = 2) x2 ## check round(x1 - solve(crossprod(A)) %*% crossprod(A, b), 5) round(x2 - solve(crossprod(A)) %*% crossprod(A, b), 5)## generate sample data set.seed(1234) n <- 10 p <- 4 A <- matrix(rnorm(n * p, 1), n, p) b <- rnorm(n) ## solve the system of linear equations using qr x1 <- fastQR::qrsolve(A = A, b = b) x1 ## solve the system of linear equations using rb qr x2 <- fastQR::qrsolve(A = A, b = b, nb = 2) x2 ## check round(x1 - solve(crossprod(A)) %*% crossprod(A, b), 5) round(x2 - solve(crossprod(A)) %*% crossprod(A, b), 5)
qrupdate provides the update of the QR factorization after the addition of rows or columns to the matrix with . The QR factorization of the matrix returns the matrices and such that . The and matrices are factorized as and , with , such that and upper triangular matrix and . qrupdate accepts in input the matrices and either the complete matrix or the reduced one, . See Golub and Van Loan (2013) for further details on the method.
qrupdate(Q, R, k, U, type = NULL, fast = NULL, complete = NULL)qrupdate(Q, R, k, U, type = NULL, fast = NULL, complete = NULL)
Q |
a |
R |
a |
k |
position where the columns or the rows are added. |
U |
either a |
type |
either 'row' of 'column', for adding rows or columns. Default is 'column'. |
fast |
fast mode: disable to check whether the provided matrices are valid inputs. Default is FALSE. |
complete |
logical expression of length 1. Indicates whether an arbitrary orthogonal completion of the |
A named list containing
the updated Q matrix.
the updated R matrix.
Wide input factors and column additions producing more columns than rows are rejected even in fast mode.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## Add one column ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- qr(X, complete = TRUE) Q <- output$Q R <- output$R ## create column u to be added k <- p+1 u <- matrix(rnorm(n), n, 1) X1 <- cbind(X, u) ## update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = u, type = "column", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Add m columns ## create data: n > p set.seed(1234) n <- 10 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R ## create the matrix of two columns to be added ## in position 2 k <- 2 m <- 2 U <- matrix(rnorm(n*m), n, m) X1 <- cbind(X[,1:(k-1)], U, X[,k:p]) # update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = U, type = "column", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Add one row ## create data: n > p set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the row u to be added u <- matrix(data = rnorm(p), p, 1) k <- n+1 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(u)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(u))) } ## update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = u, type = "row", complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Add m rows ## create data: n > p set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the matrix of rows U to be added: ## two rows in position 5 m <- 2 U <- matrix(data = rnorm(p*m), p, m) k <- 5 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(U)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(U))) } ## update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = U, type = "row", complete = FALSE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1))## Add one column ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- qr(X, complete = TRUE) Q <- output$Q R <- output$R ## create column u to be added k <- p+1 u <- matrix(rnorm(n), n, 1) X1 <- cbind(X, u) ## update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = u, type = "column", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Add m columns ## create data: n > p set.seed(1234) n <- 10 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R ## create the matrix of two columns to be added ## in position 2 k <- 2 m <- 2 U <- matrix(rnorm(n*m), n, m) X1 <- cbind(X[,1:(k-1)], U, X[,k:p]) # update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = U, type = "column", fast = FALSE, complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Add one row ## create data: n > p set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the row u to be added u <- matrix(data = rnorm(p), p, 1) k <- n+1 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(u)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(u))) } ## update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = u, type = "row", complete = TRUE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1)) ## Add m rows ## create data: n > p set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the matrix of rows U to be added: ## two rows in position 5 m <- 2 U <- matrix(data = rnorm(p*m), p, m) k <- 5 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(U)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(U))) } ## update the QR decomposition out <- fastQR::qrupdate(Q = Q, R = R, k = k, U = U, type = "row", complete = FALSE) ## check round(out$Q %*% out$R - X1, 5) max(abs(out$Q %*% out$R - X1))
rchol, provides the Cholesky decomposition of the symmetric and positive definite matrix , where is the input matrix.
rchol(X)rchol(X)
X |
an |
an upper triangular matrix of dimension which represents the Cholesky decomposition of .
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
set.seed(1234) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## compute the Cholesky decomposition of X^TX S <- fastQR::rchol(X = X) S ## check round(S - chol(crossprod(X)), 5)set.seed(1234) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## compute the Cholesky decomposition of X^TX S <- fastQR::rchol(X = X) S ## check round(S - chol(crossprod(X)), 5)
rdowndate provides the update of the thin R matrix of the QR factorization after the deletion of rows or columns to the matrix with . The R factorization of the matrix returns the upper triangular matrix such that . See Golub and Van Loan (2013) for further details on the method.
rdowndate(R, k = NULL, m = NULL, U = NULL, fast = NULL, type = NULL)rdowndate(R, k = NULL, m = NULL, U = NULL, fast = NULL, type = NULL)
R |
a |
k |
position where the columns or the rows are removed. |
m |
number of columns or rows to be removed. It is not required when deleting columns. If NULL, it defaults to the number of columns in |
U |
a |
fast |
fast mode: disable to check whether the provided matrices are valid inputs. Default is FALSE. |
type |
either 'row' of 'column', for removing rows or columns. |
R the updated R matrix.
Require a square reduced R factor; rectangular factors, including wide factors, are rejected even in fast mode.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## Remove one column ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## select the column to be deleted from X and update X k <- 2 X1 <- X[, -k] ## downdate the R decomposition R2 <- fastQR::rdowndate(R = R1, k = k, m = 1, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Remove m columns ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## select the column to be deleted from X and update X k <- 2 X1 <- X[, -c(k,k+1)] ## downdate the R decomposition R2 <- fastQR::rdowndate(R = R1, k = k, m = 2, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Remove one row ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] # select the row to be deleted from X and update X k <- 5 X1 <- X[-k,] U <- as.matrix(X[k,], p, 1) ## downdate the R decomposition R2 <- rdowndate(R = R1, k = k, m = 1, U = U, fast = FALSE, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Remove m rows ## create data: n > p set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## select the rows to be deleted from X and update X k <- 2 m <- 2 X1 <- X[-c(k,k+m-1),] U <- t(X[k:(k+m-1), ]) ## downdate the R decomposition R2 <- rdowndate(R = R1, k = k, m = m, U = U, fast = FALSE, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1)))## Remove one column ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## select the column to be deleted from X and update X k <- 2 X1 <- X[, -k] ## downdate the R decomposition R2 <- fastQR::rdowndate(R = R1, k = k, m = 1, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Remove m columns ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## select the column to be deleted from X and update X k <- 2 X1 <- X[, -c(k,k+1)] ## downdate the R decomposition R2 <- fastQR::rdowndate(R = R1, k = k, m = 2, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Remove one row ## generate sample data set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] # select the row to be deleted from X and update X k <- 5 X1 <- X[-k,] U <- as.matrix(X[k,], p, 1) ## downdate the R decomposition R2 <- rdowndate(R = R1, k = k, m = 1, U = U, fast = FALSE, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Remove m rows ## create data: n > p set.seed(10) n <- 10 p <- 6 X <- matrix(rnorm(n * p, 1), n, p) output <- fastQR::qr(X, type = "householder", nb = NULL, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## select the rows to be deleted from X and update X k <- 2 m <- 2 X1 <- X[-c(k,k+m-1),] U <- t(X[k:(k+m-1), ]) ## downdate the R decomposition R2 <- rdowndate(R = R1, k = k, m = m, U = U, fast = FALSE, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1)))
updates the R factorization when rows or columns are added to the matrix , where . The R factorization of produces an upper triangular matrix such that . For more details on this method, refer to Golub and Van Loan (2013). Columns can only be added in positions through , while the position of added rows does not need to be specified.
rupdate(X, R, U, fast = NULL, type = NULL)rupdate(X, R, U, fast = NULL, type = NULL)
X |
the current |
R |
a |
U |
either a |
type |
either 'row' of 'column', for adding rows or columns. |
fast |
fast mode: disable to check whether the provided matrices are valid inputs. Default is FALSE. |
R the updated R matrix.
Golub GH, Van Loan CF (2013). Matrix computations, Johns Hopkins Studies in the Mathematical Sciences, Fourth edition. Johns Hopkins University Press, Baltimore, MD. ISBN 978-1-4214-0794-4; 1-4214-0794-9; 978-1-4214-0859-0.
Björck Å (2015). Numerical methods in matrix computations, volume 59 of Texts in Applied Mathematics. Springer, Cham. ISBN 978-3-319-05088-1; 978-3-319-05098-8. doi:10.1007/978-3-319-05089-8.
Björck Å (2024). Numerical Methods for Least Squares Problems: Second Edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. doi:10.1137/1.9781611977950. https://doi.org/10.1137/1.9781611977950.
Bernardi M, Busatto C, Cattelan M (2024). “Fast QR updating methods for statistical applications.” 2412.05905, https://arxiv.org/abs/2412.05905.
## Add one column ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create column to be added u <- matrix(rnorm(n), n, 1) X1 <- cbind(X, u) ## update the R decomposition R2 <- fastQR::rupdate(X = X, R = R1, U = u, fast = FALSE, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Add m columns ## generate sample data set.seed(1234) n <- 10 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the matrix of columns to be added m <- 2 U <- matrix(rnorm(n*m), n, m) X1 <- cbind(X, U) # QR update R2 <- fastQR::rupdate(X = X, R = R1, U = U, fast = FALSE, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Add one row ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the row u to be added u <- matrix(data = rnorm(p), p, 1) k <- 5 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(u)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(u))) } ## update the R decomposition R2 <- fastQR::rupdate(R = R1, X = X, U = u, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Add m rows ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the matrix of rows to be added m <- 2 U <- matrix(data = rnorm(p*m), p, m) k <- 5 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(U)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(U))) } ## update the R decomposition R2 <- fastQR::rupdate(R = R1, X = X, U = U, fast = FALSE, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1)))## Add one column ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create column to be added u <- matrix(rnorm(n), n, 1) X1 <- cbind(X, u) ## update the R decomposition R2 <- fastQR::rupdate(X = X, R = R1, U = u, fast = FALSE, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Add m columns ## generate sample data set.seed(1234) n <- 10 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the matrix of columns to be added m <- 2 U <- matrix(rnorm(n*m), n, m) X1 <- cbind(X, U) # QR update R2 <- fastQR::rupdate(X = X, R = R1, U = U, fast = FALSE, type = "column") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Add one row ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the row u to be added u <- matrix(data = rnorm(p), p, 1) k <- 5 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(u)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(u))) } ## update the R decomposition R2 <- fastQR::rupdate(R = R1, X = X, U = u, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1))) ## Add m rows ## generate sample data set.seed(1234) n <- 12 p <- 5 X <- matrix(rnorm(n * p, 1), n, p) ## get the initial QR factorization output <- fastQR::qr(X, complete = TRUE) Q <- output$Q R <- output$R R1 <- R[1:p,] ## create the matrix of rows to be added m <- 2 U <- matrix(data = rnorm(p*m), p, m) k <- 5 if (k<=n) { X1 <- rbind(rbind(X[1:(k-1), ], t(U)), X[k:n, ]) } else { X1 <- rbind(rbind(X, t(U))) } ## update the R decomposition R2 <- fastQR::rupdate(R = R1, X = X, U = U, fast = FALSE, type = "row") ## check max(abs(crossprod(R2) - crossprod(X1)))