Learn R Programming

fastQR (version 1.1.4)

qr_fitted: Compute fitted values from a QR decomposition

Description

Computes the fitted values \(\widehat y = X\widehat\beta\) for a linear least-squares problem using a QR decomposition stored in compact (Householder) form.

Usage

qr_fitted(qr, tau, y)

Value

a numeric vector of fitted values \(\hat y\).

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 fitted values are computed as $$\widehat 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)
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))

Run the code above in your browser using DataLab