I am trying to find the optimal values for the parameters "ntree", "mtry" and "nodesize" for a random forest model. I tried the method below, but I could not automatically tune the values for "ntree". Could you please guide me to find a better solution to this problem. Please note that I am using this code on a large raster data set and cross-validating with "random search".
# Set up cross-validation with random search
train_control <- trainControl(method = "cv", number = 5, search = "random")
# Define hyperparameter space for random search
tune_grid <- data.frame(
mtry = sample(1:30, 10, replace = TRUE),
min.node.size = sample(c(1:30), 10, replace = TRUE),node size
splitrule = "variance"
)
# Run the random forest model with Random Search
set.seed(123)
rf_model <- train(
LST ~ .,
data = train_data,
method = "ranger",
tuneLength = 10,
num.trees = 1000
)
# View the best model performance and hyperparameters
print(rf_model$bestTune)
#Predict on the training data
train_predictions <- predict(rf_model, newdata = train_data[, -5])
#Predict on the validation data
validation_predictions3 <- predict(rf_model, newdata = validation_data[, -5])