Reputation: 75
I am learning how to use decision trees in r.
I made a model and did a prediction. I want to check the accuracy of my model. But when ever i tried to make confusion matrix using table function I am getting the error:
Error in table(test_data$Outcome, predictn) : all arguments must have the same length
The code I used is:
data = read.csv("C:/Users/VIJAY/Desktop/ML/logistic regression/diabetes.csv")
head(data)
dim(data)
library(rpart)
library(rpart.plot)
library(caret)
s = sample(768,600)
train_data = data[s,]
test_data = data[-s,]
model = rpart(Outcome ~.,data = train_data, method = "class")
rpart.plot(model,cex = .9)
predictn = predict(model,data= test_data,type = "class")
tab = table(test_data$Outcome,predictn)
Upvotes: 0
Views: 286
Reputation: 63
Your response from the test set and predictions have different lengths. I would say that predictions weren't made for all observations (maybe because of missing values of some predictors - for this consider using surrogate variables or deleting the rows which have missing values in these predictors in the test set).
btw, when you are using caret, there is a nice function caret::confusionMatrix()
Upvotes: 2