Practical Machine Learning with tidymodels

How do you fit a linear model in R?
How many different ways can you think of?
lm for linear model
glmnet for regularized regression
keras for regression using TensorFlow
stan for Bayesian regression
spark for large data sets
brulee for regression using torch
All available models are listed at https://www.tidymodels.org/find/parsnip/

Run the tree_spec chunk in your .qmd.
Edit this code to use a logistic regression model.
All available models are listed at https://www.tidymodels.org/find/parsnip/
Extension/Challenge: Edit this code to use a different model. For example, try using a conditional inference tree as implemented in the partykit package by changing the engine - or try an entirely different model type!
\(log(\frac{p}{1 - p}) = \beta_0 + \beta_1\cdot \text{A}\)
Series of splits or if/then statements based on predictors
First the tree grows until some condition is met (maximum depth, no more data)
Then the tree is pruned to reduce its complexity
workflow()? fit() and predict() apply to the pre/post processing steps in addition to the actual model fittree_spec <-
decision_tree() |>
set_mode("classification")
tree_spec |>
fit(class ~ ., data = cls_train)
#> parsnip model object
#>
#> n= 800
#>
#> node), split, n, loss, yval, (yprob)
#> * denotes terminal node
#>
#> 1) root 800 266 class_1 (0.66750000 0.33250000)
#> 2) pred_2< 0.3297983 510 30 class_1 (0.94117647 0.05882353) *
#> 3) pred_2>=0.3297983 290 54 class_2 (0.18620690 0.81379310)
#> 6) pred_1>=1.236161 71 28 class_1 (0.60563380 0.39436620)
#> 12) pred_2< 1.476946 42 7 class_1 (0.83333333 0.16666667) *
#> 13) pred_2>=1.476946 29 8 class_2 (0.27586207 0.72413793)
#> 26) pred_1>=2.070158 7 2 class_1 (0.71428571 0.28571429) *
#> 27) pred_1< 2.070158 22 3 class_2 (0.13636364 0.86363636) *
#> 7) pred_1< 1.236161 219 11 class_2 (0.05022831 0.94977169) *tree_spec <-
decision_tree() |>
set_mode("classification")
workflow() |>
add_formula(class ~ .) |>
add_model(tree_spec) |>
fit(data = cls_train)
#> ══ Workflow [trained] ════════════════════════════════════════════════
#> Preprocessor: Formula
#> Model: decision_tree()
#>
#> ── Preprocessor ──────────────────────────────────────────────────────
#> class ~ .
#>
#> ── Model ─────────────────────────────────────────────────────────────
#> n= 800
#>
#> node), split, n, loss, yval, (yprob)
#> * denotes terminal node
#>
#> 1) root 800 266 class_1 (0.66750000 0.33250000)
#> 2) pred_2< 0.3297983 510 30 class_1 (0.94117647 0.05882353) *
#> 3) pred_2>=0.3297983 290 54 class_2 (0.18620690 0.81379310)
#> 6) pred_1>=1.236161 71 28 class_1 (0.60563380 0.39436620)
#> 12) pred_2< 1.476946 42 7 class_1 (0.83333333 0.16666667) *
#> 13) pred_2>=1.476946 29 8 class_2 (0.27586207 0.72413793)
#> 26) pred_1>=2.070158 7 2 class_1 (0.71428571 0.28571429) *
#> 27) pred_1< 2.070158 22 3 class_2 (0.13636364 0.86363636) *
#> 7) pred_1< 1.236161 219 11 class_2 (0.05022831 0.94977169) *tree_spec <-
decision_tree() |>
set_mode("classification")
workflow(class ~ ., tree_spec) |>
fit(data = cls_train)
#> ══ Workflow [trained] ════════════════════════════════════════════════
#> Preprocessor: Formula
#> Model: decision_tree()
#>
#> ── Preprocessor ──────────────────────────────────────────────────────
#> class ~ .
#>
#> ── Model ─────────────────────────────────────────────────────────────
#> n= 800
#>
#> node), split, n, loss, yval, (yprob)
#> * denotes terminal node
#>
#> 1) root 800 266 class_1 (0.66750000 0.33250000)
#> 2) pred_2< 0.3297983 510 30 class_1 (0.94117647 0.05882353) *
#> 3) pred_2>=0.3297983 290 54 class_2 (0.18620690 0.81379310)
#> 6) pred_1>=1.236161 71 28 class_1 (0.60563380 0.39436620)
#> 12) pred_2< 1.476946 42 7 class_1 (0.83333333 0.16666667) *
#> 13) pred_2>=1.476946 29 8 class_2 (0.27586207 0.72413793)
#> 26) pred_1>=2.070158 7 2 class_1 (0.71428571 0.28571429) *
#> 27) pred_1< 2.070158 22 3 class_2 (0.13636364 0.86363636) *
#> 7) pred_1< 1.236161 219 11 class_2 (0.05022831 0.94977169) *
Run the tree_wflow chunk in your intro-03-classwork.qmd.
Edit this code to make a workflow with your own model of choice.
Extension/Challenge: Other than formulas, what kinds of preprocessors are supported?
How do you use your new tree_fit model?

Run:
predict(tree_fit, new_data = cls_test)
What do you notice about the structure of the result?

Run:
augment(tree_fit, new_data = cls_test)
How does the output compare to the output from predict()?
new_data and the output are the sameHow do you understand your new tree_fit model?
How do you understand your new tree_fit model?
You can extract_*() several components of your fitted workflow.
⚠️ Never predict() with any extracted components!
How do you understand your new tree_fit model?
You can use your fitted workflow for model and/or prediction explanations:
Learn more at https://www.tmwr.org/explain.html

Extract the model engine object from your fitted workflow and check it out.