library(tidymodels)folds <-vfold_cv(mtcars, v =5)fit_resamples(linear_reg() |>set_engine("lm") |>set_mode("regression"), mpg ~ wt, folds)
Exercise 13: Compute RMSE manually
Difficulty: Beginner.
Show solution
RInteractive R
sqrt(mean((c(1, 2, 3) -c(1.1, 2.2, 2.7))^2))
Exercise 14: Compute MAE
Difficulty: Beginner.
Show solution
RInteractive R
mean(abs(c(1, 2, 3) -c(1.1, 2.2, 2.7)))
Exercise 15: Out-of-fold predictions
Difficulty: Advanced.
Show solution
RInteractive R
set.seed(1); folds <-sample(rep(1:5, length.out =nrow(mtcars)))oof <-numeric(nrow(mtcars))for (i in1:5) { tr <- mtcars[folds != i, ]; te <- mtcars[folds == i, ] oof[folds == i] <-predict(lm(mpg ~ wt, data = tr), te)}sqrt(mean((mtcars$mpg - oof)^2))
Exercise 16: CV with feature engineering
Difficulty: Advanced.
Show solution
RInteractive R
ctrl <-trainControl(method ="cv", number =5)train(mpg ~ wt +I(wt^2), data = mtcars, method ="lm", trControl = ctrl)
Exercise 17: Tuning grid with CV
Difficulty: Advanced.
Show solution
RInteractive R
ctrl <-trainControl(method ="cv", number =5)train(mpg ~ ., data = mtcars, method ="rf", tuneGrid =expand.grid(mtry =c(2, 4, 6)), trControl = ctrl)
Exercise 18: Bootstrap validation
Difficulty: Advanced.
Show solution
RInteractive R
ctrl <-trainControl(method ="boot", number =50)train(mpg ~ ., data = mtcars, method ="lm", trControl = ctrl)
Exercise 19: Reproducible CV with seeds
Difficulty: Advanced.
Show solution
RInteractive R
set.seed(1)seeds <-vector(mode ="list", length =6)for (i in1:5) seeds[[i]] <-sample.int(1e4, 1)seeds[[6]] <-1ctrl <-trainControl(method ="cv", number =5, seeds = seeds)
Exercise 20: Nested CV concept
Difficulty: Advanced. Outer CV for evaluation, inner CV for tuning.
Show solution
RInteractive R
# Conceptual outline# outer: vfold_cv(data, v = 5)# for each outer fold: tune model on inner CV, evaluate on outer test# Returns honest performance estimate when tuning is part of the model
What to do next
Machine-Learning-Exercises (shipped), CV inside ML pipelines.
tidymodels-Exercises (coming), modern workflow CV.