Sonia
Sonia

Reputation: 482

How tunelength parameter works in caret

I'm using following code to implement elastic net using R

model <- train(
Sales ~., data = train_data, method = "glmnet",
trControl = trainControl("cv", number = 10),
tuneLength = 10
)

I'm confused about tunelength paramater. In Cran I'm seeing that

To change the candidate values of the tuning parameter, either of the tuneLength or tuneGrid arguments can be used. The train function can generate a candidate set of parameter values and the tuneLength argument controls how many are evaluated. In the case of PLS, the function uses a sequence of integers from 1 to tuneLength. If we want to evaluate all integers between 1 and 15, setting tuneLength = 15 would achieve this

But train function is taking dependent & independent variable from my data then how it's using tuneLength parameter? Can you please help me understand?

Upvotes: 1

Views: 9233

Answers (1)

Dean
Dean

Reputation: 499

In caret the train() function has a number of arguments to help select the "optimal" tuning parameters for your chosen model.

Model tuning is explained in detail in package documentation here.

Users can customize the tuning process by specifying a grid of possible parameter values that the model will use when training the model.

For some models, the use of tuneLength is an alternative to specifying a tuneGrid.

For example, one method of searching for the 'optimal' model parameters is using random selection. In this case the tuneLength argument is used to control the number of combinations generated by this random tuning parameter search.

To use random search, another option is available in trainControl called search. Possible values of this argument are "grid" and "random". The built-in models contained in caret contain code to generate random tuning parameter combinations. The total number of unique combinations is specified by the tuneLength option to train.

It is covered in more detail here: http://topepo.github.io/caret/random-hyperparameter-search.html

It is important to check the model you are using in the train function and look at which tuning parameters are used for that model. It will then be easier to understand how to correctly customize the model fitting process.

For your example of using method = 'glmnet' here is a comparison using tuneGrid and tuneLength (taken from package tests):

cctrl1 <- trainControl(method = "cv", number = 3, returnResamp = "all",
                       classProbs = TRUE, summaryFunction = twoClassSummary)

test_class_cv_model <- train(trainX, trainY, 
                             method = "glmnet", 
                             trControl = cctrl1,
                             metric = "ROC",
                             preProc = c("center", "scale"),
                             tuneGrid = expand.grid(.alpha = seq(.05, 1, length = 15),
                                                    .lambda = c((1:5)/10)))

cctrlR <- trainControl(method = "cv", number = 3, returnResamp = "all", search = "random")


test_class_rand <- train(trainX, trainY, 
                         method = "glmnet", 
                         trControl = cctrlR,
                         tuneLength = 4)


Upvotes: 3

Related Questions