Okay, let's put the concepts of MAE, MSE, RMSE, and R-squared into practice. Seeing how these metrics are calculated with actual numbers helps solidify understanding and prepares you to evaluate your own regression models.
Imagine we've built a simple regression model to predict house prices based on their size (in square feet). We trained this model on some data, and now we want to evaluate its performance on a separate set of 5 houses it hasn't seen before (our test set).
Here are the actual prices and the prices predicted by our model (in thousands of dollars):
House ID | Size (sq ft) | Actual Price (1000s)(y_i)$ | Predicted Price (1000s)(\hat{y}_i)$ |
---|---|---|---|
1 | 1500 | 300 | 310 |
2 | 1200 | 250 | 240 |
3 | 1800 | 380 | 350 |
4 | 1400 | 290 | 300 |
5 | 1600 | 330 | 340 |
Our goal is to use the metrics we've learned to quantify how well our model's predictions match the actual prices. We have n=5 data points in our test set.
The first step is always to find the difference between the actual value (yi) and the predicted value (y^i) for each data point. This difference is called the error or residual.
Errori=yi−y^iLet's add an 'Error' column to our table:
House ID | Actual Price (yi) | Predicted Price (y^i) | Error (yi−y^i) |
---|---|---|---|
1 | 300 | 310 | -10 |
2 | 250 | 240 | 10 |
3 | 380 | 350 | 30 |
4 | 290 | 300 | -10 |
5 | 330 | 340 | -10 |
MAE gives us the average size of the errors, ignoring whether they are positive or negative.
The formula is:
MAE=n1i=1∑n∣yi−y^i∣| House ID | Error (yi−y^i) | Absolute Error ∣yi−y^i∣ | | :------- | :---------------------- | :-------------------------------- | | 1 | -10 | 10 | | 2 | 10 | 10 | | 3 | 30 | 30 | | 4 | -10 | 10 | | 5 | -10 | 10 | | Sum | | 70 |
Now, calculate the average:
MAE=570=14Interpretation: On average, our model's price prediction is off by $14,000. The units of MAE are the same as the target variable (thousands of dollars in this case).
MSE calculates the average of the squared errors. Squaring the errors makes them all positive and gives much more weight to larger errors.
The formula is:
MSE=n1i=1∑n(yi−y^i)2House ID | Error (yi−y^i) | Squared Error (yi−y^i)2 |
---|---|---|
1 | -10 | 100 |
2 | 10 | 100 |
3 | 30 | 900 |
4 | -10 | 100 |
5 | -10 | 100 |
Sum | 1300 |
Now, calculate the average:
MSE=51300=260Interpretation: The MSE is 260. Notice how the single large error of 30 for House 3 contributed 900 to the sum, significantly impacting the MSE. The units here are (thousands of dollars) squared, which isn't very intuitive. This leads us to RMSE.
RMSE is simply the square root of the MSE. This brings the metric back into the original units of the target variable, making it easier to interpret.
The formula is:
RMSE=MSE=n1i=1∑n(yi−y^i)2Interpretation: The RMSE is approximately $16,120. Like MAE, it represents a typical magnitude of error in our predictions, measured in thousands of dollars. Because it's derived from MSE, RMSE is more sensitive to large errors than MAE (notice RMSE 16.12 > MAE 14).
R2 tells us the proportion of the variance in the actual prices that our model is able to explain. It compares our model's errors to the errors of a very simple baseline model that just predicts the average price for all houses.
The formula is:
R2=1−SSTSSR=1−∑i=1n(yi−yˉ)2∑i=1n(yi−y^i)2Where:
Let's calculate SST:
Calculate the Mean Actual Price (yˉ):
yˉ=5300+250+380+290+330=51550=310The average actual price is $310,000.
Calculate Deviations from the Mean: Find (yi−yˉ) for each house.
Calculate Squared Deviations: Square these deviations.
Sum Squared Deviations: Add them up to get SST.
House ID | Actual Price (yi) | Mean Price (yˉ) | Deviation (yi−yˉ) | Squared Deviation (yi−yˉ)2 |
---|---|---|---|---|
1 | 300 | 310 | -10 | 100 |
2 | 250 | 310 | -60 | 3600 |
3 | 380 | 310 | 70 | 4900 |
4 | 290 | 310 | -20 | 400 |
5 | 330 | 310 | 20 | 400 |
Sum | 9400 (SST) |
So, SST=9400.
Now we can calculate R2:
R2=1−SSTSSR=1−94001300 R2=1−0.1383≈0.8617Interpretation: Our model's R2 is approximately 0.86, or 86%. This means that our model (using house size) explains about 86% of the variability observed in the actual house prices in our test set. This is generally considered a good fit for this simple example. An R2 of 1 would mean a perfect fit, while an R2 of 0 would mean our model is no better than just predicting the average price for every house.
Here's a summary of the calculated metrics for our house price prediction model:
Metric | Value | Interpretation |
---|---|---|
MAE | 14.00 | Average absolute prediction error is $14,000. |
MSE | 260.00 | Average squared prediction error (units are $ squared). |
RMSE | 16.12 | Typical prediction error magnitude is $16,120 (sensitive to large errors). |
R2 | 0.8617 | Model explains ~86% of the variance in house prices. |
A scatter plot is often helpful for visualizing regression performance. We plot actual values on one axis and predicted values on the other. Points falling close to the diagonal line (y=x) indicate accurate predictions.
Scatter plot showing actual house prices against predicted prices. The dashed gray line represents a perfect prediction (Predicted=Actual). Points close to this line indicate good predictions by the model.
This practice exercise demonstrates how to compute the standard regression metrics from a set of actual and predicted values. Calculating these yourself builds intuition about what each metric represents and how they respond differently to prediction errors. When evaluating your own models, you'll typically use libraries that compute these for you, but understanding the underlying calculations is fundamental.
© 2025 ApX Machine Learning