Fit an interaction regression in R and compute Durbin-Watson and residual autocorrelation
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write four R functions that fit an ordinary least squares model with an interaction term, expose its coefficients, and compute two diagnostics for autocorrelation in the residuals.
- `fit_interaction_model(df, y, x1, x2)`: `y`, `x1` and `x2` are column names given as character strings. Fit `lm()` with the formula `y ~ x1 * x2` and return the `lm` object. Call `stop()` if any name is not a column of `df`.
- `model_coefs(model)`: return `coef(model)`, which holds the intercept, both main effects and the `x1:x2` interaction.
- `durbin_watson(model)`: return the scalar `sum((e_t - e_{t-1})^2) / sum(e_t^2)` computed over `residuals(model)` in row order.
- `residual_autocorrelation(model, lag = 1)`: return `sum(e[(lag+1):n] * e[1:(n-lag)]) / sum(e^2)`, with no de-meaning of the residuals.
Example:
```r
m <- fit_interaction_model(df, "sales", "price", "ads")
model_coefs(m) # c(`(Intercept)`=..., price=..., ads=..., `price:ads`=...)
durbin_watson(m) # e.g. 1.87
residual_autocorrelation(m, lag = 1) # e.g. 0.06
```
```hint Building the formula
The column names arrive as strings, so the formula has to be built from text. Check what the `*` operator expands to inside an R formula.
```
```hint Shifting the residuals
Both diagnostics compare each residual with an earlier one. Base R has a function that returns the successive differences of a vector, and index ranges can line a vector up with a shifted copy of itself. Watch what those ranges do when `lag` is too large.
```
### Constraints & Assumptions
- Only base R and the packages already installed are available, with no network access.
- Residuals are used in the row order of `df`, so the rows are assumed to be in time order.
### What a Strong Answer Covers
- Building the formula from string column names and validating the names before fitting
- What `x1 * x2` expands to and which coefficient names `model_coefs()` returns
- Both diagnostics computed from the residuals in row order, matching the given formulas exactly
- Edge cases such as an out-of-range `lag`, rows dropped for missing values, and a perfect fit
- How the Durbin-Watson statistic relates to the lag-1 autocorrelation and which values signal a problem
### Follow-up Questions
- How do you interpret a Durbin-Watson value near 2 versus near 0 or 4?
- If the residuals show autocorrelation, how would you adjust the model?
- Why does Durbin-Watson become unreliable when the model includes a lagged value of `y` as a predictor, and what would you use instead?
- With `price:ads` in the model, what does the coefficient on `price` mean?
Overview: Write four R functions that fit an OLS model with an interaction term from string column names, return its coefficients, and compute the Durbin-Watson statistic and a lag-k residual autocorrelation. It tests formula building in R, input validation, and how to read serial correlation in regression residuals.