Learn R Programming

fastQR (version 1.1.4)

qr_lse_resid: Compute residuals using QR decomposition

Description

Computes the residual vector \(r = y - \hat y\) for a linear least-squares problem using a QR decomposition computed internally.

Usage

qr_lse_resid(X, y)

Value

a numeric vector of residuals.

Arguments

X

numeric matrix of dimension \(n \times p\).

y

numeric response vector of length \(n\).

Details

The QR decomposition of \(X\) is computed internally. The residuals are obtained as $$r = y - Q Q^\top y$$ without explicitly forming the orthogonal matrix \(Q\).

This function is intended as a convenience wrapper for least-squares computations when the explicit QR factors are not required.

Examples

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

Run the code above in your browser using DataLab