Learn R Programming

fastQR (version 1.1.4)

qr_resid: Compute residuals from a QR decomposition

Description

Computes the residual vector \(r = y - \widehat y\) for a linear least-squares problem using a QR decomposition stored in compact (Householder) form.

Usage

qr_resid(qr, tau, y)

Value

a numeric vector of residuals of dimension \(n\).

Arguments

qr

numeric matrix containing the QR decomposition of \(X\) in compact form (as returned by qr_fast()).

tau

numeric vector of Householder coefficients.

y

numeric response vector of length \(n\).

Details

The residuals are computed as $$r = y - Q Q^\top y$$ without explicitly forming the orthogonal matrix \(Q\). The computation relies on the Householder reflectors stored in qr and tau.

Examples

Run this code
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))

Run the code above in your browser using DataLab