Error in ConfusionMatrix the data and reference factors must have the same number of levels

后端 未结 9 976
自闭症患者
自闭症患者 2020-12-08 20:13

I\'ve trained a tree model with R caret. I\'m now trying to generate a confusion matrix and keep getting the following error:

Error in confusionMatrix

相关标签:
9条回答
  • 2020-12-08 20:47

    make sure you installed the package with all its dependencies:

    install.packages('caret', dependencies = TRUE)
    
    confusionMatrix( table(prediction, true_value) )
    
    0 讨论(0)
  • 2020-12-08 20:50

    Try use:

    confusionMatrix(table(Argument 1, Argument 2)) 
    

    Thats worked for me.

    0 讨论(0)
  • 2020-12-08 21:01

    The length problem you're running into is probably due to the presence of NAs in the training set -- either drop the cases that are not complete, or impute so that you do not have missing values.

    0 讨论(0)
  • 2020-12-08 21:01

    If your data contains NAs then sometimes it will be considered as a factor level,So omit these NAs initially

    DF = na.omit(DF)
    

    Then,if your model fit is predicting some incorrect level,then it is better to use tables

    confusionMatrix(table(Arg1, Arg2))
    
    0 讨论(0)
  • 2020-12-08 21:02

    I had same issue but went ahead and changed it after reading data file like so..

    data = na.omit(data)

    Thanks all for pointer!

    0 讨论(0)
  • 2020-12-08 21:04

    Try specifying na.pass for the na.action option:

    predictionsTree <- predict(treeFit, testdata,na.action = na.pass)
    
    0 讨论(0)
提交回复
热议问题