
So far, we have described the data we have: wrangling and visualizing it.
For the rest of the semester, we will build models from the data to
y?These are the two families of machine learning: unsupervised and supervised learning.
Supervised: there is an outcome y

Predict a penguin’s body mass from its flipper length (regression), or predict its sex from its body measurements (classification).
Unsupervised: no outcome, only x variables

Are there groups of penguins with similar bills (clustering)? Can four body measurements be summarized by two (dimension reduction)?
You need to be able to identify which type of problem you are solving before you can choose a method.
Finding structure (unsupervised)
Making predictions (supervised)
Methods in bold are the ones we learn in this class. The rest follow the same big picture, and are a natural next step after this class.
Data with a special structure have their own families of models:
| Data | Example question | Models |
|---|---|---|
| Time series | What will Austin’s electricity demand be next week? | ARIMA, exponential smoothing (ETS), state space models, Prophet, recurrent neural networks (LSTM) |
| Spatial data | What is the air pollution between two monitors? | Kriging (Gaussian processes), spatial autoregressive (SAR/CAR) models, geographically weighted regression, point process models |
| Networks | Which airports hold the flight network together? | Exponential random graph models (ERGM), stochastic block models, latent space models, graph neural networks |
| Time to an event | How long until a customer cancels a subscription? | Kaplan–Meier curves, Cox proportional hazards model, accelerated failure time models, random survival forests |
| Grouped data | How do test scores vary within and across schools? | Fixed-effects models, random-intercept and random-slope (mixed-effects) models, generalized linear mixed models (GLMMs), generalized estimating equations (GEE) |
| Cause and effect | Does a job training program raise income? | Randomized experiments (A/B tests), propensity score matching and weighting, difference-in-differences, instrumental variables, regression discontinuity |
Unsupervised learning
No outcome
x variablesMethods:
Supervised learning
Predict an outcome y:
y)y)Methods:
Which model is the best? We need metrics to compare models
for each candidate ML algorithm
In the second half, the code follows a template: a full tidymodels workflow can be long, but most lines look the same from one problem to the next. The focus is on understanding what each algorithm or step does, and how to apply it to a specific problem: which algorithm suits the question, which settings to try, and which data each step should use.
penguins_split <- initial_split(penguins_clean, prop = 0.75)
penguins_train <- training(penguins_split)
penguins_test <- testing(penguins_split)
# cross-validation: which data should go in?
folds <- vfold_cv(penguins_train, v = 10) # (a)
folds <- vfold_cv(penguins_test, v = 10) # (b)
folds <- vfold_cv(penguins_clean, v = 10) # (c)| Algorithm | Fit the model | Look at the results |
|---|---|---|
| PCA | stats::prcomp() |
library("factoextra"), fviz_eig(), fviz_pca_var(), fviz_pca_biplot() |
| K-means clustering | stats::kmeans() |
library("factoextra"), fviz_nbclust(), fviz_cluster() |
| Linear regression | stats::lm() |
summary(), predict(), resid() |
| Logistic regression | stats::glm(family = "binomial") |
predict(type = "response"), plotROC::geom_roc() |
Each of these fits one model with one function call, which is all we need for unsupervised methods and for linear and logistic regression.
More flexible algorithms (k-nearest neighbors, decision trees, random forests) have hyperparameters, so we need to find the best version of the model: train-test split, cross-validation, and hyperparameter tuning.
tidymodelstidymodels is a collection of packages, one for each step of the big picture. It also covers the algorithms above: linear_reg() and logistic_reg() fit the same models as lm() and glm().
| Package | Step in the big picture | Functions |
|---|---|---|
rsample |
Train-test split, cross-validation folds | initial_split(), training(), testing(), vfold_cv() |
recipes |
Preprocessing | recipe(), step_normalize(), step_naomit(), prep(), bake() |
parsnip |
Choosing the algorithm | linear_reg(), logistic_reg(), nearest_neighbor(), decision_tree(), rand_forest(), set_engine(), set_mode() |
workflows |
Combining preprocessing and algorithm | workflow(), add_recipe(), add_model() |
tune |
Cross-validation and tuning | fit_resamples(), tune(), tune_grid(), collect_metrics(), select_best(), finalize_workflow(), last_fit() |
dials |
Grids of tuning values | grid_regular(), neighbors() |
yardstick |
Metrics | metrics(), rmse(), accuracy(), conf_mat(), roc_curve(), roc_auc() |
broom |
Turning model output into tibbles | tidy(), glance(), augment() |
Unsupervised learning
| Oct 19 | 9a | Clustering: k-means and hierarchical clustering |
| Oct 21 | 9b | Principal component analysis |
Supervised learning: the classic models
| Oct 23 | 9c | Linear regression |
| Oct 26 | 10a | Logistic regression |
| Oct 28 | 10b | ML in practice I: from a question to an ML problem |
Supervised learning: flexible models and tuning
| Oct 30 | 10c | Introduction to tidymodels |
| Nov 2 | 11a | K-nearest neighbors and cross validation |
| Nov 4 | 11b | ML in practice II: building a KNN model, start to finish |
| Nov 6 | 11c | Regression and classification trees |
| Nov 9 | 12a | Random forests |
| Nov 11 | 12b | ML in practice III: building a random forest, start to finish |
| Nov 16 | 13a | Case study III: classifying U.S. flights |
| Nov 18 | 13b | Case study III: classifying U.S. flights (cont.) |