ALL >> Education >> View Article
Confusion Matrix In R: How To Make & Calculate [with Examples]

Confusion Matrix in R: How to Make & Calculate [With Examples]
Today, R is among the most used tools for statistical and data analysis. Because of its superior computational, visual, and graphical capabilities, this open-source environment is widely adopted. Working knowledge of the R programming language can be very useful for engineering students, corporate analytics professionals, and anybody else interested in data science.
This post will specifically aim at helping you understand how to make and calculate a confusion matrix in R. We'll also provide some examples to help you better understand how this metric works. First, let's kick off with an introduction to the confusion matrix.
What is a Confusion Matrix?
A confusion matrix is a table that is used to evaluate the performance of a machine-learning model. The table is made up of four different types of predictions:
True Positives (TP): These are the cases where the model predicted the positive class and the actual class was also positive.
True Negatives (TN): These are the cases where the model predicted ...
... the negative class and the actual class was also negative.
False Positives (FP): These are the cases where the model predicted the positive class but the actual class was negative.
False Negatives (FN): These are the cases where the model predicted the negative class but the actual class was positive.
The confusion matrix can be used to calculate several different metrics, such as accuracy, precision, recall, and specificity.
How to Make a Confusion Matrix in R
A confusion matrix is a table that is used to evaluate the performance of a machine-learning model. The matrix is composed of four quadrants: true positives, false positives, false negatives, and true negatives. Each row represents the predicted values for each class, while each column represents the actual values for each class.
The main diagonal of the matrix contains the correct predictions (true positives and true negatives), while the off-diagonal elements contain the incorrect predictions (false positives and false negatives). To calculate the accuracy of the model, we can simply take the ratio of correct predictions to total predictions.
In R, we can create a confusion matrix using thetable() function. The first argument is used to specify the actual values, while the second argument is used to specify the predicted values. For example, if we have a vector of actual values y and a vector of predicted values p, we can create a confusion matrix as follows:
table(y,p)
p
y 0 1
0 TN FP
1 FN TP
How to Calculate a Confusion Matrix in R
A confusion matrix is a table that is used to evaluate the performance of a machine-learning model. The table shows the predicted class for each data point in the test set, compared to the actual class of the data point.
To calculate a confusion matrix in R, you can use the caret package. This package contains a function called confusionMatrix(), which takes two arguments: the first is the actual class labels, and the second is the predicted class labels.
So, for our example, if we have 10 data points in our test set, and our model predicts 8 of them correctly, then we would have a “true positive” rate of 80%. Similarly, if our model predicts 2 data points as belonging to Class A when they belong to Class B, then we would have a “false positive” rate of 20%.
Here’s how you would calculate these values using the caret package:
library(caret)
confusionMatrix(test_labels, predicted_labels)
Examples of Confusion Matrices
There are many ways to create a confusion matrix, but in general, the goal is to show how well your classification model is performing. This can be done by showing the number of correct and incorrect predictions for each class.
A confusion matrix can also be used to calculate a variety of statistics, such as accuracy, precision, recall, and specificity. In this blog post, we'll focus on how to make a confusion matrix in R and how to calculate some of these statistics.
To start, let's take a look at a simple example. Say we have a classification model that predicts whether or not someone will default on their loan. Our model has two classes: 0 for non-default and 1 for default. After running our model on some data, we get the following results:
True class Predicted class
0 0
0 0
1 1 --> This is a truly positive
0 0
1 0 --> This is a false negative
1 1 --> This is a truly positive
0 0
0 1 --> This is a false positive
TN | FP
FN|TP
Our confusion matrix would then look like this:
Predicted class
True class 0 1 TOTAL
0 3 1 4 Non-Default (N) 85% 25% 60% - Recall = TP / (TP+FN) = 2/3 = 66.7% row wise sum=
Conclusion
A confusion matrix is a powerful tool for understanding the performance of your machine-learning models. In this article, we've walked through how to create and interpret a confusion matrix using R. We hope that this has been helpful in better understanding how your models are performing and where you might need to make improvements.
If you are a data science Aspirant but are looking for a support system to help you build a long-term path in this domain, Skillslash’s Data Structures and Algorithms course and learn in-depth about the topic and become a successful professional in this field. Skillslash also offers Data Science Course In Bangalore with a placement guarantee, Skillslash can help you get into it with its Full Stack Developer Course In Bangalore. Get in touch with the support team to know more.
Skillslash also offers the e DATA SCIENCE COURSE IN IN HYDERABAD
Add Comment
Education Articles
1. Az-104 For Absolute Beginners: Start Your Azure Journey TodayAuthor: Disha
2. Aima: Shaping Global Leaders With The Advanced Management Programme
Author: Aima Courses
3. 3d Game Design At Bennett University: A Path To A Creative Career
Author: Rohit Ridge
4. Why An Integrated Doctorate In Business Administration Is A Game-changer
Author: LSMT
5. Guaranteed Interviews & Pay-after-placement: A New Era Of Online Mba..
Author: University Guru
6. Notion Press Unveils 100-day Bookstore Challenge; In A First For India Will Build A Reader-first Mobile Bookstore From Scratch In 100-days
Author: Kishore
7. Best Microsoft Dynamics 365 Course In Hyderabad | D365 Training
Author: Hari
8. Friendship Day Celebration – Pyp Ia
Author: Ela Green School
9. Sgt University Admissions 2025: Step Towards Quality Education In Haryana
Author: Vidyavision
10. Top 5 Digital Marketing Institutes In Nagpur
Author: madhuri
11. Best Sap Cpi Course In Hyderabad | Sap Cpi Online Training
Author: gollakalyan
12. Guides, Mentors, Friends: Honouring The Heart Of Education: Teachers
Author: Harshad Valia International School
13. Generative Ai For Devops Training Bangalore | Ai For Devops
Author: Visualpath
14. Aima’s Post Graduate Diploma In Management: Your Path To Success
Author: Aima Courses
15. Why More Professionals Are Upskilling Through Advanced Diploma Programs ?
Author: IIBMS Institute