Heisenberg
Heisenberg

Reputation: 8806

How to pick a different model from the `finalModel` in caret?

In caret, using functions best, tolerance, etc. I can pick a less complex model with a small performance penalty (tutorial).

After using tolerance, I now know that I want, say, the 3rd models among all the ones tuned by caret. Is it possible to extract that model similar to how I can pick caret_result$finalModel? Or do I have to take the hyperparameters of that model and re-fit the model with them?

Upvotes: 1

Views: 1469

Answers (1)

topepo
topepo

Reputation: 14316

See update.train:

> mod1 <- train(Species ~ ., data = iris, method = "rpart")
> mod1
CART 

150 samples
4 predictor
3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Bootstrapped (25 reps) 
Summary of sample sizes: 150, 150, 150, 150, 150, 150, ... 
Resampling results across tuning parameters:

  cp    Accuracy   Kappa    
0.00  0.9434796  0.9145259
0.44  0.7609620  0.6544837
0.50  0.4731651  0.2350673

Accuracy was used to select the optimal model using  the largest value.
The final value used for the model was cp = 0. 

> update(mod1, param = list(cp = .44))
CART 

150 samples
4 predictor
3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Bootstrapped (25 reps) 
Summary of sample sizes: 150, 150, 150, 150, 150, 150, ... 
Resampling results across tuning parameters:

  cp    Accuracy   Kappa    
0.00  0.9434796  0.9145259
0.44  0.7609620  0.6544837
0.50  0.4731651  0.2350673

The tuning parameter was set manually.
The final value used for the model was cp = 0.44. 

Upvotes: 2

Related Questions