Learn R Programming

text2emotion (version 0.1.0)

tune_rf_model: Tune Random Forest Model Hyperparameters

Description

This function performs hyperparameter tuning for a Random Forest model using grid search. It searches over the grid of `mtry` (number of variables to consider at each split) and `ntree` (number of trees in the forest) to find the best model based on training accuracy.

Usage

tune_rf_model(
  train_matrix,
  train_labels,
  mtry_grid = c(5, 10, 20),
  ntree_grid = c(100, 200, 300),
  seed = 123,
  verbose = TRUE
)

Value

A list containing the best hyperparameters (`mtry`, `ntree`, and `accuracy`):

  • `mtry`: The best number of variables to consider at each split.

  • `ntree`: The best number of trees in the forest.

  • `accuracy`: The accuracy achieved by the model with the best hyperparameters.

Arguments

train_matrix

A sparse matrix (class `dgCMatrix`) representing the training feature data.

train_labels

A factor vector representing the training labels.

mtry_grid

A vector of values to search for the `mtry` parameter (number of variables to consider at each split). Default is `c(5, 10, 20)`.

ntree_grid

A vector of values to search for the `ntree` parameter (number of trees in the forest). Default is `c(100, 200, 300)`.

seed

A seed value for reproducibility. Default is `123`.

verbose

A logical indicating whether to print progress information during the grid search. Default is `TRUE`.

Details

The function trains multiple Random Forest models using different combinations of `mtry` and `ntree` values, and evaluates their performance based on training accuracy. The hyperparameters that give the highest accuracy are returned as the best parameters. The process uses the `ranger` package for training the Random Forest model.