R caret train glmnet final model lambda values not as specified
The final model is basically a refit with your whole dataset AFTER alpha
and lambda
were optimized using resampling techniques.
If you print model$finalModel$call
you see the call being made (omitted x, y structure for shortness):
Call: glmnet(x, y, family = "binomial", alpha = 1)
Here, alpha
is set (if you had set a sequence it would be the optimum alpha
found), but no specified lambda is set to train, and therefore an automatic sequence is generated based on your data and the model is fitted. It then predicts with the same training set with lambdaOpt
(and the rest of the sequence you gave). Take a look at glmnet vignette and how you can specify different lambda
after training.
If you type:
> names(model$modelInfo) [1] "label" "library" "type" "parameters" "grid" "loop" [7] "fit" "predict" "prob" "predictors" "varImp" "levels" [13] "tags" "sort" "trim"
and then walk through each of those sections, you can take a look at what train
is doing. You can see in model$modelInfo$predict
how it predicts on lambdaOpt
and the rest of your sequence.
When you print model$results
you actually get your list of lambda
and the performance on the whole training set with each one:
alpha lambda Accuracy Kappa AccuracySD KappaSD1 1 1.000000e-05 0.5698940 0.15166891 0.09061320 0.171335242 1 1.832981e-05 0.5698940 0.15166891 0.09061320 0.171335243 1 3.359818e-05 0.5698940 0.15166891 0.09061320 0.171335244 1 6.158482e-05 0.5698940 0.15166891 0.09061320 0.171335245 1 1.128838e-04 0.5698940 0.15166891 0.09061320 0.171335246 1 2.069138e-04 0.5698940 0.15166891 0.09061320 0.171335247 1 3.792690e-04 0.5698940 0.15166891 0.09061320 0.171335248 1 6.951928e-04 0.5698940 0.15166891 0.09061320 0.171335249 1 1.274275e-03 0.5675708 0.14690433 0.09071728 0.1708566510 1 2.335721e-03 0.5643334 0.14059590 0.09153010 0.1720403611 1 4.281332e-03 0.5629588 0.13822063 0.09403553 0.1771544112 1 7.847600e-03 0.5694974 0.15221600 0.08791315 0.1643392213 1 1.438450e-02 0.5700431 0.15448347 0.08864353 0.1650933214 1 2.636651e-02 0.5695053 0.15189752 0.08113581 0.1518461915 1 4.832930e-02 0.5635977 0.14112303 0.05833646 0.1161722616 1 8.858668e-02 0.5305835 0.08983718 0.08116759 0.1475230717 1 1.623777e-01 0.4800871 0.01124082 0.05827521 0.0571529818 1 2.976351e-01 0.4725241 0.00000000 0.04488500 0.0000000019 1 5.455595e-01 0.4725241 0.00000000 0.04488500 0.0000000020 1 1.000000e+00 0.4725241 0.00000000 0.04488500 0.00000000
To summarise what's happening in caret+glmnet:
optimizes
alpha
andlambda
within the tuneGrid you provided using resampling techniques;refits the model, now on the whole training set, with optimal
alpha
;predicts on the whole training set with
lambdaOpt
found in 1. and on the rest of the sequence of lambdas in tuneGrid.