ResidualMatrix-class: The ResidualMatrix class

Description Construction Methods Author(s) Examples

Description

The ResidualMatrix class supports delayed calculation of the residuals from a linear model fit. This serves as a light-weight representation of what would otherwise be a large dense matrix in memory. It also enables efficient matrix multiplication based on features of the the original matrix (e.g., sparsity).

Construction

ResidualMatrix(x, design=NULL, keep=NULL) returns a ResidualMatrix object, given:

When keep=NULL, the ResidualMatrix contains values equivalent to lm.fit(x=design, y=x)$residuals.

Methods

In the following code chunks, x is a ResidualMatrix object:

colSums(x), colMeans(x), rowSums(x) and rowMeans(x) will return the relevant statistics for a ResidualMatrix x.

%*%, crossprod and tcrossprod can also be applied where one or both of the arguments are ResidualMatrix objects.

ResidualMatrix objects are derived from DelayedMatrix objects and support all of valid operations on the latter. All operations not listed here will use the underlying DelayedArray machinery. Unary or binary operations will generally create a new DelayedMatrix instance containing a ResidualMatrixSeed.

Author(s)

Aaron Lun

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
design <- model.matrix(~gl(5, 50))

library(Matrix)
y0 <- rsparsematrix(nrow(design), 200, 0.1)
y <- ResidualMatrix(y0, design)
y

# For comparison:
fit <- lm.fit(x=design, y=as.matrix(y0))
DelayedArray(fit$residuals)

# Keeping some of the factors:
y2 <- ResidualMatrix(y0, design, keep=1:2)
y2
DelayedArray(fit$residuals + design[,1:2] %*% fit$coefficients[1:2,])

# Matrix multiplication:
crossprod(y)
tcrossprod(y)
y %*% rnorm(200)

ResidualMatrix documentation built on Nov. 8, 2020, 7:29 p.m.