Introduction to Caret
Overview
Teaching: 40 min
Exercises: 0 minQuestions
What is Caret
Objectives
Master Caret package for Machine Learning
2.1 What is Caret

The caret package (short for Classification And REgression Training) is a set of functions that attempt to streamline the process for creating predictive models. The package contains tools for:
data splitting pre-processing feature selection model tuning using resampling variable importance estimation as well as other functionality.
There are many different modeling functions in R. Some have different syntax for model training and/or prediction. The package started off as a way to provide a uniform interface the functions themselves, as well as a way to standardize common tasks (such parameter tuning and variable importance).
The current release version can be found on CRAN and the project is hosted on github. Caret was developed by Max Kuhn Here only touch some of the very basic command that is useful for our Machine Learning class.
2.2 Why using Caret
- R has so many ML algorithms, challenge to keep track, different syntax for different packages
- Possibly the biggest project in R
- All in one supervised learning problem
- Uniform interface
- Standard pre & post processing
2.3 Install caret
In R console:
install.packages("caret", dependencies = c("Depends", "Suggests"))
In R studio:
Select Tools\Install Packages and select caret from CRAN
Once installed, load the caret package to make sure that it works:
library(caret)
2.4 Pre-processing using caret
There are several steps that we will use caret for. For preprocessing raw data, we gonna use caret in these tasks:
- Preprocessing with missing value
- Data partition: training and testing
Pre-processing with missing value
- Most of the time the input data has missing values (
NA, NaN, Inf) due to data collection issue (power, sensor, personel). - There are three main problems that missing data causes: missing data can introduce a substantial amount of bias, make the handling and analysis of the data more arduous, and create reductions in efficiency
- These missing values need to be treated/cleaned before we can use because “Garbage in => Garbage out”.
- There are several ways to treat the missing values:
- Method 1: remove all missing
NAvaluesdata("airquality") # Here we use this sample data because it contains missing value new_airquality1 <- na.omit(airquality) - Method 2: Set
NAto mean valueNA2mean <- function(x) replace(x, is.na(x), mean(x, na.rm = TRUE)) new_airquality2 <-replace(airquality, TRUE, lapply(airquality, NA2mean)) - Method 3: Use
Imputeto handle missing values In statistics, imputation is the process of replacing missing data with substituted values. Because missing data can create problems for analyzing data, imputation is seen as a way to avoid pitfalls involved with listwise deletion of cases that have missing values. That is to say, when one or more values are missing for a case, most statistical packages default to discarding any case that has a missing value, which may introduce bias or affect the representativeness of the results. Imputation preserves all cases by replacing missing data with an estimated value based on other available information. Once all missing values have been imputed, the data set can then be analysed using standard techniques for complete data. There have been many theories embraced by scientists to account for missing data but the majority of them introduce bias. A few of the well known attempts to deal with missing data include: hot deck and cold deck imputation; listwise and pairwise deletion; mean imputation; non-negative matrix factorization; regression imputation; last observation carried forward; stochastic imputation; and multiple imputation.
Here we use preProcess function from caret to perform bagImpute (Bootstrap Aggregation Imputation):
library(caret)
PreImputeBag <- preProcess(airquality,method="bagImpute")
DataImputeBag <- predict(PreImputeBag,airquality)
In addition to bagImpute, we also can use knnImpute (K-Nearest Neighbour Imputation)
knnImpute can also be used to impute missing value, however, it standardize the data after Imputing:
MData <- airquality[,-c(1,5,6)]
PreImputeKNN <- preProcess(MData,method="knnImpute",k=5)
DataImputeKNN <- predict(PreImputeKNN,MData)
#Convert back to original scale
RescaleDataM <- t(t(DataImputeKNN)*PreImputeKNN$std+PreImputeKNN$mean)
Note
bagImpute is more powerful and computational cost than knnImpute
Visualize the input data using corrplot
We can also quickly visualize the cross correlation between the input data to see the relationship:
dmatrix <- cor(DataImputeBag[,1:4])
corrplot(dmatrix)

Key Points
Caret