# Confusion matrices for two lists of raster objects

I have two lists of raster objects. One contains validation plots and the other contains results from a random forest binary classification of those plots. I would like to loop through both lists with caret::confusionMatrix to assess accuracy of the classifier. However, some rasters only have one class present, and caret::confusionMatrix returns the

Error: there must be at least two levels in the data.

• How are you casting the rasters to factors for confusionMatrix? Supply a levels argument to factor giving all the possible levels. Jun 2 '20 at 18:28

Sample data - you should make something like this in your question to illustrate:

> r1 = raster(matrix(sample(1:3,25,TRUE),5,5))
> r2 = raster(matrix(sample(1:3,25,TRUE),5,5))


Confusion matrix of a raster is an error:

> confusionMatrix(r1,r2)
Error: data and reference should be factors with the same levels.


So convert to a factor with the same levels:

> confusionMatrix(factor(r1[],levels=1:3),factor(r2[],levels=1:3))
Confusion Matrix and Statistics

Reference
Prediction 1 2 3
1 5 1 2
2 3 5 5
3 3 0 1


Then if one of your rasters is lacking levels, then it still works:

> r3 = raster(matrix(1,5,5))
> confusionMatrix(factor(r1[],levels=1:3),factor(r3[],levels=1:3))
Confusion Matrix and Statistics

Reference
Prediction  1  2  3
1  8  0  0
2 13  0  0
3  4  0  0


The only condition here is that you have to know all the possible values in advance. If a sudden "42" sneaks in somewhere then it will break. Get all unique values in your rasters beforehand and then create factors with that.