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, reduce = TRUE, reQR = 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.

reduce

A logical value for whether redundant parameters in X should be removed. This should be TRUE (default) for most cases.

reQR

A logical value for whether to re-perform QR if reduce = TRUE, and X has been reduced.

...

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 changes from dense to sparse, or vice versa, and redundant columns removed.

rank

The rank of the X matrix.

fix

Logical value for whether redundant columns were removed form X. TRUE means columns were removed.

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)
QR$fixed
dim(QR$X) # Reduced again
colnames(QR$X)


RRPP documentation built on June 22, 2024, 6:45 p.m.