MLElasticNet: Elastic Net (coordinate-descent path)

MLElasticNetR Documentation

Elastic Net (coordinate-descent path)

Description

Fits the elastic-net coefficient path via coordinate descent over a glmnet-style lambda grid (or an explicit grid). Reports coefficients in the original predictor scale, intercepts, the deviance-ratio path, and predictions for new data. The mixing parameter alpha interpolates between lasso (alpha = 1) and ridge (alpha = 0).

Methods

Public methods


MLElasticNet$new()

Initialize the MLElasticNet object.

Usage
MLElasticNet$new(low_memory = FALSE, n_threads = NULL)
Arguments
low_memory

Logical (default FALSE). FALSE: the coordinate descent runs on a centered and standardized working copy of X (one n x p matrix while fitting). TRUE: it runs on X itself (an mmap_matrix is read in place, "readonly" suffices) with no allocation of the size of X; same estimator and grid, the two engines agree to rounding (about 1e-12 in the coefficients).

n_threads

Integer, the thread budget of the fit's matrix products (NULL, the default: the library's default budget, trex_get_num_threads(), initially 1).


MLElasticNet$fit()

Fit the EN path over an auto-generated glmnet-style grid.

Usage
MLElasticNet$fit(
  X,
  y,
  alpha = 1,
  n_lambda = 100,
  lambda_min_ratio = -1,
  standardize = TRUE,
  intercept = TRUE,
  use_strong_rule = FALSE,
  max_iter = 1e+05,
  tol = 1e-07
)
Arguments
X

Predictor matrix (n x p), or an mmap_matrix (bound zero-copy; "readonly" suffices, the fit does not modify X).

y

Response vector (n).

alpha

Mixing in [0, 1]: 1 = lasso, 0 = ridge (default 1).

n_lambda

Grid size (default 100).

lambda_min_ratio

Ratio lambda_min/lambda_max; negative (default) auto-selects glmnet's rule.

standardize

Standardize columns to unit SD (default TRUE).

intercept

Fit an unpenalized intercept (default TRUE).

use_strong_rule

Enable the sequential strong rule (default FALSE).

max_iter

Max CD sweeps per lambda (default 100000).

tol

Convergence tolerance (default 1e-7).

Returns

Invisibly returns self.


MLElasticNet$fit_grid()

Fit the EN path at an explicit lambda grid (sorted descending internally); use to evaluate at exactly glmnet's sequence.

Usage
MLElasticNet$fit_grid(
  X,
  y,
  lambda_grid,
  alpha = 1,
  standardize = TRUE,
  intercept = TRUE,
  use_strong_rule = FALSE,
  max_iter = 1e+05,
  tol = 1e-07
)
Arguments
X

Predictor matrix (n x p), or an mmap_matrix (zero-copy).

y

Response vector (n).

lambda_grid

Explicit lambda values (any order, > 0).

alpha

Mixing in [0, 1] (default 1).

standardize

Standardize columns to unit SD (default TRUE).

intercept

Fit an unpenalized intercept (default TRUE).

use_strong_rule

Enable the sequential strong rule (default FALSE).

max_iter

Max CD sweeps per lambda (default 100000).

tol

Convergence tolerance (default 1e-7).

Returns

Invisibly returns self.


MLElasticNet$get_coef()

Coefficient path in original predictor scale (p x n_lambda).

Usage
MLElasticNet$get_coef()
Returns

Numeric matrix.


MLElasticNet$get_intercepts()

Intercept per lambda (zero if intercept = FALSE).

Usage
MLElasticNet$get_intercepts()
Returns

Numeric vector.


MLElasticNet$get_lambdas()

Lambda grid actually used (descending).

Usage
MLElasticNet$get_lambdas()
Returns

Numeric vector.


MLElasticNet$get_dev_ratio()

Fraction of null deviance explained per lambda (glmnet %Dev).

Usage
MLElasticNet$get_dev_ratio()
Returns

Numeric vector.


MLElasticNet$converged()

Whether every lambda reached the convergence tolerance.

Usage
MLElasticNet$converged()
Returns

Logical scalar.


MLElasticNet$n_nonconverged()

Number of lambdas whose coordinate descent hit max_iter.

Usage
MLElasticNet$n_nonconverged()
Returns

Integer.


MLElasticNet$predict()

Predictions per lambda for new data.

Usage
MLElasticNet$predict(X_new)
Arguments
X_new

Predictor matrix with the same number of columns as the fit, or an mmap_matrix (zero-copy).

Returns

Numeric matrix (nrow(X_new) x n_lambda).


MLElasticNet$clone()

The objects of this class are cloneable with this method.

Usage
MLElasticNet$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

References

H. Zou and T. Hastie, "Regularization and Variable Selection via the Elastic Net," J. R. Stat. Soc. Ser. B Stat. Methodol., vol. 67, no. 2, pp. 301-320, 2005.

J. Friedman, T. Hastie, and R. Tibshirani, "Regularization Paths for Generalized Linear Models via Coordinate Descent," J. Stat. Softw., vol. 33, no. 1, pp. 1-22, 2010, doi: 10.18637/jss.v033.i01.

See Also

Other ML methods: MLElasticNetCV, MLLpNormScaler, MLPCA, MLRidge, MLRidgeCV, MLSVD, MLZScoreScaler, ml_agglomerative_cluster(), ml_cut_tree(), ml_distance_metric, ml_linkage_method

Examples

set.seed(1)
n <- 50; p <- 8
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 2 + X[, 3] - X[, 5] + rnorm(n)
en <- MLElasticNet$new()
en$fit(X, y, alpha = 1.0)
dim(en$get_coef())        # p x n_lambda
en$predict(X[1:5, ])


TRexSelector documentation built on Nov. 20, 2026, 5:09 p.m.