'plot.gam and draw.gam produce different smooths for same GAMM object when using factor smooths
I've been experimenting with plotting different GAMMs using the plot.gam function from the mgcv package and draw.gam function from the gratia package. When plotting a GAMM with a beta distribution (using factor smooths as 'random effects'), I noticed the smooths were different. In large, they have the same shape, but the smooths do differ, and I was wondering if I have coded something wrong, or if there are differences between the functions I have overlooked when dealing with factor smooths? Without factor smooths, these differences were not present.
I have the newest version of the package gratia installed.
My concrete question is: Is there a reason for these differences in the smooths?
Here is a reproducible example:
devtools::install_github('gavinsimpson/gratia')
library(gratia)
data <- gamSim(4, 400) %>%
mutate(rand = rep(letters[1:4], each = 100), rand = as.factor(rand))
model_3 <- bam(
y ~
s(x2, by = fac) +
s(x2, rand, by = fac, bs = "fs", m = 1),
data = data
)
draw(model_3, fun = plogis, constant = (coef(model_3)[1]),
select = c(1,2,3)) & theme_bw()
plot(model_3,
shade = TRUE,
shade.col = "lightblue",
shift = coef(model_3)[1],
trans = plogis,
seWithMean = T,
pages = 1)
Would be grateful for input! Thanks!
Solution 1:[1]
This was a bug introduced in 0.7.0-1 when I edited the code to fix a separate problem and made a silly mistake. I didn't catch it as this wasn't being tested before and I didn't notice the error when I looked at the test output when I edited the code and wrote tests.
The issue was that while the constant was being added in the add_constant() call, the code didn't return the modified object. Instead the original object was being return by add_constant().
I have fixed this locally and will push the fix to GitHub shortly. Look for version 0.7.0-5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gavin Simpson |
