gelnet

Generalized Elastic Nets

gelnet implements several extensions of the elastic net regularization scheme: individual feature penalties for the L1 term, feature-feature penalties for the L2 term, and translation coefficients for the L2 term. It supports linear regression, binary logistic regression and one-class logistic regression models.

References

Installation

The package can be installed directly from GitHub using

if( !require(devtools) ) install.packages("devtools")
# devtools::install_github("ArtemSokolov/gelnet")
install.packages("gelnet", repos = c('https://artemsokolov.r-universe.dev', 'https://cloud.r-project.org'))

A grammar of regularization

Inspired by ggplot2, gelnet models are assembled from independent terms using the + operator. A fully-specified model definition is then provided to gelnet_train() for training. Consider the following example:

library( gelnet )

model <- gelnet(X) + model_lin(y) + rglz_L1(0.1) + rglz_L2(1, P = L)
fit <- gelnet_train( model )

where the building blocks are:

The terms can be added in any order, and a partially-built definition can be reused as the starting point for several models:

base <- gelnet(X) + model_lin(y) + rglz_L2(1, P = L)

sparse    <- base + rglz_L1(0.1)
sparser   <- base + rglz_L1(0.5)
five_feat <- base + rglz_nf(5)

Once assembled, a definition is fit with gelnet_train():

model <- gelnet_train( sparse )

Applying a model

gelnet_train() returns a list with the fitted weights w (and, for supervised tasks, a bias term b). Inference on new samples can be done directly with the dot product against the weights:

scores <- Xnew %*% fit$w + fit$b

For model_lin(), scores are the predicted response values directly. For model_blr() and model_oclr(), scores are the logits of the underlying logistic model; convert them to probabilities with the sigmoid function, e.g. 1 / (1 + exp(-scores)).

See vignette("basics") for a complete walkthrough.