Search code examples
rr-caretglmnetlasso-regression

Keeping one parameter fixed and search on randomly in caret


I would like to keep the parameter alpha fixed at 1 and use random search for lambda, is this possible?

library(caret)

X <- iris[, 1:4]
Y <- iris[, 5]

fit_glmnet <- train(X, Y, method = "glmnet", tuneLength = 2, trControl = trainControl(search = "random"))

Solution

  • I do not think this can be achieved by specifying directly in caret train but here is how to emulate the desired behavior:

    From this link

    one can see random search for lambda is achieved by:

    lambda = 2^runif(len, min = -10, 3)
    

    where len is the tune length

    To emulate random search over one parameter:

    len <- 2
    fit_glmnet <- train(X, Y,
                        method = "glmnet",
                        tuneLength = len,
                        trControl = trainControl(search = "grid"),
                        tuneGrid = data.frame(alpha = 1, lambda = 2^runif(len, min = -10, 3)))