Q&A 6 How do you evaluate regression models using R², RMSE, and MAE?

6.1 Explanation

After training a regression model, it’s important to assess how well it fits the data. Three common evaluation metrics are:

  • R² (coefficient of determination): Measures the proportion of variance explained by the model.
  • RMSE (root mean squared error): Penalizes large errors more heavily; interpretable in the original unit.
  • MAE (mean absolute error): Measures the average magnitude of errors in predictions.

We’ll use the polynomial regression model from the previous example to calculate these metrics.

6.2 Python Code

from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
import numpy as np

# True vs predicted values
y_true = df["medv"]
y_pred = df["Predicted"]

# Evaluation metrics
r2 = r2_score(y_true, y_pred)
rmse = np.sqrt(mean_squared_error(y_true, y_pred))
mae = mean_absolute_error(y_true, y_pred)

print(f"R² Score: {r2:.4f}")
print(f"RMSE: {rmse:.4f}")
print(f"MAE: {mae:.4f}")
R² Score: 0.5484
RMSE: 6.1743
MAE: 4.2245

6.3 R Code

library(Metrics)

# True vs predicted values
y_true <- df$medv
y_pred <- df$Predicted

# Evaluation metrics
r2 <- cor(y_true, y_pred)^2
rmse_val <- rmse(y_true, y_pred)
mae_val <- mae(y_true, y_pred)

cat(sprintf("R² Score: %.4f\n", r2))
R² Score: 0.5484
cat(sprintf("RMSE: %.4f\n", rmse_val))
RMSE: 6.1743
cat(sprintf("MAE: %.4f\n", mae_val))
MAE: 4.2245

Takeaway: Use R² to understand overall model fit, RMSE for penalizing large errors, and MAE for average deviation. Together, they give a complete picture of regression performance.