Sure @jalFaizy. Let me break it down for you.
- In this part of the code, I am loading the packages to be used — caret and caretEnsemble. After that I am using the trainControl() function to specify the type of cross-validation we wish to use.
library(caret) # for training models
library(caretEnsemble) # for creating ensemble of models
mycontrol = trainControl(method="cv", number=10, savePredictions=TRUE, classProbs=TRUE)
- Then I have specified the algorithms that I will be using to train models on the iris dataset. ‘rf’ is for RandomForest and ‘knn’ for k-nearest neighbors.
model_list = c('rf', 'knn') # list of algorithms
mydata = iris
- Here I have used caretList() function from the caretEnsemble package to fit both RandomForest and KNN models on our data.
set.seed(121)
models = caretList(Species~., data=mydata, trControl=mycontrol, methodList=model_list)
- Finally after creating multiple models, we will want to compare them. In this case we have got 2 models, RandomForest and KNN. To compare the models we will use the resample() function and then use its output in the modelCor() function to find the inter-model correlation between RandomForest and KNN.
results = resamples(models)
modelCor(results)
Hope it helps.