# How to calculate error in rpart in r

#1

hello,

``````library(adabag)
data(Vehicle)
l <- length(Vehicle[,1])
# Create samples from 67% of data,create samples from 1:l
sub <- sample(1:l,2*l/3)
mfinal <- 15
maxdepth <- 5

Vehicle.rpart <- rpart(Class~.,data=Vehicle[sub,],maxdepth=maxdepth)
Vehicle.rpart.pred <- predict(Vehicle.rpart,newdata=Vehicle[-sub, ],type="class")
**tb <- table(Vehicle.rpart.pred,Vehicle\$Class[-sub])**
**error.rpart <- 1-(sum(diag(tb))/sum(tb))**
``````

I am unable to understand what exactly is being done in the last two lines.I do understand that some calculation is being done to calculate the error but I am not 100% clear about it.
Can someone kindly explain this to me.

#2

From what I understand.

sum(tb) gives the total number of instances.

sum(diag(tb)) gives the instances with correct predictions.

by taking the ratio you are calculating the accuracy and by subtracting from one , you are getting the error rate.

tb is like this A B C
A 2 3 1
B 4 7 0
C 2 1 9

where columns gives the correct classes
row row sides represents the classes predicted.

By taking sum of all the cases in matrix, you get the total number of instances.
summing diagonal of this matrix gives you the correct instances.

Rest you are simply taking the ratio and all. Let me know if you still donâ€™t get it.