Fix inconsistent behavior with random effect basis in tensor-product interaction #360
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 follows from the discussion in #358.
The PR resolves behavior with the
draw.gam
function when using terms liketi(x, fac, bs=c("tp", "re"))
orti(x, y, fac, bs=c("tp", "re"))
.Previously an error:
would be raised when there was only one continuous term (
x
) term, but the error was caused by invalid behavior, not by a check. When there was two continuous terms it would mysteriously work, but in fact, this turns out to be invalid behavior also.The first reason for this can be found via the stack trace:
Which also explains why it works for one continuous variable + random effect, but not not two continuous variables + random effect, since this check:
gratia/R/smooth-estimates.R
Lines 988 to 996 in 03bb56e
Makes sure that
gratia:::eval_smooth.tensor.smooth
only callsgratia:::too_far_to_na
when there are two variables in total, not three or more.This issue could then be fixed by adding a type check around:
if (is.numeric(input[[sm_vars[1L]]]) && is.numeric(input[[sm_vars[2L]]])) {
gratia/R/too_far.R
Lines 165 to 171 in 03bb56e
in
gratia:::.call_too_far
, which fixes the error in the first case to go away, but now both cases have the same undefined behaviour (the plots treat the variable as continuous). To identify why the wrong type of plot was used, we have to look at the heuristic ingratia::draw_smooth_estimates
:gratia/R/smooth-estimates.R
Lines 1198 to 1237 in 03bb56e
Since
sm_type
is always"Tensor product int.
forti
and ("Tensor product int." for
te) _[defined as such from
smooth_type.tensor.smooth`]_ ,none of the 1D cases are able to capture the term, even if it only has one continuous basis.To address this I add a separate condition to the "Factor smooth" case:
This way tensor products with a random effect basis are handled by the "Factor smooth" plotting function, which both properly handles the univariate continuous case properly, and properly raises a warning for the bivariate case.
In the last case (bivariate) it did unfortunately have the side-effect of causing an error later in
gratia:::assemble.gam
because the skipped term wasn't removed from the evaluated smooths, however this was fixed simply by slicing both the list of generated plots and the data frame of evaluated smooths by theNULL
mask (indicating plot skipped due to missing implementation):I also added unit tests that makes sure that the univariate case works, while the bivariate case properly skips the term, without throwing an error.
PS: This is my first PR to a public repo I am not personally involved in, so feel free to tell me if I did something wrong ;)