How to use Different Algorithms using Caret Package in R
The caret package in R is a powerful tool for performing machine learning tasks, including training and evaluating models, feature selection, and hyperparameter tuning. It provides a unified interface to the various algorithms, making it easy to switch between different models and compare their performance.
Caret package in R
The caret (Classification And REgression Training) package in R Programming Language is the comprehensive toolkit for training and evaluating machine learning models. It provides a unified interface for working with various algorithms, handling data preprocessing tasks, feature selection, model tuning, and performance evaluation.
Classification Algorithm using CARET Package
Now let’s train a random forest model on the iris dataset using the CARET Package.
R
library (caret) data (iris) # the model model <- train (Species ~ ., data = iris, method = "rf" ) # Evaluate the model results <- trainControl (method = "cv" , number = 15) performance <- train (Species ~ ., data = iris, method = "rf" , trControl = results) # Print print (performance) |
Output:
Random Forest 150 samples 4 predictor 3 classes: 'setosa', 'versicolor', 'virginica' No pre-processing Resampling: Cross-Validated (15 fold) Summary of sample sizes: 140, 140, 141, 140, 139, 140, ... Resampling results across tuning parameters: mtry Accuracy Kappa 2 0.9544108 0.9317932 3 0.9470034 0.9206821 4 0.9536700 0.9303397 Accuracy was used to select the optimal model using the largest value. The final value used for the model was mtry = 2.
The below example is more or less the same as the above but the main difference lies in the method which you have used as a method. In the previous example, it was a random forest but in this it is rpart.
R
library (caret) data (iris) # the model model <- train (Species ~ ., data = iris, method = "rpart" ) # Evaluate the model results <- trainControl (method = "cv" , number = 12) performance <- train (Species ~ ., data = iris, method = "rpart" , trControl = results) # Print print (performance) |
Output:
CART 150 samples 4 predictor 3 classes: 'setosa', 'versicolor', 'virginica' No pre-processing Resampling: Cross-Validated (12 fold) Summary of sample sizes: 138, 137, 137, 138, 138, 138, ... Resampling results across tuning parameters: cp Accuracy Kappa 0.00 0.9203297 0.8804324 0.44 0.7456502 0.6236446 0.50 0.3977411 0.1111111 Accuracy was used to select the optimal model using the largest value. The final value used for the model was cp = 0.
Regression Algorithm using Caret Package
Caret stands for the Classification and Regression Task also we have seen two examples of classification tasks above. Now let’s see how can we train a regression model using a linear method that is “lm” in the method parameter of the function.
R
library (caret) data <- mtcars train_control <- trainControl (method= "cv" , number=4) model_lm <- train (mpg~., data=data, method= "lm" , trControl=train_control) print (model_lm) |
Output:
Linear Regression 32 samples 10 predictors No pre-processing Resampling: Cross-Validated (4 fold) Summary of sample sizes: 24, 24, 24, 24 Resampling results: RMSE Rsquared MAE 4.04832 0.631639 3.300931 Tuning parameter 'intercept' was held constant at a value of TRUE
Contact Us