Learn R Programming

quickSentiment (version 0.1.0)

rf_model: 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 handles the necessary conversion from a sparse DFM to a dense matrix and corrects for column name inconsistencies.

Usage

rf_model(train_vectorized, Y, test_vectorized, parallel = FALSE)

Value

A list containing two elements:

pred

A vector of class predictions for the test set.

model

The final, trained `ranger` model object.

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

Examples

Run this code
# Create dummy vectorized data
train_matrix <- matrix(runif(100), nrow = 10)
test_matrix <- matrix(runif(50), nrow = 5)
y_train <- factor(sample(c("P", "N"), 10, replace = TRUE))

# Run model
model_results <- rf_model(train_matrix, y_train, test_matrix)
print(model_results$pred)

Run the code above in your browser using DataLab