# NOT RUN {
library(recipes)
library(rsample)
library(parsnip)
# ------------------------------------------------------------------------------
set.seed(6735)
folds <- vfold_cv(mtcars, v = 5)
# ------------------------------------------------------------------------------
# tuning recipe parameters:
spline_rec <-
recipe(mpg ~ ., data = mtcars) %>%
step_ns(disp, deg_free = tune("disp")) %>%
step_ns(wt, deg_free = tune("wt"))
lin_mod <-
linear_reg() %>%
set_engine("lm")
# manually create a grid
spline_grid <- expand.grid(disp = 2:5, wt = 2:5)
# Warnings will occur from making spline terms on the holdout data that are
# extrapolations.
spline_res <-
tune_grid(spline_rec, model = lin_mod, resamples = folds, grid = spline_grid)
spline_res
show_best(spline_res, metric = "rmse", maximize = FALSE)
# ------------------------------------------------------------------------------
# tune model parameters only (example requires the `kernlab` package)
car_rec <-
recipe(mpg ~ ., data = mtcars) %>%
step_normalize(all_predictors())
svm_mod <-
svm_rbf(cost = tune(), rbf_sigma = tune()) %>%
set_engine("kernlab") %>%
set_mode("regression")
# Use a space-filling design with 7 points
set.seed(3254)
svm_res <- tune_grid(car_rec, model = svm_mod, resamples = folds, grid = 7)
svm_res
show_best(svm_res, metric = "rmse", maximize = FALSE)
autoplot(svm_res, metric = "rmse") +
scale_x_log10()
# }
Run the code above in your browser using DataLab