suppressPackageStartupMessages(library(recipes))
suppressPackageStartupMessages(library(rsample))
suppressPackageStartupMessages(library(parsnip))
suppressPackageStartupMessages(library(tune))
suppressPackageStartupMessages(library(broom))
set.seed(6735)
tr_te_split <- initial_split(mtcars)
spline_rec <- recipe(mpg ~ ., data = mtcars) %>% step_ns(disp)
lin_mod <- linear_reg()
spline_res <- last_fit(lin_mod, spline_rec, split = tr_te_split)
# get the fitted workflow
spline_res %>%
extract_workflow()
#> ══ Workflow [trained] ══════════════════════════════════════════════════════════
#> Preprocessor: Recipe
#> Model: linear_reg()
#>
#> ── Preprocessor ────────────────────────────────────────────────────────────────
#> 1 Recipe Step
#>
#> • step_ns()
#>
#> ── Model ───────────────────────────────────────────────────────────────────────
#>
#> Call:
#> stats::lm(formula = ..y ~ ., data = data)
#>
#> Coefficients:
#> (Intercept) cyl hp drat wt qsec
#> 23.087028 0.326218 0.005969 -0.009576 -0.902839 0.185826
#> vs am gear carb disp_ns_1 disp_ns_2
#> 1.492756 4.101555 0.174875 -1.278962 -15.149506 -4.905087
# We don't have a tidymodels api like `collect_metrics()` for getting training
# set statistics. We generally think that it is a bad idea. You can still do it
# in a few lines though
spline_res %>%
extract_workflow() %>%
augment(training(tr_te_split)) %>%
rmse(mpg, .pred) # or use with a metric set
#> # A tibble: 1 × 3
#> .metric .estimator .estimate
#> <chr> <chr> <dbl>
#> 1 rmse standard 1.69