R-squared and adjusted R-squared
What is R-squared?
Range: for typical regression (can be negative for bad models without intercept)
Derivation from First Principles
WHY do we need this? The mean is our baseline predictor—if we know nothing, we'd predict the mean. A regression model tries to beat this baseline. We need a metric that compares how much variance remains (residuals) vs. the original variance.
Start with variance decomposition:
WHY this works: Total variance = Explained variance + Unexplained variance
-
Total variance : How much values spread around their mean
-
Residual variance : How much predictions miss the actual values
-
Explained variance : How much the model predictions vary from the mean
Now derive :
This ratio is :
WHY this interpretation works: If (perfect fit), . If (model no better than mean), .

For simple linear regression (): where is the Pearson correlation coefficient between and .
WHY? In simple regression, the proportion of variance explained equals the squared correlation. This doesn't hold for multiple regression!
What is Adjusted R-squared?
Alternative form:
Derivation: Why the Penalty?
THE PROBLEM: Regular always increases when you add more features, even if those features are pure noise!
WHY this happens: More parameters → model can fit training noise → decreases → increases, even if the model generalizes worse.
THE FIX: Penalize by degrees of freedom used.
Start with the variance ratio:
- Residual variance estimate: (unbiased estimator)
- Total variance estimate: (sample variance)
Build the adjusted metric:
Relate to :
WHY this works: The penalty factor increases as increases. Adding a useless feature increases but doesn't decrease enough to compensate → decreases.
Step 1: Compute mean WHY this step? The mean is our baseline—how well would we do predicting the average every time?
Step 2: Compute WHY this step? This is the total variance we're trying to explain.
Step 3: Compute WHY this step? This is the variance our model fails to explain—the leftover error.
Step 4: Compute
Interpretation: The model explains 98% of variance in house prices. Excellent fit!
Step 1: Apply the formula WHY this step? We need to penalize for the 2 features we used.
Step 2: Calculate
Interpretation: After penalizing for 2 features, we still explain 96% of variance.
Now add useless 3rd feature (random noise):
- New (slightly higher due to overfitting)
WHY it decreased: The new feature added negligible explanatory power but consumed a degree of freedom. caught this!
WHY the huge drop? With and , you only have 1 degree of freedom left! The model is fitting noise, not signal. reveals this disaster.
Why it feels right: High means the model fits the training data well.
Why it's wrong:
- measures training fit, not generalization
- Overfitted models have high but fail on new data
- doesn't detect if you're fitting noise
The fix: Always validate on held-out test data. Use cross-validation. Check for feature proliferation.
Why it feels right: increased, which sounds like improvement.
Why it's wrong: cannot decrease when adding features (mathematically impossible on training data). The increase might be pure overfitting.
The fix: Use . If it decreases or barely changes, the new features are useless. Better yet, use cross-validated metrics.
Why it feels right: In typical linear regression with intercept, holds.
Why it's wrong: If you fit a model without an intercept or use a model so bad that , can be negative!
Example: Force regression through origin when data has large intercept → terrible predictions → negative .
The fix: Always include an intercept unless you have strong theoretical reasons. If , your model is worse than predicting the mean.
Key Properties
Property 1: R-squared and Model Complexity
- is non-decreasing in (number of features)
- can decrease if added feature is weak
- For nested models (Model A ⊂ Model B), but may be
Property 2: Perfect Fit vs. No Fit
Property 3: Relationship Between Metrics
The "penalty" is , which grows with:
- More features (↑)
- Smaller sample ( ↓)
- Worse fit ( ↓)
Recall Explain to a 12-year-old
Imagine you're trying to guess how tall your classmates are. If you just guess "average height" every time, you'll be pretty wrong. Now, what if you use clues like their age and shoe size? If your guesses get way better, R-squared measures HOW MUCH BETTER.
Think of it like a test score: if R-squared is 0.80, your clues helped you get 80% of the way from "random guessing" to "perfect prediction." An R-squared of 1.0 is 100%—you're a mind reader!
But here's the trick: what if you add a silly clue like "favorite color"? It won't actually help, but you might get lucky and your score goes up a tiny bit just by chance. Adjusted R-squared is like a strict teacher who says "Nope, I'm taking points off for using useless clues!" It keeps you honest and makes sure you only use clues that truly help.
When to Use Which
| Metric | Use When |
|---|---|
| Comparing models with same number of features | |
| Comparing models with different numbers of features | |
| Neither | Final model evaluation (use test set MSE, MAE, cross-validation) |
WHY? Both and are training metrics. They don't tell you how well the model generalizes. For production decisions, always validate on unseen data.
Remember: Regular R² is like a student who counts "I studied!" even if they just stared at the book. Adjusted R² is the actual exam score.
Connections
- Linear Regression -squared evaluates linear model fit
- Mean Squared Error - relates to MSE:
- Overfitting - High with low signals overfitting
- Feature Selection - Use to decide if features help
- Cross-Validation - Better than for true performance estimation
- Bias-Variance Tradeoff - implicitly trades off bias and variance
- Model Selection - AIC/BIC provide alternative penalized fit metrics
#flashcards/ai-ml
What does R-squared measure? :: The proportion of variance in the dependent variable explained by the model, ranging from 0 (no explanation) to 1 (perfect explanation).
Write the formula for R-squared in terms of sum of squares.
What is the key difference between R-squared and adjusted R-squared?
Write the formula for adjusted R-squared.
Why can regular R-squared be misleading?
When is adjusted R-squared preferred over R-squared?
What does represent?
What does represent?
Can R-squared be negative?
What is the range of adjusted R-squared?
How does R-squared relate to correlation in simple linear regression?
What does R-squared = 0 mean?
What does R-squared = 1 mean?
Why does adjusted R-squared penalize adding features?
What is the penalty term in adjusted R-squared?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Chalो dekhte hain iska core intuition. Jab tum koi regression model banate ho, tumhe pata kaise chalega ki wo achha kaam kar raha hai ya nahi? Yahi kaam R-squared karta hai. Simple bhaasha mein, ye batata hai ki tumhare data mein jitna variation (utaar-chadhaav) hai, uska kitna percentage tumhara model explain kar paa raha hai. Socho ki agar tumhe kuch nahi pata, to tum har baar simply average (mean) value predict karoge — ye tumhara baseline hai. R-squared basically ye compare karta hai ki tumhara model us baseline se kitna behtar hai. Formula mein matlab jo error bacha (unexplained), aur matlab total variation. Agar error zero hai to (perfect), aur agar model mean jaisa hi hai to (bekaar).
Ab yahan ek badi problem aati hai jiski wajah se Adjusted R-squared aata hai. Regular R-squared ki ek gandi aadat hai — jab bhi tum koi naya feature add karte ho, chahe wo bekaar noise hi kyun na ho, R-squared हमेशा badh jaata hai ya same rehta hai, kabhi ghatta nahi. Iska matlab tum galti se ye soch sakte ho ki zyada features add karne se model behtar ho raha hai, jabki reality mein wo sirf training data ke noise ko yaad kar raha hai (overfitting). Isiliye Adjusted R-squared thoda "skeptical" version hai — ye tumhe har extra feature ke liye penalty deta hai. Formula mein jo term hai, wo degrees of freedom ka dhyaan rakhta hai, jahan features ki sankhya hai. Agar naya feature actually useful hai to Adjusted R-squared badhega, warna wo ghat jaayega.
Ye kyun important hai? Real life mein tumhare paas bahut saare features ho sakte hain aur tumhe decide karna hota hai ki kaunse rakhne hain. Agar tum sirf plain R-squared pe bharosa karoge to tum har cheez model mein daalte jaaoge aur model dikhne mein achha lagega par naye data pe fail ho jaayega. Adjusted R-squared tumhe honest feedback deta hai ki extra complexity worth hai ya nahi. Interview mein bhi ye ek common question hai, aur practically model banate waqt ye tumhari model selection ko sahi rakhta hai — isliye dono ka fark samajhna zaroori hai.