Learn R Programming

fastQR (version 1.1.4)

qr_coef: Compute least-squares coefficients from a QR decomposition

Description

Computes the coefficient vector \(\widehat\beta\) solving the least-squares problem \(\min_\beta \|y - X\beta\|_2\), using a QR decomposition stored in compact (Householder) form.

Usage

qr_coef(qr, tau, y, pivot = NULL, rank = NULL)

Value

a numeric vector of regression coefficients.

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\).

pivot

optional integer vector of length \(p\) containing the 1-based column permutation used during the QR factorization. If supplied, the returned coefficients are reordered to match the original column order.

rank

optional integer specifying the numerical rank of \(X\). If supplied, only the leading rank components are used in the triangular solve.

Details

The coefficients are obtained by first computing \(Q^\top y\) and then solving the resulting upper-triangular system involving the matrix \(R\). The orthogonal matrix \(Q\) is never formed explicitly.

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)
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))

Run the code above in your browser using DataLab