Built with Mathigon

Glossary

Select one of the keywords on the left…

Linear RegressionModel Evaluation

Reading time: ~10 min

To build a reliable linear regression model, we need a way to measure how good (or bad) its predictions are. Think of it like taking a test: you need a score to know how well you did! In machine learning, we call these scoring methods loss functions.

What do you think is the goal of our model when it comes to "loss"?

Minimize it
Maximize it
Ignore it

Mean-Squared Error (MSE)

One of the most popular loss functions is Mean-Squared Error (MSE). It simply answers the question: on average, how far off are our predictions?

MSE works by finding the difference between our predicted value and the actual value (an "error"), squaring that difference, and taking the average across all data points. Wait, why do we square it? Because squaring ensures that negative errors (guessing too low) and positive errors (guessing too high) don't . Squaring the errors also heavily penalizes really bad predictions (outliers).

MSE Formula

\begin{aligned} MSE = \frac{1}{n} \sum_{i=1}^{n}(y_i - \hat{y_i})^2 \end{aligned}

Where:

  • n: the number of data points.
  • y_i: the actual, true value.
  • \hat{y_i}: our model's predicted value.

R-Squared

While MSE tells us how big our errors are, we sometimes want to know "how much of the mystery did we solve?". To find out, we use a goodness-of-fit measure called R-squared.

R-squared tells us what percentage of the changes (variance) in our target variable (y) can be explained by our feature (x). A perfect model would have an R-squared of (or 100%), while a terrible model might have a score of .

R-Squared Formula

\begin{aligned} R^2 = 1 - \frac{\sum_{i=1}^{n}(y_i - \hat{y_i})^2 }{\sum_{i=1}^{n}(y_i - \bar{y})^2 } \end{aligned}

The formula essentially compares our model to a simple "baseline" (predicting the average value every time) to see how much improvement we've made!

To build intuition for yourself, try changing the weight and bias terms in the interactive applet below to see how the MSE and R-squared values respond to different lines. Notice how a better fitting line decreases the MSE and increases the R-squared!

Selecting An Evaluation Metric

There is no single "best" evaluation metric; it depends on your specific goal!

  • If you want to heavily punish large errors and don't care that the error is measured in "squared units", you use MSE.
  • If you'd rather look at the standard "average distance" without squaring things, you might use Mean Absolute Error (MAE).

Whatever the case, your choice of evaluation metric should reflect what you actually care about when measuring the success of your predictions.

Sina