
make predictions from a "cv.glmnet" object.
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.
Arguments
- object
Fitted
"cv.glmnet"or"cv.relaxed"object.- newx
Matrix of new values for
xat which predictions are to be made. Must be a matrix; can be sparse as inMatrixpackage. See documentation forpredict.glmnet.- s
Value(s) of the penalty parameter
lambdaat which predictions are required. Default is the values="lambda.1se"stored on the CVobject. Alternativelys="lambda.min"can be used. Ifsis numeric, it is taken as the value(s) oflambdato 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.
References
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.
Author
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:
Trevor Hastie [email protected]
Examples
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.07501387
#> [2,] 0.07501387
#> [3,] 0.07501387
#> [4,] 0.07501387
#> [5,] 0.07501387
coef(cv.fit)
#> 21 x 1 sparse Matrix of class "dgCMatrix"
#> lambda.1se
#> (Intercept) 0.07501387
#> 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"
#> lambda.min
#> (Intercept) 0.06451924
#> V1 -0.03536507
#> V2 .
#> V3 0.05242244
#> V4 .
#> V5 .
#> V6 .
#> V7 0.06397749
#> V8 .
#> V9 .
#> V10 .
#> V11 .
#> V12 .
#> V13 .
#> V14 .
#> V15 .
#> V16 -0.02403396
#> V17 .
#> V18 .
#> V19 .
#> V20 .
predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002))
#> s=0.001 s=0.002
#> [1,] 0.18365095 0.18549497
#> [2,] -0.29444884 -0.28133067
#> [3,] -0.07069425 -0.07378082
#> [4,] 0.28268286 0.28912207
#> [5,] 0.32817600 0.32201042
cv.fitr = cv.glmnet(x, y, relax = TRUE)
predict(cv.fit, newx = x[1:5, ])
#> lambda.1se
#> [1,] 0.07501387
#> [2,] 0.07501387
#> [3,] 0.07501387
#> [4,] 0.07501387
#> [5,] 0.07501387
coef(cv.fit)
#> 21 x 1 sparse Matrix of class "dgCMatrix"
#> lambda.1se
#> (Intercept) 0.07501387
#> 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"
#> lambda.min
#> (Intercept) 0.06451924
#> V1 -0.03536507
#> V2 .
#> V3 0.05242244
#> V4 .
#> V5 .
#> V6 .
#> V7 0.06397749
#> V8 .
#> V9 .
#> V10 .
#> V11 .
#> V12 .
#> V13 .
#> V14 .
#> V15 .
#> V16 -0.02403396
#> V17 .
#> V18 .
#> V19 .
#> V20 .
predict(cv.fit, newx = x[1:5, ], s = c(0.001, 0.002), gamma = "gamma.min")
#> s=0.001 s=0.002
#> [1,] 0.18365095 0.18549497
#> [2,] -0.29444884 -0.28133067
#> [3,] -0.07069425 -0.07378082
#> [4,] 0.28268286 0.28912207
#> [5,] 0.32817600 0.32201042