How to fix tables with different lengths

1.5k Views Asked by At

First I am a newbie to R (less than two months) and first time asking a question here so I apologize if I do not include all is needed.

I am trying to create a confusion matrix from a random forest using:

library (randomForest)
library (caret)
rfSeg1 <- randomForest(desc ~ Seg1, data= roi_dataSeg1, importance= TRUE, mtry = 4, ntree= 500, trControl = rfSeg1Control, tuneGrid = rfSeg1Grid,metric = "Kappa", maximize = true, na.action=na.exclude)

conftbl <- confusionMatrix(rfSeg1$predicted,roi_dataSeg1$desc)

I was successful using this with a different input data set however fails with new data inputs. I have to create a table with Sensitivity, Specificity, PPV, NPV etc. by class for each study boundary. I get the following error:

Error in table(data, reference, dnn = dnn, ...) : 
  all arguments must have the same length

I ran:

> identical(levels(rfSeg1$predicted),levels(roi_dataSeg1$desc))
[1] TRUE
> identical(length(rfSeg1$predicted),length(roi_dataSeg1$desc))
[1] FALSE
> length(roi_dataSeg1$desc)
[1] 152242
> length(rfSeg1$predicted)
[1] 151567

I have looked online and tried several things but to no avail. Any help will be greatly appreciated to fix this error.

0

There are 0 best solutions below