Search code examples
rpredictconfusion-matrix

Why am I getting error for this confusion matrix in R?


I am using a binary value "Mpg01", where Mpg01 depicts if mileage is over median of data then 1 else 0: I have run a logistic regression however when creating a confusion matrix: I am receiving the below error:

Input:

table(carslogic$Mpg01, predict > 0.5)

Output:
> table(carslogic$Mpg01, predict > 0.5)
Error in predict > 0.5 : 
  comparison (6) is possible only for atomic and list types

What is the reason for this error?How can i fix it?


Solution

  • Maybe you should use predict() > 0.5 See this as example

    data(mtcars)
    fit <- glm((cyl>4) ~ mpg, data=mtcars )
    table((mtcars$cyl>4), predict(fit)>0.5)
    
            FALSE TRUE
      FALSE     9    2
      TRUE      0   21