I am beginner to doing train, validation and test using the Caret package. I researched online and found it somewhat confusing (it appears most show train and test without validation), can you show a code example of how this works?
I am beginner to doing train, validation and test using the Caret package. I researched online and found it somewhat confusing (it appears most show train and test without validation), can you show a code example of how this works?
train allows you to do validation and much more. You can supply a trainControl function to the trControl argument that allows you to specify the details of your training procedure. By default train already splits 75% of the data you pass into it for training and 25% for validation, you can also change this in the trainControl.
I suggest you check out train and trainControl documentation, here and here to know more about the details you can specify in your training procedure.
Below is a simple example of training a Random Forest with 5-Fold Cross Validation and doing standardization of the data using Caret and the train function to better exemplify.
(Note: I added some unecessary things such as verboseIter = TRUE and classProbs = TRUE just to show you some features you get with caret)
library(caret)
library(datasets)
# Loading the iris dataset
data(iris)
# Specifying an 80-20 train-test split
train_idx = createDataPartition(iris$Species, p = .8, list = F)
# Creating the training and testing sets
train = iris[train_idx, ]
test = iris[-train_idx, ]
# Declaring the trainControl function
train_ctrl = trainControl(
method = "cv", #Specifying Cross validation
number = 5, # Specifying 5-fold
verboseIter = TRUE, # So that each iteration you get an update of the progress
classProbs = TRUE # So that you can obtain the probabilities for each example
)
rf_model = train(
Species ~., # Specifying the response variable and the feature variables
method = "rf", # Specifying the model to use
data = train,
trControl = train_ctrl,
preProcess = c("center", "scale") # Do standardization of the data
)
# Get the predictions of your model in the test set
predictions = predict(rf_model, newdata = test)
# See the confusion matrix of your model in the test set
confusionMatrix(predictions, test$Species)
Hope this helps you a bit