This lesson is being piloted (Beta version)

Introduction to Caret

Overview

Teaching: 40 min
Exercises: 0 min
Questions
  • What is Caret

Objectives
  • Master Caret package for Machine Learning

2.1 What is Caret

image

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.

caret cheatsheet

2.2 Why using Caret

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:

Pre-processing with missing value

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)

image

Key Points

  • Caret