Site icon DataFlair

Classification in R Programming: The all in one tutorial to master the concept!

FREE Online Courses: Dive into Knowledge for Free. Learn More!

In this tutorial, we will study the classification in R thoroughly. We will also cover the Decision Tree, Naïve Bayes Classification and Support Vector Machine. To understand it in the best manner, we will use images and real-time examples.

Introduction to Classification in R

We use it to predict a categorical class label, such as weather: rainy, sunny, cloudy or snowy.

Important points of Classification in R

There are various classifiers available:

An example of classification in R through Support Vector Machine is the usage of classification() function:

classification(trExemplObj,classLabels,valExemplObj=NULL,kf=5,kernel=”linear”)

Wait! Have you completed the tutorial on Clustering in R

Arguments:

1. trExemplObj – It is an exemplars train eSet object.

2. classLabels – It is being stored in eSet object as variable name e.g “type”.

3. valExemplObj – It is known as exemplars validation eSet object.

4. kf – It is termed as the k-folds value of the cross-validation parameter. Also, the default value is 5-folds. By setting “Loo” or “LOO” a Leave-One-Out Cross-Validation which we have to perform.

5. kernel – In classification analysis, we use a type of Kernel. The default kernel is “linear”.

6. classL – The labels of the train set.

7. valClassL – It is termed as the labels of the validation set if not NULL.

8. predLbls – It is defined as the predicted labels according to the classification analysis.

Decision Tree in R

It is a type of supervised learning algorithm. We use it for classification problems. It works for both types of input and output variables. In this technique, we split the population into two or more homogeneous sets. Moreover, it is based on the most significant splitter/differentiator in input variables.

The Decision Tree is a powerful non-linear classifier. A Decision Tree makes use of a tree-like structure to generate relationship among the various features and potential outcomes. It makes use of branching decisions as its core structure.

In classifying data, the Decision Tree follows the steps mentioned below:

1. Important Terminologies related to Decision Tree

2. Types of Decision Tree

Don’t forget to check out the R Decision Trees in detail

3. Categorical (classification) Trees vs Continuous (regression) Trees

Regression trees are used when the dependent variable is continuous while classification trees are used when the dependent variable is categorical.

In continuous, a value obtained is a mean response of observation.

In classification, a value obtained by a terminal node is a mode of observations.

There is one similarity in both cases. The splitting process continues results in grown trees until it reaches to stopping criteria. But, the grown tree is likely to overfit data, leading to poor accuracy on unseen data. This brings ‘pruning’. Pruning is one of the techniques which uses tackle overfitting.

4. Advantages of Decision Tree in R

You must definitely explore the R Nonlinear Regression Analysis

Disadvantages of R Decision Tree

Introduction to Naïve Bayes Classification

We use Bayes’ theorem to make the prediction. It is based on prior knowledge and current evidence.

Bayes’ theorem is expressed by the following equation:

where P(A) and P(B) are the probability of events A and B without regarding each other. P(A|B) is the probability of A conditional on B and P(B|A) is the probability of B conditional on A.

Introduction to Support Vector Machines

What is Support Vector Machine?

We use it to find the optimal hyperplane (line in 2D, a plane in 3D and hyperplane in more than 3 dimensions). Which helps in maximizes the margin between two classes. Support Vectors are observations that support hyperplane on either side.
It helps in solving a linear optimization problem. It also helps out in finding the hyperplane with the largest margin. We use the “Kernel Trick” to separate instances that are inseparable.

Terminologies related to R SVM

Why Hyperplane?

It is a line in 2D and plane in 3D. In higher dimensions (more than 3D), it’s called as a hyperplane. Moreover, SVM helps us to find a hyperplane that can separate two classes.

What is Margin?

A distance between the hyperplane and the closest data point is called a margin. But if we want to double it, then it would be equal to the margin.

How to find the optimal hyperplane?

First, we have to select two hyperplanes. They must separate the data with no points between them. Then maximize the distance between these two hyperplanes. The distance here is ‘margin’.

What is Kernel?

It is a method which helps to make SVM run, in case of non-linear separable data points. We use a kernel function to transforms the data into a higher dimensional feature space. And also with the help of it, perform the linear separation.

Different Kernels

1. linear: u’*v
2. polynomial: (gamma*u’*v + coef0)^degree
3. radial basis (RBF) : exp(-gamma*|u-v|^2)sigmoid : tanh(gamma*u’*v + coef0)

RBF is generally the most popular one.

How SVM works?

  1. Choose an optimal hyperplane which maximizes margin.
  2. Applies penalty for misclassifications (cost ‘c’ tuning parameter).
  3. If the non-linearly separable the data points. Then transform data to high dimensional space. It is done so in order to classify it easily with the help of linear decision surfaces.

Time to master the concept of Data Visualization in R

Advantages of SVM in R

Disadvantages of SVM in R

Support Vector Machine – Regression

Applications of Classification in R

Summary

We have studied about classification in R along with their usages and pros and cons. We have also learned real-time examples which help to learn classification in a better way.

Next tutorial in our R DataFlair tutorial series – e1071 Package | SVM Training and Testing Models in R

Still, if any doubts regarding the classification in R, ask in the comment section.

Exit mobile version