cv.glmnet
returns an object of type cv.glmnet
(using dots in class names is asking for trouble with method dispatching, but this does not seem to be the problem here), which has overwritten methods for predict
and coef
, namely predict.cv.glmnet
and coef.cv.glmnet
. When you read their docs with
> ?coef.cv.glmnet
> ?predict.cv.glmnet
you will notice that they have an option s
for specifying the lambda used. The coefficients seen in your question refer to s="lambda.1se"
. Set it to s="lambda.min"
, and the result is identical with your model trained with lambda.min
:
> coef(cv.ridge, s="lambda.min")
19 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) -1.468326e+03
(Intercept) .
PrivateYes -5.278781e+02
Accept 1.004588e+00
...
> pred.out <- predict(cv.ridge, glmnet(model.matrix(Apps~.,College), s="lambda.min")
> mean((pred.out- College$AppsCollege$Apps, alpha=0, nfold=100)
> coef(cv.ridge, s="lambda.min")
19 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) -1.468326e+03
(Intercept) .
PrivateYes -5.278781e+02
Accept 1.004588e+00
...
> pred.out <- predict(cv.ridge, model.matrix(Apps~.,College), s="lambda.min")
> mean((pred.out- College$Apps)^2)
[1] 1358455
Remark: What do your data sets train
and test
contain? Apparently somethindsomething different from College
because your MSE and coefficients are different.