Repository navigation
ENH: Add on-demand residuals to rolling regression results - #10462
Conversation
Add RollingRegressionResults.get_resid, which computes one residual per window when requested, y[t] - x[t] @ params[t] by default, or the one-step ahead error y[t] - x[t] @ params[t-1] with out_of_sample=True. The residuals use the model design matrix so they also work for models created with from_formula. Test values are verified against R. closes statsmodels#7521 xref statsmodels#6986
|
One comment and some additional testing is needed. Please add a test test when |
bashtage
left a comment
There was a problem hiding this comment.
More testing and a small docstring fix.
|
|
||
| Returns | ||
| ------- | ||
| {ndarray, Series} |
There was a problem hiding this comment.
Should be something like residuals : ndarray or Series
{ } notation is reserved for literals, e.g, {"up", "down"}.
|
The docstring has been updated to explicitly state that residuals should be : ndarray or Series. In addition, a test with missing="skip" as per your request has been implemented. This test checks for both values of out_of_sample for both pandas and numpy data input, making sure that the shape and pandas index are correct, that non-NaNs equal y - x @ params, and that NaNs occur at skipped windows/missing observations. In working on this test, I found that there is a bug where missing="skip" causes silently wrong parameters to be calculated in RollingWLS/RollingOLS due to an insufficient NaN check and outdated sums caused by skipped windows. These have been fixed separately in #10463. Therefore, this test uses get_resid vs. model fit parameters directly rather than vs. WLS. |
This PR adds get_resid(out_of_sample=False) for RollingRegressionResults, per suggestion #7521 (comment) to address the "obvious cases" brought up by bashtage in 2022. Calculated on demand rather than saved as part of fit, which was the source of the concern in #6986.
The default calculates resid[t] = y[t] - x[t] @ params[t], where parameters correspond to window ending at t (i.e., including t). With out_of_sample=True, it calculates one-step-ahead residual resid[t] = y[t] - x[t] @ params[t-1], with parameters corresponding to the previous window.
Since it relies on the model design matrix, it works with from_formula and categoricals. For RollingWLS, it returns unweighted residuals (as WLS.resid). It retains index for pandas and returns NaN for observations before min_nobs and observations with missing values. To conserve memory, it does not return per-window residual matrix.
Tests were added that confirm that it behaves correctly comparing against WLS on the window with different combinations (pandas/numpy input, with/without constant, with missing observations, with/without weights). One additional test checks the expanding window version. Verified against external R reference (base R lm() per window on the first 30 rows of macrodata, for both OLS and WLS with weights 1/population) at relative tolerance 1e-8. R code is provided in the test comments. Finally, a test was added that shows that from_formula with categorical variable produces the same result as explicitly coded design matrix. All new tests fail on main.
I would be happy to change the API name if desired (e.g., adding resid property for the default case).
Closes #7521.
Xref #6986.
NumPy's guide.
AI Disclosure
Contributions must comply with the statsmodels AI Policy.
Tick exactly one of the following. If AI tools were used, replace the
<...>placeholders in the second option with the tool name(s) and how the tool's output was
used.
Tool(s):
Claude Code (Claude Opus 5.5).Used for:
drafting the implementation and tests.I have personally read, understood, and can explain every line of this diff, and
have verified the statistical/numerical correctness of the change.