9.3

1.6 Examples🔗ℹ

First the Quick Start of R glmnet’s vignette, on R’s own data, then one section per model, then three on formulas, and one that fits the same data from every data source. Each starts from the literate program of the same name in "glmnet/examples/", whose companion under "glmnet/examples/test/" runs it and checks the result the prose promises, then goes further: it varies λ or α to show what the penalty does, and uses the family’s prediction helpers on new data.

The small fixtures of the model sections are chosen so that the right answer is known in advance: a response built from some predictors plus a column of pure noise, which a good fit should leave out. Where a section prints a sweep over λ, it rounds the numbers to three decimal places so the rows line up. The Quick Start uses R glmnet’s QuickStartExample, and the formula and data-source sections R’s mtcars and iris; their numbers are R’s.

    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