fitLinearModel: Fit a linear model

View source: R/fitLinearModel.R

fitLinearModelR Documentation

Fit a linear model

Description

No-frills fitting of a linear model to the rows of any matrix-like object with numeric values.

Usage

fitLinearModel(
  x,
  design,
  get.coefs = TRUE,
  subset.row = NULL,
  BPPARAM = SerialParam(),
  rank.error = TRUE
)

Arguments

x

A numeric matrix-like object where columns are samples (e.g., cells) and rows are usually features (e.g., genes).

design

A numeric design matrix with number of rows equal to ncol(x). This should be of full column rank.

get.coefs

A logical scalar indicating whether the coefficients should be returned.

subset.row

An integer, character or logical vector indicating the rows of x to use for model fitting.

BPPARAM

A BiocParallelParam object specifying the parallelization backend to use.

rank.error

Logical scalar indicating whether to throw an error when design is not of full rank.

Details

This function is basically a stripped-down version of lm.fit, made to operate on any matrix representation (ordinary, sparse, whatever). It is generally intended for use inside other functions that require robust and efficient linear model fitting.

If design is not of full rank and rank.error=TRUE, an error is raised. If rank.error=FALSE, NA values are returned for all entries of the output list.

Value

If get.coefs=TRUE, a list is returned containing:

  • coefficents, a numeric matrix of coefficient estimates, with one row per row of x (or a subset thereof specified by subset.row) and one column per column of design.

  • mean, a numeric vector of row means of x. Computed as a courtesy to avoid iterating over the matrix twice.

  • variance, a numeric vector of residual variances per row of x. Computed by summing the residual effects from the fitted model.

  • residual.df, an integer scalar containing the residual degrees of freedom for design.

Otherwise, if get.coefs=FALSE, the same list is returned without coefficients.

Author(s)

Aaron Lun

Examples

y <- Matrix::rsparsematrix(1000, 1000, 0.1)
design <- model.matrix(~runif(1000))

output <- fitLinearModel(y, design)
head(output$coefficients)
head(output$variance)


LTLA/scuttle documentation built on March 9, 2024, 11:16 a.m.