Learn R Programming

fastQR (version 1.1.4)

qr_R: Reconstruct the R, matrix from a QR object.

Description

returns the \(R\) matrix of the full QR decomposition. If \(r = \mathrm{rank}(X) < p\), then only the reduced \(R \in \mathbb{R}^{r \times p}\) matrix is returned.

Usage

qr_R(qr, rank = NULL, pivot = NULL, complete = NULL)

Value

returns part or all of \(R\). If complete is TRUE, \(R\) has \(n\) rows. If complete is FALSE, \(R\) has \(p\) rows.

Arguments

qr

object representing a QR decomposition. This will typically have come from a previous call to qr.

rank

the rank of x as computed by the decomposition.

pivot

a vector of length \(p\), specifying the permutation of the columns of \(X\) applied during the QR decomposition process. The default is NULL if no pivoting has been applied.

complete

logical flag (length 1). Indicates whether the \(R\) matrix is to be completed by binding zero-value rows beneath the square upper triangle. If \(r = \mathrm{rank}(X) < p\), then only the reduced \(R \in \mathbb{R}^{r \times p}\) matrix is returned.

References

golub_van_loan.2013fastQR

bjorck.2015fastQR

bjorck.2024fastQR

bernardi_etal.2024fastQR

Examples

Run this code
## generate sample data
set.seed(1234)
n <- 12
p <- 5
X <- matrix(rnorm(n * p), n, p)

## get the full QR decomposition with pivot
qr_res <- fastQR::qr_fast(X = X,
                          tol = sqrt(.Machine$double.eps),
                          pivot = TRUE)

## get the full R matrix
R1 <- qr_R(qr_res$qr, complete = TRUE)

## check that X^TX = R^TR
## get the permutation matrix
P <- qr_pivot2perm(pivot = qr_res$pivot)
max(abs(crossprod(R1 %*% P) - crossprod(X)))
max(abs(crossprod(R1) - crossprod(X %*% t(P))))

## get the reduced R matrix
R2 <- qr_R(qr_res$qr, qr_res$rank, complete = FALSE)

## check that X^TX = R^TR
## get the permutation matrix
P <- qr_pivot2perm(pivot = qr_res$pivot)
max(abs(crossprod(R2 %*% P) - crossprod(X)))
max(abs(crossprod(R2) - crossprod(X %*% t(P))))

Run the code above in your browser using DataLab