Learn R Programming

fastQR (version 1.0.0)

qrridge_cv: Cross-validation of the RIDGE estimator for the linear regression model

Description

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

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

the matrix \(X^\top X\).

XTy

\(X^\top y\).

sigma_hat

estimated residual variance.

df

degrees of freedom.

Q

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

R

\(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.

lambda

a vector of lambdas.

k

an integer vector defining the number of groups for CV.

seed

ad integer number defining the seed for random number generation.

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)
beta      <- rep(1, p)
y         <- X %*% beta + eps
X_test    <- matrix(rnorm(5 * p, 1), 5, p)
output    <- fastQR::qrridge_cv(y = y, X = X, lambda = c(1,2), 
                                k = 5, seed = 12, X_test = X_test, type = "QR")
output$coeff

Run the code above in your browser using DataLab