set.seed(47) # can be any number....
sample(10,3) # sample three numbers between 1 and 10 inclusive[1] 9 2 7
Recall our definition of statistical learning:
Statistical learning refers to a set of tools for making sense of complex datasets. — Preface of Introduction to Statistical Learning with applications in R (ISLR)
So far, we have discussed clustering, which is a form of unsupervised learning. Now, we turn to supervised learning

You are probably already familiar with statistical learning — even if you did not know what the phrase meant before
Examples of statistical learning algorithms include:
Generalized linear models (GLMs) and penalized versions (lasso, ridge, elastic net)
Smoothing splines, generalized additive models (GAMs)
Decision trees and its variants (e.g., random forests, boosting)
Two main types of problems:
Regression models: estimate average value of response given a set of covariates (i.e. the response is quantitative)
Classification models: determine the most likely class of a set of discrete response variable classes given a set of covariates (i.e. the response is categorical)
Depends on your goal — the big picture: inference vs prediction
Let \(Y\) be the response variable, and \(X\) be the predictors, then the learned model will take the form:
\[ \hat{Y}=\hat{f}(X) \]
Care about the details of \(\hat{f}(X)\)? \(\Rightarrow\) You want to peform statistical inference
Fine with treating \(\hat{f}(X)\) as a obscure/mystical machine? \(\Rightarrow\) Your interest is prediction
Note: while any algorithm can be used for prediction, options are limited for inference
Active area of research on using more mystical models for statistical inference
There are several trade-offs between different algorithms
Model flexibility vs interpretability
Good fit vs overfit or underfit
Parsimony versus black-box
Generally speaking: trade-off between a model’s flexibility (i.e. how “wiggly” it is) and how interpretable it is
The simpler parametric form of the model, the easier it is to interpret
Hence why linear regression (least squares) is popular in practice
Parametric models, for which we can write down a mathematical expression for \(f(X)\) before observing the data (e.g. linear regression), are inherently less flexible
Nonparametric models, in which \(f(X)\) is estimated from the data (e.g. kernel regression)
If your goal is prediction, your model can be as arbitrarily flexible as it needs to be
We’ll discuss how to estimate the optimal amount of flexibility shortly…
Comic from xkcd
Model assessment:
Model selection:
Data are generated from a smoothly varying non-linear model (shown in black), with random noise added:
\[ Y = f(X) + \epsilon \]
Left panel: intuitive notion of the meaning of model flexibility
Right panel: how the mean squared error (model assessment metric) changes with flexibility (will come back to this panel later)
Orange line: an inflexible, fully parametrized model (simple linear regression)
Green line: an overly flexible, nonparametric model
We want to learn a statistical model that provides a good estimate of \(f(X)\) without overfitting
Split the data into two groups:
Training data: data used to train models
Test data: “held-out” data used to test the learned models
Repeat data splitting \(k\) times:
Each observation is placed in the “held-out” / test data exactly once
This is called k-fold cross validation (typically set \(k\) to 5 or 10) . . .
By assessing models using “held-out” data, we act to ensure that we get a generalizable(!) estimate of \(f(X)\)
Toy illustration of 5-fold CV
\(k\)-fold cross validation is often the preferred approach, but the trade-off is that CV analyses take \({\sim}k\) times longer than analyses that utilize train-test splitting (note, too, that \(k\) must be selected)
An important aspect of statistical analysis is that it is reproducible. You should…
Record your analysis in a notebook (e.g., R Markdown). This notebook should be complete enough such that if you gave it and datasets to someone, they should be able to:
Recreate your entire analysis
Achieve the exact same (numerical) results
To ensure they achieve the exact same (numerical) results, we can set a seed before each instance of random sampling (e.g., when assigning data to training or test sets or \(k\)-folds)
[1] 9 2 7
Loss function (aka objective or cost function) is a metric that represents the model’s quality of fit at a given observation
\[ \displaystyle \text{Quadratic loss} = (Y_i - \hat{f}(X_i))^2 \]
We estimate the risk (i.e. the expected loss) of a model by taking the average of the loss function over all observations — this represents the quality of fit of a model
\[\text{MSE} = \frac{1}{n} \sum_i^n (Y_i - \hat{f}(X_i))^2\]
For regression models, we typically use the MSE to evaluate model fit
For classification, model assessment is not quite so clear-cut1
Misclassification rate: percentage of predictions that are wrong
However, interpretation can be affected by class imbalance:
If two classes are equally represented in a dataset, a misclassification rate of 2% is good
But, if one class comprises 99% of the data, that 2% is no longer such a good result…
Model selection: picking the best model from a suite of possible models (e.g., using MSE for a regression models or misclassification rate for a classification models)
Two things to keep in mind:
Every model should be learned using the same training and test data
Do not resample the data between the time the time you, e.g., perform linear regression vs the time you perform random forest
For regression, a third point should be kept in mind: a metric like MSE is unit-dependent
Look at the plots. For any given value of \(x\):
Look at the plots. For any given value of \(x\):
Here, we define the “best” model as the one that minimizes the test-set MSE.
The true MSE can be decomposed into \({\rm MSE} = {\rm (Bias)}^2 + {\rm Variance}\)

Towards the left: high bias, low variance. Towards the right: low bias, high variance.
The optimal amount of flexibility is somewhere in the middle
An English translation: “Entities must not be multiplied beyond necessity”
You do this all the time — weighing simplicity with outcome (e.g. when giving directions)
In terms of model selection: When faced with several methods that give roughly equivalent performance, pick the simplest one

The more features, the merrier?… Not quite
Adding additional signal features that are truly associated with the response will improve the ftted model
Adding noise features that are not truly associated with the response will lead to a deterioration in the model
Noise features increase the dimensionality of the problem, increasing the risk of overfitting