Partial F-Test for Nested Models
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `def nested_model_f_test(X: np.ndarray, y: np.ndarray, drop_indices: list[int]) -> tuple[float, float, int]`. Fit a full OLS model of `y` on all of `X` and a restricted model with columns `drop_indices` removed (both with intercept), then return the partial F-test of the null that the dropped coefficients are jointly zero.
Requirements:
- Add an intercept to both models.
- Return `(f_value, p_value, df_diff)`: `float`, `float`, `int`.
- Result equals `full_res.compare_f_test(restricted_res)`.
Formula:
```python
F = ((RSS_r - RSS_f) / df_diff) / (RSS_f / (n - p - 1))
# df_diff = len(drop_indices)
# p = number of predictors in the FULL model (excluding intercept)
# p_value = survival function of F under F(df_diff, n - p - 1)
```
Example:
```python
import numpy as np
X = np.array([[1, 1], [-1, 1], [1, -1], [-1, -1], [0, 0], [0, 0]], dtype=float)
y = np.array([5, 1, 3, 2, 4, 3], dtype=float)
nested_model_f_test(X, y, drop_indices=[1])
# -> approximately (0.2143, 0.6749, 1)
```
Dropping column 1 raises the residual sum of squares from 3.5 to 3.75. With n = 6 and p = 2, F = (0.25 / 1) / (3.5 / 3) = 3/14 on 1 and 3 degrees of freedom, so the second column adds almost nothing.
```hint Which residual variance
The denominator estimates the error variance. Think about which of the two models gives an unbiased estimate whether or not the null is true.
```
```hint Using statsmodels
If you call `compare_f_test`, check which model must be the receiver and which the argument.
```
### Constraints & Assumptions
- `X` has shape (n, p) with no constant column, and `drop_indices` are column indices into `X`.
- The grader uses a fixed `X` of shape (35, 5), a fixed `y` and `drop_indices=[2, 4]`. Floats are compared with `numpy.testing.assert_allclose` and the int with `==`.
- statsmodels or direct RSS computation may be used.
### What a Strong Answer Covers
- Building both design matrices with an intercept, removing the right columns, and returning the three values with the required types.
- The partial F statistic with the right numerator and denominator degrees of freedom, and the upper-tail p-value.
- The null hypothesis being tested and why the denominator uses the full model's residual variance.
- The assumptions behind the F distribution and the cases where the test misleads.
### Follow-up Questions
- When is this partial F-test invalid or misleading?
- How does this relate to comparing models by `AIC` or a likelihood-ratio test?
- How can two predictors each look insignificant on their t-tests while the partial F-test that drops both of them is significant?
Overview: Implement a partial F-test that compares a full OLS model with a restricted model missing some columns, returning the F-statistic, p-value and degrees of freedom. It tests nested model comparison, residual degrees of freedom, the statsmodels compare_f_test call, and when the F distribution stops holding.