predict.cv.glmnet.RdThis 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.
Fitted "cv.glmnet" or "cv.relaxed" object.
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.
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.
Value (single) of 'gamma' at which predictions are to be made
The object returned depends on the ... argument which is passed
on to the predict method for glmnet objects.
This function makes it easier to use the results of cross-validation to make a prediction.
Friedman, J., Hastie, T. and Tibshirani, R. (2008)
Regularization Paths for Generalized Linear Models via Coordinate
Descent (2010), Journal of Statistical Software, Vol. 33(1), 1-22,
doi:10.18637/jss.v033.i01
.
Simon, N., Friedman, J., Hastie, T. and Tibshirani, R. (2011)
Regularization Paths for Cox's Proportional
Hazards Model via Coordinate Descent, Journal of Statistical Software, Vol.
39(5), 1-13,
doi:10.18637/jss.v039.i05
.
Hastie, T., Tibshirani, Robert and Tibshirani, Ryan (2020) Best Subset,
Forward Stepwise or Lasso? Analysis and Recommendations Based on Extensive Comparisons,
Statist. Sc. Vol. 35(4), 579-592,
https://arxiv.org/abs/1707.08692.
Glmnet webpage with four vignettes, https://glmnet.stanford.edu.
glmnet, and print, and coef methods, and
cv.glmnet.
x = matrix(rnorm(100 * 20), 100, 20)
y = rnorm(100)
cv.fit = cv.glmnet(x, y)
predict(cv.fit, newx = x[1:5, ])
#> lambda.1se
#> [1,] 0.09337702
#> [2,] 0.09337702
#> [3,] 0.09337702
#> [4,] 0.09337702
#> [5,] 0.09337702
coef(cv.fit)
#> 21 x 1 sparse Matrix of class "dgCMatrix"
#> s0
#> (Intercept) 0.09337702
#> V1 .
#> V2 .
#> V3 .
#> V4 .
#> V5 .
#> V6 .
#> V7 .
#> V8 .
#> V9 .
#> V10 .
#> V11 .
#> V12 .
#> V13 .
#> V14 .
#> V15 .
#> V16 .
#> V17 .
#> V18 .
#> V19 .
#> V20 .
coef(cv.fit, s = "lambda.min")
#> 21 x 1 sparse Matrix of class "dgCMatrix"
#> s0
#> (Intercept) 0.09337702
#> V1 .
#> V2 .
#> V3 .
#> V4 .
#> V5 .
#> V6 .
#> V7 .
#> V8 .
#> V9 .
#> V10 .
#> V11 .
#> V12 .
#> V13 .
#> V14 .
#> V15 .
#> V16 .
#> V17 .
#> V18 .
#> V19 .
#> V20 .
predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002))
#> s0 s1
#> [1,] 0.135033994 0.1255374559
#> [2,] 0.563796290 0.5593714796
#> [3,] 0.189078763 0.1902399348
#> [4,] -0.345108903 -0.3324224776
#> [5,] 0.001156588 -0.0004136382
cv.fitr = cv.glmnet(x, y, relax = TRUE)
predict(cv.fit, newx = x[1:5, ])
#> lambda.1se
#> [1,] 0.09337702
#> [2,] 0.09337702
#> [3,] 0.09337702
#> [4,] 0.09337702
#> [5,] 0.09337702
coef(cv.fit)
#> 21 x 1 sparse Matrix of class "dgCMatrix"
#> s0
#> (Intercept) 0.09337702
#> V1 .
#> V2 .
#> V3 .
#> V4 .
#> V5 .
#> V6 .
#> V7 .
#> V8 .
#> V9 .
#> V10 .
#> V11 .
#> V12 .
#> V13 .
#> V14 .
#> V15 .
#> V16 .
#> V17 .
#> V18 .
#> V19 .
#> V20 .
coef(cv.fit, s = "lambda.min", gamma = "gamma.min")
#> 21 x 1 sparse Matrix of class "dgCMatrix"
#> s0
#> (Intercept) 0.09337702
#> V1 .
#> V2 .
#> V3 .
#> V4 .
#> V5 .
#> V6 .
#> V7 .
#> V8 .
#> V9 .
#> V10 .
#> V11 .
#> V12 .
#> V13 .
#> V14 .
#> V15 .
#> V16 .
#> V17 .
#> V18 .
#> V19 .
#> V20 .
predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002), gamma = "gamma.min")
#> s0 s1
#> [1,] 0.135033994 0.1255374559
#> [2,] 0.563796290 0.5593714796
#> [3,] 0.189078763 0.1902399348
#> [4,] -0.345108903 -0.3324224776
#> [5,] 0.001156588 -0.0004136382