Screen OLS regression predictors by p-value at a significance level alpha
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `significant_predictors(df, target, alpha=0.05)`. Fit an ordinary least squares (OLS) regression of column `target` on all other columns of `df` with an intercept, and return the names of the predictors whose two-sided p-value is strictly less than `alpha`.
Requirements:
- Return a `list[str]` (the result is compared with `==`).
- Never include the intercept (`'const'`), even if it is significant.
- Preserve the left-to-right column order of `df`.
- Return `[]` when no predictor qualifies.
Use `statsmodels` OLS with `sm.add_constant`. The p-values are in `results.pvalues`, indexed by parameter name.
Example:
```python
# df columns: ['a', 'b', 'c', 'd', 'y'], target='y'
# a and c are strongly related to y; b and d are noise
significant_predictors(df, 'y', alpha=0.05) # -> ['a', 'c']
```
```hint Which names to loop over
`results.pvalues` has an entry for the intercept as well as one per predictor. Think about which sequence of names you iterate over: one choice gives you the required order and keeps the intercept out at the same time.
```
```hint Boundary cases
Check what a strict comparison does with a p-value equal to `alpha` or with a NaN p-value, and what `sm.add_constant` does by default when the data already contains a constant column.
```
### What a Strong Answer Covers
- Builds the design matrix from every column except the target and adds the intercept explicitly.
- Reads the p-values by parameter name and returns only real predictors, in the DataFrame's column order, as a plain list.
- Applies the strict comparison with `alpha` and returns an empty list when nothing qualifies.
- Names the degenerate inputs (an existing constant column, missing values, non-numeric columns, collinear predictors, too few rows) and says how the function behaves on each.
- Explains what a coefficient's p-value tests and why screening many predictors at one fixed `alpha` lets noise through.
### Follow-up Questions
- This function tests every predictor at the same `alpha`. With 50 candidate predictors and no real signal, how many would you expect to pass, and how would you correct for that?
- How can multicollinearity distort these p-values, and how would you detect it?
- If the residual variance grows with one of the predictors, which part of the output can you still trust, and what would you change in the fit?
Overview: Write a Python function that fits an OLS regression with statsmodels and returns the predictors whose two-sided p-values fall below a given alpha, in column order and without the intercept. It tests reading regression output, handling degenerate inputs, and knowing when per-predictor p-values mislead.