I am trying to perform cross validation for my data set using random forest.
My response variable is of datatype factor with 2 levels (1, 2).
I am using this function below for my cross validation technique
k = 10
Imputed_data$id <- sample(1:k , nrow(Imputed_data), replace = TRUE)
list <- 1:k
prediction <- data.frame()
testsetcopy <- data.frame()
progress.bar <- create_progress_bar("text")
progress.bar$init(k)
for (i in 1:k){
trainingset <- subset(Imputed_data,id %in% list[-i])
testset <- subset(Imputed_data, id %in% c(i))
# run a random forest model
mymodel <- randomForest(trainingset$Accepted~ ., data = trainingset)
temp <- as.data.frame(predict(mymodel, testset[,-13]))
prediction <- rbind(prediction, temp)
testsetcopy <- rbind(testsetcopy, as.data.frame(testset[,13]))
progress.bar$step()
}
result <- cbind(prediction, testsetcopy[,1])
names(result) <- c("Predicted", "Actual")
result$Difference <-abs(result$Actual-result$Predicted)
summary(result$Difference)
I am getting a error in the line
result$Difference <-abs(result$Actual-result$Predicted)
In Ops.factor(result$Actual, result$Predicted) : ‘-’ not meaningful for factors
I could understand that abs cant be used for factors and - is also not used.
I am new to R, and i am unsure how i could then calculate my result. Any lead will be helpful.