caret
package automatically converts factor variables to one-hot encoding. We can also convert the factor variable to a numeric variable before training any model. Here is a minimal, reproducible example
library(caret)
#Split into train and test dataset
set.seed(123)
trainIndex <- createDataPartition(iris$Sepal.Length, p = .8,
list = FALSE,
times = 1)
train <- iris[ trainIndex,]
test <- iris[-trainIndex,]
#Cross validation
fitControl <- trainControl(
method = "cv",
number = 10)
#Fit RF model
#Approach 1 (one-hot encoding)
set.seed(123)
rf.fit1 <- train(Sepal.Length ~ ., data = train,
method = "ranger",
trControl = fitControl,
preProcess=c("center", "scale"))
#Prediction for calibration data
rf_cal1 <- predict(rf.fit1, newdata = train)
#Prediction for validation data
rf_val1 <- predict(rf.fit1, newdata = test)
#Calculation of RMSE for training and testing data
RMSE(rf_cal1, train$Sepal.Length)
#> [1] 0.1843668
RMSE(rf_val1, test$Sepal.Length)
#> [1] 0.389818
#Approach 2
#Conversion of character to numeric variable
train$Species <- as.numeric(train$Species)
test$Species <- as.numeric(test$Species)
set.seed(123)
rf.fit2 <- train(Sepal.Length ~ ., data = train,
method = "ranger",
trControl = fitControl,
preProcess=c("center", "scale"))
#Prediction for calibration data
rf_cal2 <- predict(rf.fit2, newdata = train)
#Prediction for validation data
rf_val2 <- predict(rf.fit2, newdata = test)
#Calculation of RMSE for training and testing data
RMSE(rf_cal2, train$Sepal.Length)
#> [1] 0.1915434
RMSE(rf_val2, test$Sepal.Length)
#> [1] 0.3909754
I want to know the opinion of the community on whether we should use one-hot encoding or character to numeric conversion is also statistically okay.