How to resolve different levels in confusion matrix in R




While using SVM in R through this code:
ctrl <- trainControl(method=“repeatedcv”,repeats = 10)
svmfit <- train(as.factor(Recommended) ~ ., data = train1,method = “svmRadial”,trControl = ctrl)
prediction <- predict(svmfit, newdata = test1, type = ‘raw’)
I got the following error:

As can be seen there are two levels in the prediction whereas there are none in the response column.
Now this has happened because before doing some feature engineering I had combined the train and test data:
#combine data set
test$Recommended <- 0
merge <- rbind(train, test)

Though this is not giving a problem in fitting a model however I would like to know how can i see the confusion matrix from this.
I am using caret and kernlab packages.
Can someone please help me with this???


Hi @pagal_guy,

So problem could be these -

  • test1$Recommended is not a factor variable

  • test1$Recommended have less levels as compared to your prediction data

Anyways, Try this -
test1$Recommended <- as.factor(test1$Recommended)
levels(test_1$Recommended) <- levels(prediction)

Hope this helps.