QRforX: QR decomposition of linear model design matrices

View source: R/QRforX.r

QRforXR Documentation

QR decomposition of linear model design matrices

Description

This function performs a QR decomposition (factorization) on a linear model design matrix (X) and returns useful results for subsequent analysis. This is intended as an internal function but can be used externally. Because base::qr and Matrix::qr have different options for QR algorithms, this function assures that results are consistent for other RRPP function use, whether X is a dense or sparse matrix.

Usage

QRforX(X, returnQ = TRUE, ...)

Arguments

X

A linear model design matrix, but can be any object coercible to matrix.

returnQ

A logical value whether to return the Q matrix. Generating a Q matrix can be computationally intense for large matrices. If it is not explicitly needed, this argument can be FALSE.

...

Further arguments passed to base::qr.

Value

An object of class QR is a list containing the following:

Q

The Q matrix, if requested.

R

The R matrix.

X

The X matrix, which could be changed from dense to sparse, or vice versa, and redundant columns removed.

dimnames

The dimnames associated with Q, R, or X

rank

The rank of the X matrix.

S4

Logical value for whether Q, R, and X are S4 class objects.

Author(s)

Michael Collyer

Examples

## Simple Example
data(Pupfish)
fit <- lm.rrpp(coords ~ Pop, data = Pupfish, print.progress = FALSE)
QR <- QRforX(model.matrix(fit))
QR$Q
QR$R
QR$rank
QR$S4

## Not run, but one could get base::qr and Matrix::qr results as

# base::qr(as.matrix(QR$X))
# Matrix::qr(QR$X)

## Complex example

data("PupfishHeads")
fit <- suppressWarnings(lm.rrpp(headSize ~ sex + 
locality/year, data = PupfishHeads))
X <- model.matrix(fit)
dim(X) # Already reduced
colnames(X)
X <- model.matrix(terms(fit), fit$LM$data)
dim(X) # Retains redundant parameters
colnames(X)
QR <- QRforX(X)
dim(QR$X)
dim(QR$Q)
dim(QR$R)
QR$rank


RRPP documentation built on July 7, 2026, 1:07 a.m.