Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR was assisted by Claude.
Summary
predict.subfit()builds the model matrix for new data withmodel.matrix(delete.response(terms(object$formula)), data = newdata, xlev = object$xlvls). These terms do not have thepredvarsattribute of the training model frame, somodel.matrix()computes terms with a data-dependent basis, such aspoly()orsplines::ns(), again from the new data. The coefficients of the submodel then apply to other basis columns.Consequences:
proj_linpred()andproj_predict()withnewdata,varsel()withd_test, and K-fold CV, where the submodels predict the left-out fold. In the example below, the predictions for 10 training rows given asnewdatadiffer from the training-data predictions of the same rows by up to 5.8 on the linear-predictor scale (the outcome has an SD of about 1.4).cv_varsel(validate_search = TRUE)with LOO CV fails: for one left-out observation,poly(x3, 2)stops with'degree' must be less than number of unique points.Reproduction on master (custom reference model, no Stan fit):
Changes
fit_glm_ridge_callback()stores the terms of its training model frame (withpredvars) as the new elementtermsof thesubfitobject. The L1 search stores the terms of its model frame in the same element.subfit_terms_newdata()builds the terms ofobject$formulawithout the response and copies thepredvarsentries of its variables fromobject$terms.predict.subfit()uses these terms for new data. Ifobject$termsis missing or does not contain all variables (for example, asubfitobject from an older projpred version), the function returns the plain terms, as before.outdmin_tester_trad()expects the new element.NEWS.mdentry.The other submodel fitters (
lme4,mgcv,gamm4,MASS,nnet,ordinal,mclogit) use their ownpredict()methods, which keeppredvars; I did not check them. The reference model is not affected, because its predictions come from its ownpredict()method.Results
For the example above, the predictions for new data now equal the training predictions of the same rows (difference about 1e-16). The predictions for the training data, the projected draws and all results for models without such terms are identical to master.
Tests
The new
test_subfit_predvars.Ruses a Gaussian reference model with apoly()orsplines::ns()term. It checks that the predictions for new data equal the predictions for the same training observations (forward projection withpoly()and withns(), and L1-search submodels withpoly()), and thatcv_varsel(validate_search = TRUE)runs. All four tests fail on master (three with wrong predictions, one with the error above). The full suite passes withNOT_CRAN=true: 257 tests, 55 810 expectations, 0 failures, the same 4 warnings as master. Tested with this change applied to master 73cf0ee (projpred 2.11.0.9000), R 4.6.1, brms 2.23.0, rstanarm 2.32.2, cmdstanr 0.9.0.9001, CmdStan 2.39.0, on CSC Roihu (Linux, AMD EPYC).