Ridge regression (also known as Tikhonov regularization) shrinks the regression coefficients by adding a quadratic penalty term to the optimization problem. minimizeβ 12||y−Xβ||22+λ||β||22 with X∈Rm×n, y∈Rm, β∈Rn and 0<λ∈R.
It is well known that β can be estimated by the following formula. ˆβ=(X⊤X+λI)−1X⊤y here I∈Rn,n refers to the identity matrix.
The optimization problem can be rewritten into a quadratic optimization problem minimize(β,γ)12γ⊤γ+λβ⊤βsubject toy−Xβ=γ with γ∈Rn.
In R the packages glmnet and MASS provide functionality for ridge regression.
longley[,1]
y <- as.matrix(longley[,-1]) x <-
library(slam)
Sys.setenv(ROI_LOAD_PLUGINS = FALSE)
library(ROI)
library(ROI.plugin.qpoases)
function(...) {
dbind <- function(x, y) {
.dbind <- simple_triplet_zero_matrix(NROW(x), NCOL(y))
A <- simple_triplet_zero_matrix(NROW(y), NCOL(x))
B <-rbind(cbind(x, A), cbind(B, y))
}Reduce(.dbind, list(...))
}
function(x, y, lambda) {
qp_ridge <- simple_triplet_diag_matrix
stdm <- NROW(x); n <- NCOL(x)
m <- dbind(stdm(2 * lambda, n), stdm(1, m))
Q0 <- c(b = double(n), g = double(m))
a0 <- OP(objective = Q_objective(Q = Q0, L = a0))
op <- cbind(x, stdm(1, m))
A1 <-constraints(op) <- L_constraint(A1, eq(m), y)
bounds(op) <- V_bound(ld = -Inf, nobj = ncol(Q0))
op
}
qp_ridge(x, y, 0)
op <- ROI_solve(op, "qpoases")) (qp0 <-
## Optimal solution found.
## The objective value is: 6.616251e+00
cbind(round(coef(lm.fit(x, y)), 3), round(head(solution(qp0), ncol(x)), 3))
## [,1] [,2]
## GNP 0.217 0.217
## Unemployed 0.021 0.021
## Armed.Forces 0.004 0.004
## Population -1.700 -1.700
## Year 0.117 0.117
## Employed -0.311 -0.311
qp_ridge(x, y, 10)
op <- ROI_solve(op, "qpoases")) (qp1 <-
## Optimal solution found.
## The objective value is: 1.087148e+01
head(solution(qp1), NCOL(x))
## [1] 0.11039939 0.01231982 0.01120254 -0.24179694 0.03957795 0.04689382