confusion matrix - predict function

How do you interpret this code:

"Training confusion matrix"

predicted <- predict(tree, type = "prob")[,1]
predicted.final <- as.factor(ifelse(predicted > 0.5, "Fail", "Pass"))
confusionMatrix(predicted.final, factor(dataset$Pass.Flag))

what does the [,1] mean in the first row?

The predict function is returning an object predicted with a lot of information packed into it, which you can see with your code with str (predicted) Since I don't know what object tree is, I can't give you the specifics (see FAQ: What's a reproducible example (`reprex`) and how do I do one?), but there are whole lot of possible components. Run (from 'help(predict)`

for(fn in methods("predict"))
   try({
       f <- eval(substitute(getAnywhere(fn)$objs[[1]], list(fn = fn)))
       cat(fn, ":\n\t", deparse(args(f)), "\n")
       }, silent = TRUE)

In any even [,1] is a "slice" of the second column of the data packed into a list-like object of the result of running the function predict.

thank you, i've tried the str(predicted), but not sure what this means:

Named num [1:140] 0.6974 0.0992 0.6974 0.6974 0.2473 ...

  • attr(*, "names")= chr [1:140] "4" "8" "13" "17" ...

the first row looks like the probability for each observation.

what does attr(*, "names") mean?

It means that the first component of prediction is a list of 140 numbers and their corresponding labels, generically an attr. If you run

predicted <- predict(tree, type = "prob")

without the trailing slice, you can see the structure of the entire object.

1 Like

ah, got it. Thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.