I’m a total noob at R trying to complete a Stats II project. I keep getting the following error for the code below: [1] “Confusion Matrix: TRAINING set based on random forest model built using 2 trees”
Error in table(train.data$heart_data, train.data.predict): all arguments must have the same length
Traceback:
-
table(train.data$heart_data, train.data.predict)
-
stop(“all arguments must have the same length”)
set.seed(6522048) library(randomForest) model_rf1 <- randomForest(target ~ age+sex+cp+trestbps+chol+restecg+exang+ca, data=train.data, ntree = 2)
print(“======================================================================================================================”)
print(‘Confusion Matrix: TRAINING set based on random forest model built using 2 trees’)
train.data.predict <- predict(model_rf1, train.data, type = “class”)conf.matrix <- table(train.data$heart_data, train.data.predict)[,c('YES','NO')] rownames(conf.matrix) <- paste("Actual", rownames(conf.matrix), sep = ": ") colnames(conf.matrix) <- paste("Prediction", colnames(conf.matrix), sep = ": ") format(conf.matrix,justify="centre",digit=2) print("============================================================================================== ========================") print('Confusion Matrix: TESTING set based on random forest model built using 2 trees') test.data.predict <- predict(model_rf1, test.data, type = "class") conf.matrix <- table(test.data$heart_data, test.data.predict)[,c('YES','NO')] (conf.matrix) <- paste("Actual", rownames(conf.matrix), sep = ": ") colnames(conf.matrix) <- paste("Prediction", colnames(conf.matrix), sep = ": ") format(conf.matrix,justify="centre",digit=2)`
Thanks in advance for any help!
Jeff Routson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.