Learn R Programming

fastQR (version 1.1.4)

qr_lse_coef: Compute least-squares coefficients using QR decomposition

Description

Computes the coefficient vector \(\hat\beta\) solving the least-squares problem \(\min_\beta \|y - X\beta\|_2\), using a QR decomposition computed internally.

Usage

qr_lse_coef(X, y)

Value

a numeric vector of regression coefficients.

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

This function is intended as a convenience wrapper for least-squares estimation 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)

coef1 <- fastQR::qr_lse_coef(X, y)

## reference computation
coef2 <- base::qr.coef(base::qr(X), y)

max(abs(coef1 - coef2))

Run the code above in your browser using DataLab