Skip to content

Fix subscript out of bounds in predict() for stepwise models with i() - #673

Open
SAY-5 wants to merge 1 commit into
lrberge:masterfrom
SAY-5:fix-predict-stepwise-i
Open

Fix subscript out of bounds in predict() for stepwise models with i()#673
SAY-5 wants to merge 1 commit into
lrberge:masterfrom
SAY-5:fix-predict-stepwise-i

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jul 18, 2026

Copy link
Copy Markdown

Fixes #672.

predict(newdata = ...) errored with "subscript out of bounds" when the model was extracted from a stepwise estimation containing i(), e.g. reg <- feols(am ~ sw(wt + i(cyl), wt + i(cyl)), mtcars); predict(reg[[1]], newdata = mtcars).

The i() level check in predict() looped over seq_along(object$model_matrix_info) while indexing mm_info_new (the info rebuilt from newdata). For a stepwise model the stored list can hold more entries than the rebuilt one, so mm_info_new[[i]] went past the end. I bound the loop to the entries present in both lists, which keeps the existing check on new i() levels intact.

Added a regression test in the PREDICT chunk. I don't have a local toolchain to build the package's C++ here, so I couldn't run the full suite, but the change is a one-line bound on the loop and the test mirrors the existing i() prediction tests.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: predict() fails with newdata when extracting a model from a stepwise estimation containing i()

1 participant