I there a way to calculate the accuracy of a classified image (users-, producers accuracy, Kappa) in R, assuming I have a two columns matrix. On of the columns would contain the values that came out with the classification, the other one my observed/checked values?

2 Answers 2


There is a confusionMatrix() function implemented in R:

library(caret) #required for confusionMatrix()

#example values
a <- c(1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1) #values from classification
b <- c(1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0)  #reference values (observed/checked) for validation 

table(a,b) #shows confusion matrix
confusionMatrix(table(a,b)) #confusion matrix with Accuracy, kappa ....

Note: confusionMatrix() only works if there no empty classes in a or b.

  • 1
    Please note that the library containing the function confusionMatrix is caret. Dependencies are accounted for during the install of a package and you do not need to explicitly add the library, in this case e1071. Commented Sep 14, 2016 at 15:24
  • @Jeffrey Evans, you are right. I removed library(e1071) from my code
    – Iris
    Commented Sep 14, 2016 at 15:59
  • I have a function "accuracy" in the rfUtilities package that returns many of the same validation statistics, and some additional ones, with much less overhead than the confusionMatrix function. The output is also considerably less "digested" so one can easily accumulate results in something like a Bootstrap cross-validation. Commented Sep 14, 2016 at 19:49

Users accuracy would be easy as you only calculate the numbers of true/false positives/negatives as ratio. For Kappa and producers accuracy you need the weights (probabilities) for each class. Often they are calculated out of the distribution of the classes of the manual classifier.

Both the general use of kappa, map classification comparison and the implementation in R can be read here:


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.