Learn R Programming

fastQR (version 1.1.4)

qrls: Ordinary least squares for the linear regression model

Description

qrls, or LS for linear regression models, solves the following optimization problem $$\textrm{min}_\beta ~ \frac{1}{2}\|y-X\beta\|_2^2,$$ for \(y\in\mathbb{R}^n\) and \(X\in\mathbb{R}^{n\times p}\), to obtain a coefficient vector \(\widehat{\beta}\in\mathbb{R}^p\). The design matrix \(X\in\mathbb{R}^{n\times p}\) contains the observations for each regressor.

Usage

qrls(y, X, X_test = NULL, type = NULL)

Value

A named list containing

coeff

a length-\(p\) vector containing the solution for the parameters \(\beta\).

fitted

a length-\(n\) vector of fitted values, \(\widehat{y}=X\widehat{\beta}\).

residuals

a length-\(n\) vector of residuals, \(\varepsilon=y-\widehat{y}\).

residuals_norm2

the L2-norm of the residuals, \(\Vert\varepsilon\Vert_2^2.\)

y_norm2

the L2-norm of the response variable. \(\Vert y\Vert_2^2.\)

XTX_Qmat

\(Q\) matrix of the QR decomposition of the matrix \(X^\top X\).

XTX_Rmat

\(R\) matrix of the QR decomposition of the matrix \(X^\top X\).

QXTy

\(QX^\top y\), where \(Q\) matrix of the QR decomposition of the matrix \(X^\top X\).

R2

\(R^2\), coefficient of determination, measure of goodness-of-fit of the model.

predicted

predicted values for the test set, \(X_{\text{test}}\widehat{\beta}\). It is only available if X_test is not NULL.

Arguments

y

a vector of length-\(n\) response vector.

X

an \((n\times p)\) full column rank matrix of predictors.

X_test

an \((q\times p)\) full column rank matrix. Test set. By default it set to NULL.

type

either "QR" or "R". Specifies the type of decomposition to use: "QR" for the QR decomposition or "R" for the Cholesky factorization of \(A^\top A\). The default is "QR".

Examples

Run this code

## generate sample data
set.seed(10)
n         <- 30
p         <- 6
X         <- matrix(rnorm(n * p, 1), n, p)
X[,1]     <- 1
eps       <- rnorm(n, sd = 0.5)
beta      <- rep(0, p)
beta[1:3] <- 1
beta[4:5] <- 2
y         <- X %*% beta + eps
X_test    <- matrix(rnorm(5 * p, 1), 5, p)
output    <-  fastQR::qrls(y = y, X = X, X_test = X_test)
output$coeff

Run the code above in your browser using DataLab