Skip to content

Fit numeric effects as smooth terms in adjust() when a factor comes first - #756

Open
jmgirard wants to merge 1 commit into
mainfrom
hotfix-adjust-smooth-factor
Open

jmgirard wants to merge 1 commit into
mainfrom
hotfix-adjust-smooth-factor

Conversation

@jmgirard

Copy link
Copy Markdown
Contributor

With additive = TRUE, adjust() wraps numeric predictors in s() with predictors[predictors == predictors_num]. When a factor comes before the numeric variables in effect, the two vectors have different lengths, so == recycles and matches the wrong positions. For example, adjust(iris[c("Sepal.Length", "Species", "Sepal.Width", "Petal.Length")], select = "Sepal.Length", additive = TRUE) errors in gamm4() because no term gets s(). With the factor between two numeric variables, one of them stays linear, so the adjusted values are wrong.

This PR uses %in% instead and adds a test that compares the result with a direct gamm4() fit.

@jmgirard
jmgirard requested a lite review from Copilot November 26, 2026 15:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

1 open issue
What changed in this PR

Fixes a bug in adjust() when additive = TRUE where numeric predictors were not reliably wrapped in s() if a factor preceded them in effect, leading to gamm4() errors or incorrect linear fits.

Changes:

  • Replace a length-mismatched == comparison with %in% to correctly identify numeric predictors to smooth.
  • Add a regression test that validates adjust() output against a direct gamm4() fit for the problematic ordering.
  • Document the bug fix in NEWS.md.
File Description
tests/​testthat/​test-adjust.R Adds a regression test ensuring numeric terms are smoothed correctly when a factor appears before them.
R/​adjust.R Fixes numeric predictor detection by using %in% instead of == to avoid recycling/mis-matches.
NEWS.md Documents the additive smoothing bug fix and its user-facing impact.

Review effort: Lite

Comment thread tests/testthat/test-adjust.R Resolved
@jmgirard

Copy link
Copy Markdown
Contributor Author

I left the bare s() in the test as it is, despite Copilot's note. gamm4() reads s() terms through mgcv itself, so the formula works without mgcv attached, and the existing gamm4() test in this file uses the same form.

This branch has not been deployed

No deployments
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.

2 participants