| sparse_lm | R Documentation |
Adjust a linear model with sparse regularization.
We also add a (possibly structured) \ell_2-norm
(ridge-like). The solution path is computed at a grid of values for the
\ell_1-penalty, fixing the amount of \ell_2
regularization. See details for the criterion optimized.
sparse_lm(
x,
y,
type = c("l1", "mcp", "scad"),
lambda1 = NULL,
lambda2 = 0.01,
eta = 3.7,
weights = rep(1, nrow(x)),
penscale = rep(1, ncol(x)),
struct = Matrix::Diagonal(ncol(x), 1),
intercept = TRUE,
normalize = TRUE,
refit = FALSE,
nlambda1 = ifelse(is.null(lambda1), 100, length(lambda1)),
minratio = ifelse(nrow(x) <= ncol(x), 0.01, 1e-04),
maxfeat = ifelse(lambda2 < 0.01, min(nrow(x), ncol(x)), min(4 * nrow(x), ncol(x))),
beta0 = numeric(ncol(x)),
control = list()
)
elastic.net(
x,
y,
lambda1 = NULL,
lambda2 = 0.5,
weights = rep(1, nrow(x)),
penscale = rep(1, ncol(x)),
struct = Matrix::Diagonal(ncol(x), 1),
intercept = TRUE,
normalize = TRUE,
refit = FALSE,
nlambda1 = ifelse(is.null(lambda1), 100, length(lambda1)),
minratio = ifelse(nrow(x) <= ncol(x), 0.01, 1e-04),
maxfeat = ifelse(lambda2 < 0.01, min(nrow(x), ncol(x)), min(4 * nrow(x), ncol(x))),
beta0 = numeric(ncol(x)),
control = list(method = "quadra")
)
elastic_net(
x,
y,
lambda1 = NULL,
lambda2 = 0.5,
weights = rep(1, nrow(x)),
penscale = rep(1, ncol(x)),
struct = Matrix::Diagonal(ncol(x), 1),
intercept = TRUE,
normalize = TRUE,
refit = FALSE,
nlambda1 = ifelse(is.null(lambda1), 100, length(lambda1)),
minratio = ifelse(nrow(x) <= ncol(x), 0.01, 1e-04),
maxfeat = ifelse(lambda2 < 0.01, min(nrow(x), ncol(x)), min(4 * nrow(x), ncol(x))),
beta0 = numeric(ncol(x)),
control = list(method = "quadra")
)
lasso(
x,
y,
lambda1 = NULL,
weights = rep(1, nrow(x)),
penscale = rep(1, ncol(x)),
intercept = TRUE,
normalize = TRUE,
refit = FALSE,
nlambda1 = ifelse(is.null(lambda1), 100, length(lambda1)),
minratio = ifelse(nrow(x) <= ncol(x), 0.01, 1e-04),
maxfeat = min(nrow(x), ncol(x)),
beta0 = numeric(ncol(x)),
control = list(method = "quadra")
)
mcp(
x,
y,
lambda1 = NULL,
lambda2 = 0,
eta = 3,
weights = rep(1, nrow(x)),
penscale = rep(1, ncol(x)),
struct = Matrix::Diagonal(ncol(x), 1),
intercept = TRUE,
normalize = TRUE,
refit = FALSE,
nlambda1 = ifelse(is.null(lambda1), 100, length(lambda1)),
minratio = ifelse(nrow(x) <= ncol(x), 0.01, 1e-04),
maxfeat = ifelse(lambda2 < 0.01, min(nrow(x), ncol(x)), min(4 * nrow(x), ncol(x))),
beta0 = numeric(ncol(x)),
control = list(method = "quadra")
)
scad(
x,
y,
lambda1 = NULL,
lambda2 = 0,
eta = 3.7,
weights = rep(1, nrow(x)),
penscale = rep(1, ncol(x)),
struct = Matrix::Diagonal(ncol(x), 1),
intercept = TRUE,
normalize = TRUE,
refit = FALSE,
nlambda1 = ifelse(is.null(lambda1), 100, length(lambda1)),
minratio = ifelse(nrow(x) <= ncol(x), 0.01, 1e-04),
maxfeat = ifelse(lambda2 < 0.01, min(nrow(x), ncol(x)), min(4 * nrow(x), ncol(x))),
beta0 = numeric(ncol(x)),
control = list(method = "quadra")
)
x |
matrix of features, possibly sparsely encoded
(experimental). Do NOT include intercept. When normalized is
|
y |
response vector. |
type |
string indicating the sparse variant to be fitted. Could be "l1", "mcp" or "scad". Default is "l1". be careful as scad and mcp are still experimental and have not been fully tested yet |
lambda1 |
sequence of decreasing |
lambda2 |
real scalar; tunes the |
eta |
real positive scalar for tuning SCAD or MCP penalties. Default is 3.7. Ignored when type == "l1". |
weights |
vector with real positive values that weight the observations (like in weighted least square). Default sets all weights to 1. |
penscale |
vector with real positive values that weight the penalty of each feature. Default sets all weights to 1. |
struct |
matrix structuring the coefficients, possibly
sparsely encoded. Must be at least positive semidefinite (this is
checked internally). If |
intercept |
logical; indicates if an intercept should be
included in the model. Default is |
normalize |
logical; indicates if variables should be
normalized to have unit L2 norm before fitting. Default is
|
refit |
logical: indicates if the non null coefficients should be refit to avoid excessive bias. Default is FALSE. Can be changed later (both raw and refit coefficients are stored). |
nlambda1 |
integer that indicates the number of values to put
in the |
minratio |
minimal value of |
maxfeat |
integer; limits the number of features ever to
enter the model; i.e., non-zero coefficients for the Elastic-net:
the algorithm stops if this number is exceeded and |
beta0 |
a starting point for the vector of parameter. By default, will initialized zero. May save time in some situation. |
control |
list of argument controlling low level options of the algorithm –use with care and at your own risk– :
|
The optimized criterion is the following:
βhat
λ1,λ2 =
argminβ 1/2 RSS(β) + λ1
penη(D β) + λ/2 2
βT S β,
where
D is a diagonal matrix, whose diagonal terms are provided
as a vector by the penscale argument. The \ell_2
structuring matrix S is provided via the struct
argument, a positive semidefinite matrix (possibly of class
Matrix).
an object with class SparseFit, inheriting from QuadrupenFit.
See also SparseFit
## Simulating multivariate Gaussian with blockwise correlation
## and piecewise constant vector of parameters
beta <- rep(c(0,1,0,-1,0), c(25,10,25,10,25))
cor <- 0.75
Soo <- toeplitz(cor^(0:(25-1))) ## Toeplitz correlation for irrelevant variables
Sww <- matrix(cor,10,10) ## bloc correlation between active variables
Sigma <- Matrix::bdiag(Soo,Sww,Soo,Sww,Soo)
diag(Sigma) <- 1
n <- 50
x <- as.matrix(matrix(rnorm(95*n),n,95) %*% chol(Sigma))
y <- 10 + x %*% beta + rnorm(n,0,10)
labels <- rep("irrelevant", length(beta))
labels[beta != 0] <- "relevant"
## Lasso
plot(lasso(x, y), label=labels)
## SCAD
plot(scad(x, y), label=labels)
## MCP
plot(mcp(x, y), label=labels)
## Elastic-net
plot(elastic_net(x,y,lambda2=1), label=labels)
## Structured Elastic-net (l2-structuring prior)
plot(elastic_net(x,y,lambda2=3,struct=solve(Sigma)), label=labels)
## SCAD + L2
plot(scad(x,y, eta = 3.7, lambda2=1), label=labels)
## MCP + L2
plot(mcp(x, y, eta = 3, lambda2=1), label=labels)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.