TransWikia.com

Confusion matrices for two lists of raster objects

Geographic Information Systems Asked by RandomForester on November 9, 2021

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.

One Answer

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.

Answered by Spacedman on November 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP