wrap_glmnet: S3 wrapper for glmnet

View source: R/wrap_glmnet.R

wrap_glmnetR Documentation

S3 wrapper for glmnet

Description

Fits a 'glmnet' penalized regression model with a consistent interface. Supports regression and binary classification.

Usage

wrap_glmnet(x, y, ...)

## S3 method for class 'wrap_glmnet'
predict(object, newx, type = c("class", "prob"), s = NULL, ...)

## S3 method for class 'wrap_glmnet'
print(x, ...)

Arguments

x

A matrix or data.frame of features.

y

A factor or character vector for classification, numeric for regression.

...

Additional arguments passed to [glmnet::glmnet()]. Pass 'family = "binomial"' for binary classification.

object

A fitted 'wrap_glmnet' object.

newx

A matrix or data.frame of new observations.

type

'"class"' (default) for class labels, '"prob"' for a probability matrix. Ignored for regression.

s

Lambda value for prediction. Defaults to the midpoint of the lambda path. Pass 's = cv_fit$lambda.min' if using [glmnet::cv.glmnet()].

Value

An object of class 'wrap_glmnet' with fields:

fit

The fitted glmnet model.

levels

Class levels (NULL for regression).

task

"classification" or "regression".

Note

Multiclass ('family = "multinomial"') is not yet supported. For lambda selection, a specific 's' value can be passed to 'predict()'. By default the midpoint of the lambda path is used. For optimal lambda, use [glmnet::cv.glmnet()] externally and pass 's = fit$lambda.min'.

Examples


X <- iris[iris$Species != "virginica", 1:4]
y <- droplevels(iris[iris$Species != "virginica", "Species"])
mod <- wrap_glmnet(X, y, family = "binomial")
predict(mod, newx = X, type = "class")
predict(mod, newx = X, type = "prob")


X <- iris[iris$Species != "virginica", 1:4]
y <- droplevels(iris[iris$Species != "virginica", "Species"])
mod <- wrap_glmnet(X, y, family = "binomial")
predict(mod, newx = X, type = "class")
predict(mod, newx = X, type = "prob")


mlS3 documentation built on April 29, 2026, 1:08 a.m.