Data Partition with caret
Overview
Teaching: 20 min
Exercises: 0 minQuestions
What is Data Partition
Objectives
Learn how to split data using caret
3 Data partition: training and testing

-
In Machine Learning, it is mandatory to have training and testing set. Some time a verification set is also recommended. Here are some functions for spliting training/testing set in
caret: createDataPartition: create series of test/training partitionscreateResamplecreates one or more bootstrap samples.createFoldssplits the data into k groupscreateTimeSlicescreates cross-validation split for series data.groupKFoldsplits the data based on a grouping factor.
Due to time constraint, we only focus on createDataPartition and createFolds
3.1 Data spliting using Data Partition
Here we use createDataPartition to randomly split 60% data for training and the rest for testing:

ind1 <- createDataPartition(y=iris$Species,p=0.6,list=FALSE,times=1)
#list=FALSE, prevent returning result as a list
#times=1 to create the resample size. Default value is 1.
training <- iris[ind1,]
testing <- iris[-ind1,]
3.2 Data spliting using K-fold: Cross validation approach
The procedure has a single parameter called k that refers to the number of groups that a given data sample is to be split into. As such, the procedure is often called k-fold cross-validation. When a specific value for k is chosen, it may be used in place of k in the reference to the model, such as k=10 becoming 10-fold cross-validation.

fitControl <- trainControl(method="cv", number=10)
# train the model
model <- train(Species~., data=training,
trControl=fitControl, method="lda")
# summarize results
print(model)
predict1 <- predict(model,testing)
3.3 Other Cross-Validation approach
method: The resampling method: “boot”, “cv”, “LOOCV”, “LGOCV”, “repeatedcv”, “timeslice”, “none” and “oob”
More information on model tuning using caret can be found here
Key Points
Caret