Question

In: Computer Science

How do you make a prediction from a decision tree using confusionMatrix function in rstudio?

How do you make a prediction from a decision tree using confusionMatrix function in rstudio?

Solutions

Expert Solution

Confusion matrix is used in decision tree to predict the result of classification problem. In this the number of correct and incorrect prediction are summarized and are split into classes. It basically shows how classification model gets confused while making prediction.

I am taking here the example of one of the most popular data set “Titanic”. I have created decision tree model using the “rpart” method. The main aim of tree here is to predict whether a person would have survived based on the given variables. We compare the result of prediction with the actual value given in variable “Survived” of “Titanic” data set.

Pls follow the the comment in code below. Confusion matrix is created and prediction is made along with calculating the accuracy.

Code-

# Installing rpart package
install.packages("rpart")
# Loading the package
library(rpart)
# Set random seed
set.seed(1)
# Loading Titanic data
data("Titanic")
# Change data from table to data frame
titanic <- as.data.frame(Titanic)
# Check structure of data
str(titanic)

# Using rpart to make decision tree classification model
tree <- rpart(Survived ~ ., data = titanic, method = "class")

# Using the predict() method to make predictions, and storing result in pred
pred <- predict(tree, titanic, type = "class")

# table() method is used here to make confusion matrix

table(titanic$Survived, pred)
# Accuracy calculation
# pred
# No Yes
# No 13 (TP) 3(FN)
# Yes 5(FP) 11(TN)
# Accuracy = (TP + TN)/(TP+FN+FP+TN)
# Accuracy = (13+11)/13+3+5+11
# = 24/32 = 0.75 = 75%

Code screenshot-

Output-


Related Solutions

How do you make a decision table?
How do you make a decision table?
Using Haskell, how do you make a function i.e test that will take a list of...
Using Haskell, how do you make a function i.e test that will take a list of integers and add a 5 to the end of that list ex: test [1, 2, 3] will be changed to test [1, 2, 3, 5]
Design a decision tree that computes the logical AND function. How does it compare to the...
Design a decision tree that computes the logical AND function. How does it compare to the Perceptron solution? Can a perceptron be used to implement a 3 input exclusive NOR gate?
What do you see as the pros and cons to decision tree analysis? When do you...
What do you see as the pros and cons to decision tree analysis? When do you see being able to apply it to your personal or professional life?
Health Economics: Please answer asap How do you construct a decision tree and what does it...
Health Economics: Please answer asap How do you construct a decision tree and what does it mean to have an expected value and standard deviation? •                     Describe the risk preferences •                     What is scenario analysis and sensitivity analysis? •                     List and explain the approaches to managing risk •                     What is the difference among these concepts from a health economics perspective: HMO, POS, PPO, indemnity, risk pool, HSA, IPA, … •                     What are the payment systems?
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
how to draw a function tree for the pressure cooker using FAST method
how to draw a function tree for the pressure cooker using FAST method
How do we make best decision on the implementation of investment ?
How do we make best decision on the implementation of investment ?
How do confidence intervals differ from prediction intervals? How are these two intervals affected by the...
How do confidence intervals differ from prediction intervals? How are these two intervals affected by the distance value?
Explain to your classmate's what a null-hypothesis. How do you make the decision to reject or...
Explain to your classmate's what a null-hypothesis. How do you make the decision to reject or not reject a Null-Hypothesis? What are some examples? All work must be in your own words.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT