0
$\begingroup$

I'm trying to use 5-fold cv for training set and test set but I can't determine the confusion matrix using table() to calculate the classification rate (true positives and true negatives divided by total number) because the sets have different lengths but I'm probably doing it wrong:

(after defining dataset using read.table and column names)

#turn dataset lines into random order for splitting
variavel <- runif(nrow(dataset))
dataset <- dataset[order(variavel),]

#create 5 folds and set variable (vector) to save accuracy rates 
folds <- createFolds(dataset$Class, k=5)
str(folds)
Accuracy <- 0

ListFoldsTrain <- list()
ListFoldsTest <- list()

for (i in 1:5){

  trainingset<- dataset[-folds[[i]],]
  ListFoldsTrain[[i]]<- trainingset


  testset<- dataset[folds[[i]],]
  ListFoldsTest[[i]] <- testset

#run classification tree model
  tree.1 <-     rpart(Region~palmitic+palmitoleic+stearic+oleic+linoleic+eicosanoic+linolenic+eicosenoic, data=trainingset)


  #Confusion matrix
  tabletree <- table(trainingset$Region, predict(tree.1, type="class"))

  #Accuracy for each fold
  Accuracy[i] = sum(diag(tabletree))/length(trainingset[,1]);Accuracy[i]

}

#Accuracy for each fold
print(Accuracy)

But now how can I get the accuracy of the tree model for the test set?

$\endgroup$

1 Answer 1

1
$\begingroup$

It is almost the same as what you already have. You just need to specify the test data in your predict statement.

testtable <- table(testset$Region, 
    predict(tree.1, newdata=testset, type="class"))

#Accuracy for each fold
Accuracy[i] = sum(diag(testtable))/length(testset[,1])
$\endgroup$
2
  • $\begingroup$ G5W I tried to comment your answer but I don't have enough reputation to do so. When I run the code you wrote I get the following error: Error in table(trainingset$Region, predict(tree.1, newdata = testset, : all arguments must have the same length. Since test set is about 20% and training around 80%, any ideas? $\endgroup$
    – AntonioS
    Dec 9, 2017 at 12:17
  • $\begingroup$ Yes, I had an error. First part of table should have ben test, not training. Changing answer. $\endgroup$
    – G5W
    Dec 9, 2017 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.