MassCorr
MassCorr

Reputation: 349

Setting parameter range with caret

I am using the adaptive method to find my best model parameters. However I could not find a way to set the parameter min and max values.

For instance in the following simplify example I would like to force train function to find a k between 8 and 12. Of course I know that, for this simple case, I could use the tuneGrid parameter.

library(caret)
ctrl2 <- trainControl(method = "adaptive_cv",
                      repeats = 5)

mod2 <- train(Species ~ ., data = iris,
              method = "knn",
              tuneLength = 3,
              trControl = ctrl2)

Upvotes: 2

Views: 900

Answers (1)

nadizan
nadizan

Reputation: 1373

You can use tuneGrid to specify which tuning values to chose in the training. Please note that different models (i.e. knn, svm,..) will have different tuning values.

Also, as it states in ?caret::train :

tuneGrid
A data frame with possible tuning values. The columns are named the same as the tuning parameters. Use getModelInfo to get a list of tuning parameters for each model or see http://topepo.github.io/caret/available-models.html. (NOTE: If given, this argument must be named.)

The working code in your case would be:

library(caret)

ctrl2 <- trainControl(method = "adaptive_cv",
                      repeats = 5)

grid_knn <- expand.grid(k=8:12)

set.seed(100)

mod2 <- train(Species ~ ., data = iris,
              method = "knn",
              tuneGrid = grid_knn,
              trControl = ctrl2)

Which gives the output:

> mod2

 k-Nearest Neighbors 

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

No pre-processing
Resampling: Adaptively Cross-Validated (10 fold, repeated 5 times) 
Summary of sample sizes: 135, 135, 135, 135, 135, 135, ... 
Resampling results across tuning parameters:

  k   Accuracy   Kappa  Resamples
   8  0.9600000  0.940   5       
   9  0.9733333  0.960  50       
  10  0.9733333  0.960  50       
  11  0.9746667  0.962  50       
  12  0.9666667  0.950   6       

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

Upvotes: 1

Related Questions