How to use caret functions to achieve the same results as this for loop? Classification Random Forest caret
I want to run monte carlo cv 100 times on my data, my data is 2 class classification I want to use the caret package to achieve the same thing, mainly I'm confused about how to average the results of accuracy for example using caret?
# Initialize data for Binary classification
dataset$ID <- factor(dataset$ID)
# Initialize number of iterations
num_iter=100
# Vectors to store results
acc_vec<-c()
# Function with for loop
rf <- function(dataset)
{
for (i in 1:num_iter)
{
trainIndex <-createDataPartition(dataset$ID, p=0.8,list=FALSE)
dataset_train <-dataset[trainIndex, ]
dataset_test <-dataset[-trainIndex, ]
rf <- randomForest(ID~., data=dataset_train, importance=TRUE)
x_test <- dataset_test[, -1]
y_test <- dataset_test$ID
predictions <- predict(rf, x_test)
acc_vec <- append(acc_vec, accuracy(actual= y_test, predicted= predictions))
}
print(mean(acc_vec))
}
from Recent Questions - Stack Overflow https://ift.tt/3i9fsIT
https://ift.tt/eA8V8J
Comments
Post a Comment