Compute In-Sample R² and Adjusted R² for an OLS Fit
Quick Overview
Implement a Python function that fits ordinary least squares with an intercept and returns the in-sample R² and adjusted R². It tests how the sums of squares are formed, how the predictor-count penalty works, and how to match the statsmodels definitions exactly.
Compute In-Sample R² and Adjusted R² for an OLS Fit
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `fit_r2_and_adjusted(X: np.ndarray, y: np.ndarray) -> tuple`. The function fits ordinary least squares (OLS) with an intercept on `X` (shape `(n, p)`) and `y` (shape `(n,)`) and returns the in-sample `(r2, adj_r2)` as two floats.
Requirements:
- Derive `n` and `p` from `X.shape`, where `p = X.shape[1]` excludes the added intercept.
- Compute both metrics in-sample, with predictions on the same `X`. R² is `1 - SS_res / SS_tot`, where `SS_tot` uses the mean of `y`.
- Use `adj_r2 = 1 - (1 - r2) * (n - 1) / (n - p - 1)`.
- Return `(r2, adj_r2)` in that order as plain floats, matching statsmodels `results.rsquared` and `results.rsquared_adj`.
You may use statsmodels (`sm.add_constant`, then `sm.OLS`) or compute the fit manually.
```hint Count the predictors carefully
Once you add the intercept, the design matrix has one more column than `X`. Check which of the two counts belongs in the adjustment formula.
```
### Constraints & Assumptions
- The grader passes `n = 30` and `p = 4`, so `n - p - 1 = 25`.
- `X` holds the predictors only. The function adds the intercept itself.
- Results are compared with statsmodels `rsquared` and `rsquared_adj` on the same data.
### What a Strong Answer Covers
- Adding an intercept column before the fit, and why the mean-centered `SS_tot` is the right baseline for a model that has one.
- Forming fitted values on the training `X` and computing `SS_res` and `SS_tot` from them.
- Applying the adjustment with `p` counted without the intercept, and returning plain floats in the order `(r2, adj_r2)`.
- The degenerate inputs: constant `y`, `n - p - 1` at or below zero, and collinear columns.
### Follow-up Questions
- Why can adjusted R² decrease when you add a predictor?
- What happens to `adj_r2` when `n - p - 1` approaches zero?
- Why is in-sample R² a poor guide to how the model will perform on new data?
Overview: Implement a Python function that fits ordinary least squares with an intercept and returns the in-sample R² and adjusted R². It tests how the sums of squares are formed, how the predictor-count penalty works, and how to match the statsmodels definitions exactly.
Compute In-Sample R² and Adjusted R² for an OLS Fit
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0
Implement fit_r2_and_adjusted(X: np.ndarray, y: np.ndarray) -> tuple. The function fits ordinary least squares (OLS) with an intercept on X (shape (n, p)) and y (shape (n,)) and returns the in-sample (r2, adj_r2) as two floats.
Requirements:
Derive
n
and
p
from
X.shape
, where
p = X.shape[1]
excludes the added intercept.
Compute both metrics in-sample, with predictions on the same
X
. R² is
1 - SS_res / SS_tot
, where
SS_tot
uses the mean of
y
.
Use
adj_r2 = 1 - (1 - r2) * (n - 1) / (n - p - 1)
.
Return
(r2, adj_r2)
in that order as plain floats, matching statsmodels
results.rsquared
and
results.rsquared_adj
.
You may use statsmodels (sm.add_constant, then sm.OLS) or compute the fit manually.
Constraints & Assumptions
The grader passes
n = 30
and
p = 4
, so
n - p - 1 = 25
.
X
holds the predictors only. The function adds the intercept itself.
Results are compared with statsmodels
rsquared
and
rsquared_adj
on the same data.
What a Strong Answer Covers Guidance
Adding an intercept column before the fit, and why the mean-centered
SS_tot
is the right baseline for a model that has one.
Forming fitted values on the training
X
and computing
SS_res
and
SS_tot
from them.
Applying the adjustment with
p
counted without the intercept, and returning plain floats in the order
(r2, adj_r2)
.
The degenerate inputs: constant
y
,
n - p - 1
at or below zero, and collinear columns.
Follow-up Questions Guidance
Why can adjusted R² decrease when you add a predictor?
What happens to
adj_r2
when
n - p - 1
approaches zero?
Why is in-sample R² a poor guide to how the model will perform on new data?