How to use Different Algorithms using Caret Package in R
Last Updated :
26 Jun, 2025
The caret (Classification And Regression Training) package in R provides a unified framework for training, tuning and evaluating a wide range of machine learning algorithms.
Installing and Loading the caret Package
We will install caret and load it along with any other necessary dependencies.
R
install.packages("caret")
library(caret)
Preparing the Data
We will be using the iris data set which is a built-in dataset in R Language. We will load the data and set a random seed and use createDataPartition() to split into 80% training and 20% testing to evaluate performance on unseen observations.
R
set.seed(123)
data(iris)
idx <- createDataPartition(iris$Species, p = 0.8, list = FALSE)
train_data <- iris[idx, ]
test_data <- iris[-idx, ]
Classification Algorithms
There are many classification algorithms available in Caret package. We will define cross-validation, implement the model and evaluate the model on test data.
1. Random Forest
Random Forest aggregates many decision trees to reduce overfitting and improve accuracy.
R
install.packages("randomForest")
library(randomForest)
ctrl <- trainControl(method = "cv", number = 10)
rf_model <- train(Species ~ ., data = train_data,
method = "rf",
trControl = ctrl)
rf_preds <- predict(rf_model, test_data)
confusionMatrix(rf_preds, test_data$Species)
Output:
random forest2. CART (Decision Tree)
CART builds a single tree and prunes it based on the complexity parameter (cp).
R
install.packages("rpart")
library(rpart)
cart_model <- train(Species ~ ., data = train_data,
method = "rpart",
trControl = ctrl)
cart_preds <- predict(cart_model, test_data)
confusionMatrix(cart_preds, test_data$Species)
Output:
decision tree3. k-Nearest Neighbors (k-NN)
k-NN classifies observations based on the majority vote of their k nearest neighbors.
R
knn_grid <- expand.grid(k = seq(3, 15, by = 2))
knn_model <- train(Species ~ ., data = train_data,
method = "knn",
trControl = ctrl,
tuneGrid = knn_grid)
knn_preds <- predict(knn_model, test_data)
confusionMatrix(knn_preds, test_data$Species)
Output:
knn4. Support Vector Machine (SVM)
SVM separates classes by finding the hyperplane with maximum margin.
R
install.packages("kernlab")
library(kernlab)
svm_model <- train(Species ~ ., data = train_data,
method = "svmRadial",
trControl = ctrl,
preProcess = c("center", "scale"))
svm_preds <- predict(svm_model, test_data)
confusionMatrix(svm_preds, test_data$Species)
Output:
svmRegression Algorithms
There are many classification algorithms available in Caret package. We will define cross-validation and summarise the model and evaluate the model using Root mean squared error (RMSE).
Data Split for Regression
We’ll use the mtcars dataset to demonstrate regression examples.
R
set.seed(456)
data(mtcars)
idx2 <- createDataPartition(mtcars$mpg, p = 0.8, list = FALSE)
train_reg <- mtcars[idx2, ]
test_reg <- mtcars[-idx2, ]
1. Linear Regression
Ordinary least squares provides a baseline for regression.
R
ctrl_reg <- trainControl(method = "cv", number = 5)
lm_model <- train(mpg ~ ., data = train_reg,
method = "lm",
trControl = ctrl_reg)
print(lm_model)
lm_preds <- predict(lm_model, test_reg)
RMSE(lm_preds, test_reg$mpg)
Output:
linear regression2. Random Forest Regression
Random Forest Regression is an ensemble method that builds multiple decision trees and combines their results to improve accuracy and reduce overfitting.
R
rf_reg <- train(mpg ~ ., data = train_reg,
method = "rf",
trControl = ctrl_reg)
print(rf_reg)
rf_reg_preds <- predict(rf_reg, test_reg)
RMSE(rf_reg_preds, test_reg$mpg)
Output:
Random Forest RegressorIn this article, we demonstrated how to train and evaluate different classification and regression algorithms using the caret package in R, providing a consistent framework for model building, tuning and comparison.
Similar Reads
How to Make a Tree Plot Using Caret Package in R Tree-based methods are powerful tools for both classification and regression tasks in machine learning. The caret package in R provides a consistent interface for training, tuning, and evaluating various machine learning models, including decision trees. In this article, we will walk through the ste
3 min read
Adaboost Using Caret Package in R Adaboost (Adaptive Boosting) is an ensemble learning technique that combines multiple weak classifiers to create a strong classifier. The caret package in R provides a convenient interface for training Adaboost models, along with numerous other machine-learning algorithms. This article will walk you
3 min read
How to Use RWeka Package on a Dataset? The RWeka package in R provides a convenient interface to the powerful machine-learning algorithms offered by the Weka library. Weka is a widely used suite of machine learning software that contains a collection of tools for data preprocessing, classification, regression, clustering, and visualizati
5 min read
Pre-processing and Modelling using Caret Package in R Pre-processing and modeling are important phases in the field of data science and machine learning that affect how well predictive models work. Classification and Regression Training, or the "caret" package in R, is a strong and adaptable tool intended to make training and assessing machine learning
5 min read
Tuning Machine Learning Models using Caret package in R Machine Learning is an important part of Artificial Intelligence for data analysis. It is widely used in many sectors such as healthcare, E-commerce, Finance, Recommendations, etc. It plays an important role in understanding the trends and patterns in our data to predict useful information that can
15+ min read
How to use Box Cox transformation in the caret package in R? In this article, we will see how to use a Box Cox Transformation in R Programming Language using the caret package. Box Cox Transformation in RThe Box Cox Transformation in R is the technique used to transform non-normal data to a normal distribution by applying the power transformation. This transf
7 min read