Application ExerciseCreate a new project from this template in RStudio Pro:
Load the packages by running the top chunk of R code
:::

Application Exerciselm() to fit a linear regression using tidymodels. Save it as lm_spec and look at the object. What does it return?Hint: you’ll need https://www.tidymodels.org
05:00
fit() functionApplication ExerciseDoes this give the same results as
03:00
parsnip model object
Call:
stats::lm(formula = mpg ~ horsepower, data = data)
Coefficients:
(Intercept) horsepower
39.9359 -0.1578
Call:
lm(formula = mpg ~ horsepower, data = Auto)
Coefficients:
(Intercept) horsepower
39.9359 -0.1578
predict() functionnew_data has an underscore# A tibble: 392 × 10
.pred mpg cylinders displacement horsepower weight acceleration year
* <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 19.4 18 8 307 130 3504 12 70
2 13.9 15 8 350 165 3693 11.5 70
3 16.3 18 8 318 150 3436 11 70
4 16.3 16 8 304 150 3433 12 70
5 17.8 17 8 302 140 3449 10.5 70
6 8.68 15 8 429 198 4341 10 70
7 5.21 14 8 454 220 4354 9 70
8 6.00 14 8 440 215 4312 8.5 70
9 4.42 14 8 455 225 4425 10 70
10 9.95 15 8 390 190 3850 8.5 70
# ℹ 382 more rows
# ℹ 2 more variables: origin <dbl>, name <fct>
What does bind_cols do?
# A tibble: 392 × 10
.pred mpg cylinders displacement horsepower weight acceleration year
* <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 19.4 18 8 307 130 3504 12 70
2 13.9 15 8 350 165 3693 11.5 70
3 16.3 18 8 318 150 3436 11 70
4 16.3 16 8 304 150 3433 12 70
5 17.8 17 8 302 140 3449 10.5 70
6 8.68 15 8 429 198 4341 10 70
7 5.21 14 8 454 220 4354 9 70
8 6.00 14 8 440 215 4312 8.5 70
9 4.42 14 8 455 225 4425 10 70
10 9.95 15 8 390 190 3850 8.5 70
# ℹ 382 more rows
# ℹ 2 more variables: origin <dbl>, name <fct>
Which column has the predicted values?
Application Exercise03:00
# A tibble: 392 × 10
.pred mpg cylinders displacement horsepower weight acceleration year
* <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 19.4 18 8 307 130 3504 12 70
2 13.9 15 8 350 165 3693 11.5 70
3 16.3 18 8 318 150 3436 11 70
4 16.3 16 8 304 150 3433 12 70
5 17.8 17 8 302 140 3449 10.5 70
6 8.68 15 8 429 198 4341 10 70
7 5.21 14 8 454 220 4354 9 70
8 6.00 14 8 440 215 4312 8.5 70
9 4.42 14 8 455 225 4425 10 70
10 9.95 15 8 390 190 3850 8.5 70
# ℹ 382 more rows
# ℹ 2 more variables: origin <dbl>, name <fct>
# A tibble: 1 × 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 rmse standard 4.89
What is this estimate? (training error? testing error?)
How many observations are in the training set?
How many observations are in the test set?
How many observations are there in total?
# A tibble: 196 × 9
mpg cylinders displacement horsepower weight acceleration year origin
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 26 4 97 75 2265 18.2 77 3
2 14 8 351 153 4129 13 72 1
3 18 6 232 100 2789 15 73 1
4 34.7 4 105 63 2215 14.9 81 1
5 19.2 8 305 145 3425 13.2 78 1
6 15 8 318 150 4135 13.5 72 1
7 12 8 383 180 4955 11.5 71 1
8 30.5 4 98 63 2051 17 77 1
9 15 8 318 150 3399 11 73 1
10 16 6 250 100 3781 17 74 1
# ℹ 186 more rows
# ℹ 1 more variable: name <fct>
Application Exercise06:00
last_fit() and specify the splittrain data from the splitrmse as before) you can just use collect_metrics() and it will automatically calculate the metrics on the test data from the splitset.seed(100)
Auto_split <- initial_split(Auto, prop = 0.5)
lm_fit <- last_fit(lm_spec,
mpg ~ horsepower,
split = Auto_split)
lm_fit |>
collect_metrics()# A tibble: 2 × 4
.metric .estimator .estimate .config
<chr> <chr> <dbl> <chr>
1 rmse standard 4.96 Preprocessor1_Model1
2 rsq standard 0.613 Preprocessor1_Model1
set.seed(100)
Auto_split <- initial_split(Auto, prop = 0.5)
lm_fit <- last_fit(lm_spec,
mpg ~ horsepower,
split = Auto_split)
lm_fit |>
collect_metrics()# A tibble: 2 × 4
.metric .estimator .estimate .config
<chr> <chr> <dbl> <chr>
1 rmse standard 4.96 Preprocessor1_Model1
2 rsq standard 0.613 Preprocessor1_Model1
set.seed(100)
Auto_split <- initial_split(Auto, prop = 0.5)
lm_fit <- last_fit(lm_spec,
mpg ~ horsepower,
split = Auto_split)
lm_fit |>
collect_metrics()# A tibble: 2 × 4
.metric .estimator .estimate .config
<chr> <chr> <dbl> <chr>
1 rmse standard 4.96 Preprocessor1_Model1
2 rsq standard 0.613 Preprocessor1_Model1
fit we will use fit_resamples# Resampling results
# 5-fold cross-validation
# A tibble: 5 × 4
splits id .metrics .notes
<list> <chr> <list> <list>
1 <split [313/79]> Fold1 <tibble [2 × 4]> <tibble [0 × 3]>
2 <split [313/79]> Fold2 <tibble [2 × 4]> <tibble [0 × 3]>
3 <split [314/78]> Fold3 <tibble [2 × 4]> <tibble [0 × 3]>
4 <split [314/78]> Fold4 <tibble [2 × 4]> <tibble [0 × 3]>
5 <split [314/78]> Fold5 <tibble [2 × 4]> <tibble [0 × 3]>
How do we get the metrics out? With collect_metrics() again!
Application Exercise05:00
\(mpg = \beta_0 + \beta_1 horsepower + \beta_2 horsepower^2+ \epsilon\)
rsq is?Application ExerciseFit 3 models on the data using 5 fold cross validation:
\(mpg = \beta_0 + \beta_1 horsepower + \epsilon\)
\(mpg = \beta_0 + \beta_1 horsepower + \beta_2 horsepower^2+ \epsilon\)
\(mpg = \beta_0 + \beta_1 horsepower + \beta_2 horsepower^2+ \beta_3 horsepower^3 +\epsilon\)
Collect the metrics from each model, saving the results as results_1, results_2, results_3
Which model is “best”?
08:00
![]()
Dr. Lucy D’Agostino McGowan adapted from Alison Hill’s Introduction to ML with the Tidyverse