glmnet (version 4.1-1)

predict.cv.glmnet: make predictions from a "cv.glmnet" object.

Description

This function makes predictions from a cross-validated glmnet model, using the stored "glmnet.fit" object, and the optimal value chosen for lambda (and gamma for a 'relaxed' fit.

Usage

# S3 method for cv.glmnet
predict(object, newx, s = c("lambda.1se", "lambda.min"), ...)

# S3 method for cv.relaxed predict( object, newx, s = c("lambda.1se", "lambda.min"), gamma = c("gamma.1se", "gamma.min"), ... )

Arguments

object

Fitted "cv.glmnet" or "cv.relaxed" object.

newx

Matrix of new values for x at which predictions are to be made. Must be a matrix; can be sparse as in Matrix package. See documentation for predict.glmnet.

s

Value(s) of the penalty parameter lambda at which predictions are required. Default is the value s="lambda.1se" stored on the CV object. Alternatively s="lambda.min" can be used. If s is numeric, it is taken as the value(s) of lambda to be used. (For historical reasons we use the symbol 's' rather than 'lambda' to reference this parameter)

Not used. Other arguments to predict.

gamma

Value (single) of 'gamma' at which predictions are to be made

Value

The object returned depends on the … argument which is passed on to the predict method for glmnet objects.

Details

This function makes it easier to use the results of cross-validation to make a prediction.

References

Friedman, J., Hastie, T. and Tibshirani, R. (2008) Regularization Paths for Generalized Linear Models via Coordinate Descent, Journal of Statistical Software, Vol. 33, Issue 1, Feb 2010 https://www.jstatsoft.org/v33/i01/ https://arxiv.org/abs/1707.08692 Hastie, T., Tibshirani, Robert, Tibshirani, Ryan (2019) Extended Comparisons of Best Subset Selection, Forward Stepwise Selection, and the Lasso

See Also

glmnet, and print, and coef methods, and cv.glmnet.

Examples

Run this code
# NOT RUN {
x = matrix(rnorm(100 * 20), 100, 20)
y = rnorm(100)
cv.fit = cv.glmnet(x, y)
predict(cv.fit, newx = x[1:5, ])
coef(cv.fit)
coef(cv.fit, s = "lambda.min")
predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002))
cv.fitr = cv.glmnet(x, y, relax = TRUE)
predict(cv.fit, newx = x[1:5, ])
coef(cv.fit)
coef(cv.fit, s = "lambda.min", gamma = "gamma.min")
predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002), gamma = "gamma.min")

# }

Run the code above in your browser using DataCamp Workspace