# Define hyperparameter grid
param_grid <- expand.grid(
ntree = ntree_values,
mtry = mtry_values,
importance = importance_values
)
results <- data.frame()
# Grid search: train and evaluate a model for each combination
for (i in 1:nrow(param_grid)) {
params <- param_grid[i, ]
model <- randomForest(
as.formula(paste(target, "~ .")),
data = train,
ntree = params$ntree,
mtry = params$mtry,
importance = params$importance
)
preds <- predict(model, newdata = test)
cm <- confusionMatrix(preds, test[[target]])
}