On this page:
glmnet:   lasso and elastic-net regularized models
9.3

glmnet: lasso and elastic-net regularized models🔗ℹ

bkc

 (require glmnet) package: glmnet

glmnet is a library for building linear statistical models in Racket. It is a port of the reference implementation by Friedman, Hastie, Tibshirani et al., the R package glmnet, and fits lasso, ridge and elastic-net regularized models in six families:

  • Gaussian — ordinary least squares, ridge, lasso and elastic net for a numeric response.

  • Binomial and multinomial — two-class and K-class logistic classifiers.

  • Cox — proportional-hazards survival models.

  • Poisson — counts, with a log link.

  • Multi-response Gaussian — several numeric responses fitted jointly under a grouped lasso.

This manual has two parts: the User guide works through the library by example, and the Reference documents the public API.

License. This package is distributed under GPL-2.0-or-later.

Acknowledgements. The solver and its algorithms are the work of Jerome Friedman, Trevor Hastie, Rob Tibshirani and the other authors of the R glmnet package, whose vignettes shaped this manual.

AI Disclosure. This package and its documentation were created with the use of AI tools.

    1 User guide

      1.1 Getting started

        1.1.1 Installing

        1.1.2 A First Regression

          1.1.2.1 Plotting the fit and path

            1.1.2.1.1 Predicted versus actual values

            1.1.2.1.2 Residuals versus fitted values

            1.1.2.1.3 The lasso coefficient path

        1.1.3 Fitting from named columns

        1.1.4 Choosing a model

        1.1.5 Predicting

        1.1.6 What is not there yet

      1.2 Concepts

        1.2.1 Data layout

          1.2.1.1 Named data

        1.2.2 The penalty: α and λ

        1.2.3 Model families

        1.2.4 Results

        1.2.5 Regularization paths

        1.2.6 Predictions and coefficients

          1.2.6.1 What is predicted

          1.2.6.2 Predicting along a path

        1.2.7 Choosing λ by cross-validation

          1.2.7.1 Reading lambda-min and lambda-1se

          1.2.7.2 Predicting at the chosen λ

          1.2.7.3 Folds

          1.2.7.4 Measures of error

        1.2.8 Standardization and the intercept

        1.2.9 Convergence and errors

        1.2.10 The native library

      1.3 Data

        1.3.1 Example datasets

        1.3.2 Tables

        1.3.3 Lists and vectors

        1.3.4 CSV files

        1.3.5 Matrices from math/matrix

        1.3.6 Polars dataframes

      1.4 Formulas and named data

        1.4.1 Tables

        1.4.2 Formulas

        1.4.3 A Gaussian fit

        1.4.4 The formula algebra

          1.4.4.1 Interactions and crossing

          1.4.4.2 Powers, and R’s x^2

          1.4.4.3 Removing terms

          1.4.4.4 The intercept

          1.4.4.5 Infix formulas

          1.4.4.6 The response on the right-hand side

          1.4.4.7 Predicting from new data

          1.4.4.8 From R’s syntax

        1.4.5 Transforms

          1.4.5.1 Squares, and R’s I()

          1.4.5.2 Names in a transform

          1.4.5.3 Values and new data

          1.4.5.4 Transforms as data

          1.4.5.5 From R’s transforms

        1.4.6 Categorical predictors

          1.4.6.1 Which columns are factors

          1.4.6.2 Levels and baselines

          1.4.6.3 Treatment contrasts

          1.4.6.4 Factors in interactions

          1.4.6.5 Transforms that make factors

          1.4.6.6 New data and levels

          1.4.6.7 Categorical responses

          1.4.6.8 From R’s factors

        1.4.7 A binomial fit

        1.4.8 A Cox fit

        1.4.9 Multinomial and multi-response fits

        1.4.10 Plots

        1.4.11 Matching R

      1.5 Plots

        1.5.1 Coefficient paths

          1.5.1.1 Lasso and elastic net

          1.5.1.2 The x axis

          1.5.1.3 Labels

          1.5.1.4 Several classes or responses

        1.5.2 Cross-validation curves

        1.5.3 Combining with other plots

        1.5.4 Writing to a file

        1.5.5 Matching R

      1.6 Examples

        1.6.1 Quick start

          1.6.1.1 The data

          1.6.1.2 The path

          1.6.1.3 Coefficients and predictions

          1.6.1.4 Cross-validation

          1.6.1.5 At lambda-min

          1.6.1.6 Matching R

        1.6.2 Ordinary least squares (λ = 0)

          1.6.2.1 The data

          1.6.2.2 Fitting

          1.6.2.3 Fitted values

          1.6.2.4 When to use it

        1.6.3 Ridge regression (L2, α = 0)

          1.6.3.1 The data

          1.6.3.2 Fitting

          1.6.3.3 The shrinkage path

          1.6.3.4 When to use it

        1.6.4 Lasso (L1, α = 1)

          1.6.4.1 The data

          1.6.4.2 Fitting

          1.6.4.3 The selection path

          1.6.4.4 Choosing λ by cross-validation

          1.6.4.5 When to use it

        1.6.5 Elastic net (0 < α < 1)

          1.6.5.1 Fitting

          1.6.5.2 Between ridge and lasso

          1.6.5.3 Correlated predictors

          1.6.5.4 Choosing α and λ

          1.6.5.5 When to use it

        1.6.6 Binomial logistic regression (classification)

          1.6.6.1 The data

          1.6.6.2 Fitting

          1.6.6.3 Predicting

          1.6.6.4 Separation and the penalty

          1.6.6.5 When to use it

        1.6.7 Multinomial classification (K classes)

          1.6.7.1 The data

          1.6.7.2 Fitting

          1.6.7.3 Predicting

          1.6.7.4 Class labels

          1.6.7.5 When to use it

        1.6.8 Cox proportional hazards (survival)

          1.6.8.1 The data

          1.6.8.2 Fitting

          1.6.8.3 Ranking by risk

          1.6.8.4 Varying λ

          1.6.8.5 Censoring and events

          1.6.8.6 When to use it

        1.6.9 Poisson regression (counts)

          1.6.9.1 The data

          1.6.9.2 Fitting

          1.6.9.3 Predicting

          1.6.9.4 The response

          1.6.9.5 When to use it

        1.6.10 Multi-response Gaussian (grouped)

          1.6.10.1 The data

          1.6.10.2 Fitting

          1.6.10.3 Predicting

          1.6.10.4 Grouped versus separate fits

          1.6.10.5 When to use it

        1.6.11 Formula interactions

          1.6.11.1 The data

          1.6.11.2 Crossing weight and horsepower

          1.6.11.3 Every pair

          1.6.11.4 Choosing λ by cross-validation

          1.6.11.5 Matching R

        1.6.12 Formula transforms

          1.6.12.1 The data

          1.6.12.2 A quadratic

          1.6.12.3 R’s hp^2

          1.6.12.4 A logarithm

          1.6.12.5 New data

          1.6.12.6 Matching R

        1.6.13 Formula factors

          1.6.13.1 The data

          1.6.13.2 A factor

          1.6.13.3 A slope for each cylinder count

          1.6.13.4 New cars

          1.6.13.5 Species as classes

          1.6.13.6 Matching R

        1.6.14 Data sources

          1.6.14.1 A table

          1.6.14.2 Lists and vectors

          1.6.14.3 A CSV file

          1.6.14.4 A matrix

          1.6.14.5 A Polars dataframe

          1.6.14.6 Formulas, and back

    2 Reference

      2.1 Common arguments

      2.2 Input data

        2.2.1 Tables

      2.3 Nested lists and vectors

      2.4 CSV files

      2.5 Matrices from math/matrix

      2.6 Polars dataframes

      2.7 Example datasets

      2.8 Gaussian models

      2.9 Binomial models

      2.10 Multinomial models

      2.11 Cox models

      2.12 Poisson models

      2.13 Multi-response Gaussian models

      2.14 Regularization paths

      2.15 Cross-validation

      2.16 Formulas

      2.17 Generic model interface

        2.17.1 Printing

      2.18 Native library

      2.19 Plots