Model Fit Metrics (R-squared, RMSE)

A regression model might predict 90% of your sales perfectly on average, yet still make wildly inaccurate forecasts for your most important customers. This scenario highlights why simply looking at predictions isn't enough to assess a model's true performance. We need precise, quantifiable metrics to understand how well a model captures the underlying patterns in data and to identify its strengths and weaknesses.

The Building Blocks: Understanding Residuals

At the heart of evaluating regression models are residuals. A residual is simply the difference between the actual observed value (yiy_i) and the value predicted by the model (y^i\hat{y}_i) for a given data point. It represents the error in the model's prediction for that specific instance. A model with a good fit will generally have small residuals, indicating that its predictions are close to the actual observed values.

Visualizing Prediction Errors
This scatter plot shows actual data points (circles) and a fitted regression line. The vertical dashed lines represent the residuals, illustrating the difference between each actual value and its corresponding predicted value on the line.
Loading chart...
Key Insight: Residuals are the vertical distances between actual data points and the regression line, representing the model's prediction errors.

Quantifying Absolute Error: Root Mean Squared Error (RMSE)

The Root Mean Squared Error (RMSE) is a widely used metric that quantifies the average magnitude of the errors. It works by first squaring each residual, which penalizes larger errors more heavily than smaller ones. These squared errors are then averaged, and finally, the square root is taken to bring the metric back into the original units of the target variable. This makes RMSE directly interpretable in the context of the data, such as dollars for sales predictions or degrees for temperature forecasts.

📐 RMSE Formula

RMSE=1ni=1n(yiy^i)2RMSE = \sqrt{\frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2}

Root Mean Squared Error (RMSE)
A measure of the average magnitude of the errors. It is the square root of the average of the squared differences between predicted and actual values.
Example: If a model predicts house prices with an RMSE of $25,000, it means, on average, the model's predictions are off by about $25,000.
Check Your Understanding
How would a single, extremely large prediction error impact RMSE compared to Mean Absolute Error (MAE)?

Explaining Variance: The Power of R-squared

While RMSE gives an absolute error, R-squared (or the coefficient of determination) provides a relative measure of model fit. It quantifies the proportion of the variance in the dependent variable that is predictable from the independent variables. Essentially, R-squared tells us how much better our model is at explaining the target variable's variation compared to a very simple model that just predicts the mean of the target variable. It ranges from 0 to 1, often expressed as a percentage.

📐 R-squared Formula

R2=1SSRSST=1i=1n(yiy^i)2i=1n(yiyˉ)2R^2 = 1 - \frac{SSR}{SST} = 1 - \frac{\sum_{i=1}^{n}(y_i - \hat{y}_i)^2}{\sum_{i=1}^{n}(y_i - \bar{y})^2}

R-squared (Coefficient of Determination)
A statistical measure that represents the proportion of the variance in the dependent variable that can be explained by the independent variables in a regression model.
Example: An R-squared of 0.85 means that 85% of the variation in the target variable can be explained by the model's inputs.
Check Your Understanding
What does an R-squared value of 0.75 for a sales prediction model indicate?

Choosing the Right Lens: RMSE vs. R-squared

RMSE vs. R-squared
FeatureRoot Mean Squared Error (RMSE)R-squared (Coefficient of Determination)
UnitsSame as target variableUnitless (proportion/percentage)
Range[0, \infty)[0, 1] (or 0-100%)
Sensitivity to ScaleScale-dependentScale-independent
InterpretationAverage prediction error magnitudeProportion of variance explained
Use CaseComparing models on the same dataset; understanding absolute errorAssessing explanatory power; comparing models across different scales
Key differences and use cases for RMSE and R-squared.

Deciding which metric to prioritize depends on your specific goals. RMSE is excellent for comparing the performance of different models on the same dataset, especially when you need to understand the absolute magnitude of prediction errors in real-world terms. A lower RMSE indicates a more accurate model in absolute terms.

R-squared, conversely, is valuable when you want to understand the explanatory power of your model, or when comparing models across datasets with different scales. A higher R-squared suggests that your model explains a larger proportion of the variability in the target variable. Often, both metrics are considered together to provide a comprehensive view of model fit.

pythonCalculating Fit Metrics in Python
Try It Yourself
Modify the code to add a new data point that is a significant outlier (e.g., X=9, y=20). Rerun the model and observe how RMSE and R-squared change. Which metric changes more dramatically?
python
Key Takeaways
  • Residuals are the fundamental building blocks of model fit metrics, representing the difference between actual and predicted values.

  • Root Mean Squared Error (RMSE) quantifies the average magnitude of prediction errors in the original units of the target variable.

  • RMSE is highly sensitive to large errors (outliers) due to the squaring of residuals.

  • R-squared (Coefficient of Determination) measures the proportion of the target variable's variance explained by the model, ranging from 0 to 1.

  • R-squared is a relative metric, indicating how much better the model performs compared to a baseline mean prediction.

  • A comprehensive understanding of model fit often requires evaluating both RMSE and R-squared, as relying on a single metric can be misleading for complex real-world scenarios.

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free