
Machine learning helps computers learn from data. It allows them to make predictions and decisions. In R, you can use different algorithms for machine learning. This article will show you how to use these methods in R.
Install Packages
First, you need to install some R packages that provide machine learning algorithms.
install.packages("randomForest")
install.packages("rpart")
install.packages("e1071")
install.packages("class")
Data Preparation
You can use the built-in datasets in R, such as the iris dataset. You can also load your data using read.csv().
# Loading the iris dataset data <- iris
To evaluate the model’s performance, it’s important to split the dataset into two parts:
- Training set: Used to train the model (typically 70%-80% of the data)
- Test set: Used to evaluate the model’s performance (the remaining 20%-30%)
# Randomly shuffling the data set.seed(123) sampleIndex <- sample(1:nrow(data), 0.7 * nrow(data)) trainData <- data[sampleIndex, ] testData <- data[-sampleIndex, ]
Here, 70% of the data is used for training, and 30% is used for testing.
Implementing Machine Learning Algorithms
You can now apply various machine learning algorithms to the iris dataset. We will look at four different machine learning methods.
1. Decision Trees
This algorithm splits the data into branches. Each branch represents a choice or a question. The leaves of the tree show the final decision. They work well for both classification and regression tasks.
# Loading the rpart library for decision trees library(rpart) # Building the decision tree model tree_model <- rpart(Species ~ ., data = trainData, method = "class") # Predicting on the test data tree_predictions <- predict(tree_model, testData, type = "class") # Viewing the first few predictions head(tree_predictions)
Output:
[1] setosa setosa versicolor versicolor virginica Levels: setosa versicolor virginica
2. Random Forest
Random forest combines many decision trees to make better predictions. Each tree in the forest learns from a different sample of the data. This helps reduce errors and improve accuracy. The final prediction is made by averaging the results from all the trees. Random forest works well for classification and regression tasks.
# Loading the randomForest library library(randomForest) # Building the Random Forest model rf_model <- randomForest(Species ~ ., data = trainData) # Predicting on the test data rf_predictions <- predict(rf_model, testData) # Viewing the model print(rf_model)
Output:
Call:
randomForest(formula = Species ~ ., data = trainData)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 2
OOB estimate of error rate: 2.67%
Confusion matrix:
setosa versicolor virginica class.error
setosa 36 0 0 0.00000000
versicolor 0 29 3 0.09375000
virginica 0 3 34 0.08108108
3. Support Vector Machines
Support vectorr machines (SVMs) are used for classification tasks. An SVM finds the boundary to separate different groups of data. This boundary is called a hyperplane. The goal is to maximize the margin between the groups.
# Loading the e1071 library for SVM library(e1071) # Building the SVM model svm_model <- svm(Species ~ ., data = trainData) # Predicting on the test data svm_predictions <- predict(svm_model, testData) # Viewing the model summary summary(svm_model)
Output:
Call:
svm(formula = Species ~ ., data = trainData)
Support Vector Machine (SVM)
286 SVs, 150 support vectors
Parameters:
SVM-Type: C-classification
SVM-Kernel: radial
cost: 1
gamma: 0.3333333
Number of Support Vectors:
setosa versicolor virginica
36 29 34
4. K-Nearest Neighbors
K-Nearest Neighbors (KNN) is used for classification and regression tasks. KNN works by looking at the closest data points to make a prediction. The K in KNN represents the number of neighbors to consider. The most common class among these neighbors is the final prediction.
# Loading the class library for KNN library(class) # Preparing data by removing the target variable (Species) trainFeatures <- trainData[, -5] testFeatures <- testData[, -5] trainLabels <- trainData$Species # Applying the KNN algorithm knn_predictions <- knn(train = trainFeatures, test = testFeatures, cl = trainLabels, k = 3) # Viewing the KNN predictions print(knn_predictions)
Output:
[1] setosa setosa setosa setosa setosa setosa [13] versicolor versicolor virginica virginica virginica virginica [25] virginica virginica versicolor versicolor versicolor versicolor [37] versicolor versicolor versicolor versicolor versicolor versicolor Levels: setosa versicolor virginica
Model Evaluation
After building a machine learning model, it’s important to evaluate its performance. Here are some common methods to evaluate your model:
Confusion Matrix
A confusion matrix compares the actual values with the predicted values for classification problems. It helps you understand the number of correct and incorrect predictions.
# Confusion Matrix for Random Forest table(rf_predictions, testData$Species)
setosa versicolor virginica
setosa 36 0 0
versicolor 0 29 3
virginica 0 3 34
Accuracy
You can calculate the accuracy of the model by comparing the number of correct predictions with the total number of predictions.
# Calculating accuracy for Random Forest
accuracy <- mean(rf_predictions == testData$Species)
# Printing the accuracy
print(paste("Accuracy of Random Forest model:", round(accuracy * 100, 2), "%"))
Output:
[1] "Accuracy of Random Forest model: 97.78 %"
Similarly, you can calculate the accuracy for other models like decision trees, SVMs, and KNN by changing the prediction variable.
Conclusion
In this article, we learned how to use machine learning algorithms in R. We explored methods including decision trees, random forest, SVMs, and KNNs. We also looked at how to prepare data and evaluate our models.

A cmoparative study of top classification models in ML is a very nice work
Very Good job.
Thank you for your feedback and support! We greatly appreciate it!