rf_model: functions/random_forest_fast.R Train a Random Forest Model...

View source: R/rf.R

rf_modelR Documentation

functions/random_forest_fast.R Train a Random Forest Model using Ranger

Description

This function trains a Random Forest model using the high-performance ranger package. It natively utilizes sparse matrices (dgCMatrix) to avoid memory exhaustion and utilizes Out-Of-Bag (OOB) error for rapid hyperparameter tuning.

Usage

rf_model(
  train_vectorized,
  Y,
  test_vectorized,
  parallel = FALSE,
  tune = FALSE,
  weights = NULL
)

Arguments

train_vectorized

The training feature matrix (e.g., a 'dfm' from quanteda).

Y

The response variable for the training set. Should be a factor.

test_vectorized

The test feature matrix, which must have the same features as 'train_vectorized'.

parallel

Logical

tune

Logical. If TRUE, tunes 'mtry' using native OOB error

weights

A numeric vector of observation weights. Default is NULL

Value

A list containing four elements:

pred

A vector of class predictions for the test set.

probs

A matrix of predicted probabilities.

model

The final, trained 'ranger' model object.

best_lambda

Placeholder (NULL) for pipeline consistency.

Examples

## Not run: 
# Create dummy vectorized training and test data
train_matrix <- matrix(runif(100), nrow = 10, ncol = 10)
test_matrix <- matrix(runif(50), nrow = 5, ncol = 10)

# Provide column names (vocabulary) required by ranger
colnames(train_matrix) <- paste0("word", 1:10)
colnames(test_matrix) <- paste0("word", 1:10)

y_train <- factor(sample(c("P", "N"), 10, replace = TRUE))

# Run random forest model
model_results <- rf_model(train_matrix, y_train, test_matrix)

## End(Not run)

quickSentiment documentation built on Aug. 29, 2026, 1:07 a.m.