简体   繁体   中英

confusionMatrix - Error: `data` and `reference` should be factors with the same levels

created a Logistic model:

Banks_Logit<- glm(Banks$Financial.Condition ~ .,data = Banks, family="binomial")
options(scipen=999)

summary(Banks_Logit)

then:

pred <-predict(Banks_Logit,Banks)
gain <-gains(Banks$Financial.Condition,pred,groups=20)

plot(c(0,gain$cume.pct.of.total*sum(Banks$Financial.Condition))~
   c(0,gain$cume.obs), 
 xlab = "Observations", ylab = "Cumulative", main="Model Performance", type="l")
      lines(c(0,sum(Banks$Financial.Condition))~c(0,dim(Banks)[1]),lty=2)

library(caret)
confusionMatrix(ifelse(pred >0.5, 1,0), Banks$Financial.Condition)

error - Error: data and reference should be factors with the same levels.

this is the pred data

        1                        2                        3                        4                        5                        6                        7                        8                        9                       10 
0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999138624584560 0.9999999999891036051025 0.9999999999995110577800 0.9999999999999997779554 
                      11                       12                       13                       14                       15                       16                       17                       18                       19                       20 
0.0000000000176082421301 0.0000000000352379135751 0.0000000000431425778626 0.0000000000000002220446 0.0000000000002227450487 0.0000000000000002220446 0.0000000000000002220446 0.0000000000000002220446 0.0000000000000002220446 0.0000000000000002220446 



str(pred)
 Named num [1:20] 1 1 1 1 1 ...
 - attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...



this is the dataset (Str(Banks):
'data.frame':   20 obs. of  5 variables:
 $ Obs                : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Financial.Condition: int  1 1 1 1 1 1 1 1 1 1 ...
 $ TotCap.Assets      : num  9.7 1 6.9 5.8 4.3 9.1 11.9 8.1 9.3 1.1 ...
 $ TotExp.Assets      : num  0.12 0.11 0.09 0.1 0.11 0.13 0.1 0.13 0.16 0.16 ...
 $ TotLns.Lses.Assets : num  0.65 0.62 1.02 0.67 0.69 0.74 0.79 0.63 0.72 0.57 ...

Some example data:

Banks = data.frame(Obs = 1:100,Financial.Condition=rbinom(100,1,0.5),
TotCap.Assets = runif(100),
TotExp.Assets = runif(100),TotLns.Lses.Assets = runif(100))

You can just provide the table for confusionMatrix , to get the other metrics:

library(caret)

Banks_Logit<- glm(Banks$Financial.Condition ~ .,data = Banks, family="binomial")
pred <-predict(Banks_Logit,Banks)
confusionMatrix(table(ifelse(pred >0.5, 1,0), Banks$Financial.Condition))

Confusion Matrix and Statistics

   
     0  1
  0 36 33
  1  8 23
                                          
               Accuracy : 0.59            
                 95% CI : (0.4871, 0.6874)
    No Information Rate : 0.56            
    P-Value [Acc > NIR] : 0.3084356       
                                          
                  Kappa : 0.2158          
                                          
 Mcnemar's Test P-Value : 0.0001781       
                                          
            Sensitivity : 0.8182          
            Specificity : 0.4107          
         Pos Pred Value : 0.5217          
         Neg Pred Value : 0.7419          
             Prevalence : 0.4400          
         Detection Rate : 0.3600          
   Detection Prevalence : 0.6900          
      Balanced Accuracy : 0.6144          
                                          
       'Positive' Class : 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM