Module 16

Model Comparison

Choosing the best model for your data

Comparing Models

  • 01

    Linear vs Non-Linear

    When straight lines don't fit

  • 02

    Polynomial Regression

    Adding curves to linear models

  • 03

    Model Selection

    AIC, BIC, and cross-validation

  • 04

    Overfitting

    The danger of complexity

01

When Linear Isn't Enough

Curved Relationships

— Linear (poor fit)— True relationship

Not all relationships are linear. Growth often slows (diminishing returns), learning curves plateau, and physical processes can be exponential or logarithmic. Forcing a line on curved data gives poor predictions.

Spotting Non-Linearity

Scatter Plot
Look for curves or bends in the relationship
Residual Plot
U-shape or wave pattern = non-linearity
Residual pattern for non-linear data: Points above line at ends, below in middle (or vice versa) = curved relationship being forced into a line

A curved pattern in residuals is the clearest sign of non-linearity. The scatter plot may also show the curve directly, but residual plots magnify the pattern.

Common Non-Linear Forms

Logarithmic
y = a + b·ln(x)
Exponential
y = a·e^(bx)
Quadratic
y = a + bx + cx²
Cubic
y = a + bx + cx² + dx³

Different shapes suit different phenomena. Logarithmic: diminishing returns. Exponential: compound growth. Quadratic: parabolic (max/min). Choose based on theory and data pattern.

02

Polynomial Regression

Adding Polynomial Terms

Linear: y = b₀ + b₁x
Quadratic: y = b₀ + b₁x + b₂x²
Cubic: y = b₀ + b₁x + b₂x² + b₃x³
Still 'linear regression' because linear in the coefficients (b's)

Polynomial regression adds powers of X (x², x³, etc.). Despite the curves, it's still 'linear regression' because the model is linear in the parameters (b₀, b₁, etc.).

Fitting Polynomial Models

Linear R² = 0.45Quadratic R² = 0.94

The quadratic model captures the U-shape far better than the linear model. R² jumps from 0.45 to 0.94. But be careful—higher-degree polynomials aren't always better.

Understanding Polynomial Coefficients

Example: Performance = 10 + 5x - 0.5x²

Where x = hours of practice
Interpretation:
• Intercept (10): Baseline performance
• x coefficient (5): Initial rate of improvement
• x² coefficient (-0.5): How quickly improvement slows

Peak: Maximum at x = -b₁/(2b₂) = -5/(2×-0.5) = 5 hours

With polynomial terms, individual coefficients are harder to interpret. Focus on the overall shape: where does it peak/trough? How steep is the curve? Often the shape matters more than individual coefficients.

Polynomial Pitfalls

⚠️ Wild extrapolation
Polynomials explode outside the data range
⚠️ Overfitting
High-degree polynomials fit noise, not signal
⚠️ Multicollinearity
x, x², x³ are highly correlated
✓ Solution
Center x first, use lowest degree needed

Polynomial regression has pitfalls: wild behavior outside data range, tendency to overfit, and multicollinearity among polynomial terms. Center variables and use the lowest degree that fits well.

03

Model Selection Criteria

Fit vs Complexity

Test errorTrain errorModel Complexity →
Sweet spot: good fit without overfitting

Training error always decreases with complexity. Test error decreases then increases (overfitting). Model selection criteria try to find the sweet spot—good fit without unnecessary complexity.

Information Criteria

AIC (Akaike)
AIC = -2ln(L) + 2k
• Lighter penalty
• Prefers complex models
• Better for prediction
BIC (Bayesian)
BIC = -2ln(L) + k·ln(n)
• Heavier penalty
• Prefers simpler models
• Better for explanation
Lower = better | k = parameters, n = sample size

AIC and BIC balance fit (-2ln(L)) with a complexity penalty. BIC penalizes more heavily for large samples. Use AIC for prediction; BIC when you want to identify the 'true' model.

Testing on Held-Out Data

k-Fold Cross-Validation
■ Test fold■ Training folds

Cross-validation rotates which data is held out for testing. Average the test errors across folds. This gives an honest estimate of how well the model will predict new data.

Comparing Nested Models

Nested models: Smaller is subset of larger

Model 1: y = b₀ + b₁x
Model 2: y = b₀ + b₁x + b₂x²

Model 1 is nested in Model 2 (just set b₂ = 0)
F-test for comparing:
H₀: Extra parameters = 0 (simpler model sufficient)
Hₐ: Extra parameters ≠ 0 (complex model better)

F = [(SSE₁ - SSE₂)/(df₁ - df₂)] / [SSE₂/df₂]

For nested models, use an F-test to see if the complex model is significantly better. If p > 0.05, the simpler model is preferred—the extra terms don't help enough.

04

Overfitting

Fitting Noise, Not Signal

Good Fit
Captures trend, ignores noise
Overfit
Follows every wiggle

Overfitting means the model captures noise (random variation) rather than just the underlying pattern. It fits training data perfectly but fails on new data.

Detecting Overfitting

🚨 Training R² ≫ Test R²
Big gap between training and test performance
🚨 Too many parameters
More predictors than needed, especially relative to n
🚨 Unstable coefficients
Small changes in data → big changes in model
🚨 Implausible predictions
Extreme predictions for new observations

Key sign: big gap between training and test performance. Also watch for too many parameters relative to sample size, unstable coefficients, or wild predictions on new data.

How to Avoid Overfitting

✓ Collect more data
More data = harder to overfit
✓ Use fewer features
Simpler models generalize better
✓ Cross-validate
Test on held-out data
✓ Regularization
Ridge/LASSO penalize complexity

Prevent overfitting by: getting more data, using simpler models, validating on held-out data, and using regularization techniques (Ridge, LASSO) that penalize model complexity.

The Bias-Variance Trade-off

High Bias (Underfitting)
• Model too simple
• Misses patterns
• Consistent but wrong
High Variance (Overfitting)
• Model too complex
• Fits noise
• Changes with sample
Total Error = Bias² + Variance + Irreducible Noise

Simple models have high bias (consistently wrong) but low variance. Complex models have low bias but high variance (unstable). The best model balances both.

★

Real-World Examples

Apply your knowledge to practical scenarios

01

Advertising Diminishing Returns

A marketing analyst models Sales vs Advertising spend. Linear model: R² = 0.68, residuals show U-shaped pattern. Quadratic model (adding Ads²): R² = 0.89, residuals now random. Equation: Sales = 50 + 12(Ads) - 0.3(Ads²).

Q What does the U-shaped residual pattern in the linear model indicate? +
A The true relationship is curved, not linear. The linear model under-predicts at low and high ad spend, over-predicts in the middle—classic sign of a parabolic relationship.
Q Interpret the quadratic model: Sales = 50 + 12(Ads) - 0.3(Ads²). +
A Sales increase with advertising, but the negative Ads² coefficient means returns diminish. Eventually, more advertising has little effect (or even negative effect due to saturation/annoyance).
Q At what advertising level do sales peak? +
A Peak at Ads = -b₁/(2b₂) = -12/(2×-0.3) = -12/-0.6 = 20. Spending beyond $20K on ads actually decreases predicted sales.
Q Should the company always use the quadratic model? +
A Within the data range, yes—it fits better and makes theoretical sense (diminishing returns). But DON'T extrapolate! The parabola suggests sales go negative eventually, which is impossible.
Key Insights
  • Curved residuals signal need for polynomial terms
  • Quadratic terms capture diminishing returns or accelerating effects
  • Never extrapolate polynomial models outside the data range
02

The Overfitting Disaster

A data scientist has 50 data points and fits three models: Linear (R² = 0.45), Quadratic (R² = 0.62), and 15th-degree polynomial (R² = 0.99). They proudly present the polynomial model.

Q Why is R² = 0.99 suspicious with only 50 data points and a 15th-degree polynomial? +
A The model has 16 parameters (intercept + 15 polynomial terms) for 50 points. It's essentially memorizing the data, including noise. This is severe overfitting.
Q What would likely happen if you tested this model on new data? +
A Performance would collapse. The 15th-degree polynomial fits random wiggles in the training data that won't appear in new data. Test R² might be near 0 or even negative.
Q How should the data scientist choose between models? +
A Use cross-validation to estimate test error, or information criteria (AIC/BIC) that penalize complexity. Choose the simplest model that fits adequately—likely linear or quadratic.
Q What's the 'bias-variance trade-off' here? +
A Linear: high bias (too simple, misses patterns) but low variance (stable). 15th-degree: low bias but high variance (changes wildly with different samples). Best model balances both.
Key Insights
  • Training R² always increases with complexity—it's misleading
  • Test error (or cross-validation) reveals true predictive ability
  • The simplest adequate model usually generalizes best
03

Choosing Between Competing Models

A researcher compares four models predicting patient recovery time. Model A (2 predictors): AIC = 245, BIC = 250. Model B (5 predictors): AIC = 238, BIC = 252. Model C (8 predictors): AIC = 235, BIC = 260. Model D (3 predictors): AIC = 240, BIC = 247.

Sample Data
{
  "models": [
    {
      "name": "A",
      "predictors": 2,
      "AIC": 245,
      "BIC": 250
    },
    {
      "name": "B",
      "predictors": 5,
      "AIC": 238,
      "BIC": 252
    },
    {
      "name": "C",
      "predictors": 8,
      "AIC": 235,
      "BIC": 260
    },
    {
      "name": "D",
      "predictors": 3,
      "AIC": 240,
      "BIC": 247
    }
  ]
}
Q Which model does AIC favor? Which does BIC favor? +
A AIC favors Model C (lowest AIC = 235). BIC favors Model D (lowest BIC = 247). They disagree because BIC penalizes complexity more heavily.
Q Why do AIC and BIC give different answers? +
A AIC has a lighter complexity penalty (2k) than BIC (k×ln(n)). With moderate sample size, BIC penalizes extra parameters more, preferring simpler models.
Q If the goal is prediction, which criterion should guide choice? +
A AIC is often preferred for prediction—it's derived from minimizing prediction error. Choose Model C if prediction accuracy is paramount.
Q If the goal is finding the 'true' model for explanation, which criterion? +
A BIC is preferred for model identification—it's consistent (selects true model as n → ∞). Choose Model D for interpretability and theoretical simplicity.
Key Insights
  • AIC and BIC can recommend different models—neither is always right
  • AIC: better for prediction | BIC: better for explanation
  • When in doubt, prefer simpler models (parsimony principle)
?

Model Comparison Quiz

Test your understanding • Passing score: 70%

0 of 10 answered
Q1 multiple-choice

A residual plot shows points above the line at low X, below in the middle, and above again at high X. This suggests:

Explanation: This U-shaped or inverted-U pattern indicates non-linearity. The true relationship is curved, but we're fitting a straight line. Add a quadratic term (x²).
Q2 multiple-choice

What is overfitting?

Explanation: Overfitting means the model captures random noise in the training data, not just the true pattern. It fits training data perfectly but fails on new data.
Q3 multiple-choice

In k-fold cross-validation with k=5, what happens?

Explanation: In 5-fold CV, data is split into 5 folds. The model is trained 5 times, each time holding out 1 fold (20%) for testing. Average performance across folds estimates true prediction error.
Q4 multiple-choice

Comparing models, AIC = 150 (Model A) and AIC = 165 (Model B). Which is preferred?

Explanation: Lower AIC is better. AIC = 150 indicates Model A fits the data better after penalizing for complexity. Model A is preferred.
Q5 multiple-choice

What is the main difference between AIC and BIC?

Explanation: BIC has a heavier penalty for model complexity (k×ln(n) vs. 2k for AIC). BIC tends to select simpler models, especially with larger samples.
Q6 multiple-choice

A polynomial regression of degree 10 fits training data with R² = 0.98 but test data with R² = 0.15. This indicates:

Explanation: Huge gap between training (0.98) and test (0.15) R² is the classic sign of overfitting. The model memorized training data but can't generalize to new data.
Q7 multiple-choice

What is the bias-variance trade-off?

Explanation: Simple models may consistently miss patterns (high bias) but give stable predictions (low variance). Complex models fit patterns better (low bias) but are unstable across samples (high variance).
Q8 multiple-choice

To compare a linear model (y ~ x) with a quadratic model (y ~ x + x²), you could use:

Explanation: These are nested models (linear is a special case of quadratic with b₂=0). The F-test compares whether the extra term significantly improves fit.
Q9 multiple-choice

Which is a good practice to prevent overfitting?

Explanation: Prevent overfitting by: using cross-validation to estimate true performance, preferring simpler models (parsimony), and not adding predictors unnecessarily.
Q10 multiple-choice

Why should you never extrapolate polynomial regression models?

Explanation: Polynomials curve dramatically outside the data range, often shooting to infinity or predicting impossible values. A quadratic might predict negative sales or infinite growth—always unrealistic.

Quiz Results

0 %

← Multiple Regression Next: Logistic Regression →