Earn the caret Certificate
Practice the exercises below. When you feel ready, attempt the quiz to earn a verifiable certificate you can share on LinkedIn.
caret Exercises in R: 15 Practice Problems
Fifteen practice problems on caret in R: train, trainControl, preprocessing, tuning, model comparison.
RInteractive R
library(caret)
library(dplyr)
Exercise 1: train lm
Show solution
RInteractive R
train(mpg ~ ., data = mtcars, method = "lm")
Exercise 2: 5-fold CV
Show solution
RInteractive R
train(mpg ~ ., data = mtcars, method = "lm",
trControl = trainControl(method = "cv", number = 5))
Exercise 3: Repeated CV
Show solution
RInteractive R
train(mpg ~ ., data = mtcars, method = "lm",
trControl = trainControl(method = "repeatedcv", number = 5, repeats = 3))
Exercise 4: RF with tuneGrid
Show solution
RInteractive R
train(mpg ~ ., data = mtcars, method = "rf",
tuneGrid = expand.grid(mtry = c(2, 4, 6)))
Exercise 5: createDataPartition
Show solution
RInteractive R
set.seed(1)
idx <- createDataPartition(iris$Species, p = 0.7, list = FALSE)
nrow(iris[idx, ])
Exercise 6: Preprocessing center/scale
Show solution
RInteractive R
train(Species ~ ., data = iris, method = "knn",
preProcess = c("center","scale"))
Exercise 7: NearZeroVar
Show solution
RInteractive R
nearZeroVar(mtcars)
Exercise 8: Class probabilities
Show solution
RInteractive R
train(Species ~ ., data = iris, method = "rf",
trControl = trainControl(classProbs = TRUE))
Exercise 9: ROC summary metric
Show solution
RInteractive R
binary <- iris |> dplyr::filter(Species != "setosa") |>
dplyr::mutate(Species = droplevels(Species))
train(Species ~ ., data = binary, method = "rf",
trControl = trainControl(method = "cv", number = 5,
classProbs = TRUE, summaryFunction = twoClassSummary),
metric = "ROC")
Exercise 10: confusionMatrix
Show solution
RInteractive R
fit <- train(Species ~ ., data = iris, method = "rf")
confusionMatrix(predict(fit, iris), iris$Species)
Exercise 11: Compare with resamples
Show solution
RInteractive R
ctrl <- trainControl(method = "cv", number = 5)
m1 <- train(mpg ~ ., data = mtcars, method = "lm", trControl = ctrl)
m2 <- train(mpg ~ ., data = mtcars, method = "rf", trControl = ctrl)
resamples(list(m1 = m1, m2 = m2)) |> summary()
Exercise 12: varImp
Show solution
RInteractive R
fit <- train(mpg ~ ., data = mtcars, method = "rf")
varImp(fit)
Exercise 13: predict on test set
Show solution
RInteractive R
set.seed(1)
idx <- createDataPartition(iris$Species, p = 0.7, list = FALSE)
fit <- train(Species ~ ., data = iris[idx, ], method = "rf")
predict(fit, iris[-idx, ]) |> head()
Exercise 14: Adaptive resampling
Show solution
RInteractive R
train(mpg ~ ., data = mtcars, method = "rf",
trControl = trainControl(method = "adaptive_cv",
adaptive = list(min = 3, alpha = 0.05, method = "BT", complete = TRUE),
search = "random"))
Exercise 15: Save and load
Show solution
RInteractive R
fit <- train(mpg ~ ., data = mtcars, method = "lm")
saveRDS(fit, "caret_fit.rds")
What to do next
- tidymodels-Exercises (shipped), modern alternative.
- Machine-Learning-Exercises (shipped), broader practice.
Ready to earn the caret Certificate?
The quiz is concept-based and respects your time: pass it once and your verifiable certificate is yours to share on LinkedIn, your resume, or your portfolio. Take it when you feel comfortable with the material.
Attempt the quiz→