Closed DHLocke closed 1 year ago
mgcv is very picky about data types; PredictMat()
requires that the data is provided exactly as you had it in the data used to fit the model. Even though g
is present in ds3
it is not a factor and that in and of itself is sufficient for it to throw the error you observed:
r$> sm <- smooth_estimates(m_biv, data = ds3)
Error in PredictMat(smooth, data) : Can't find by variable
r$> ds4 <- ds3 |> mutate(g = factor(g))
r$> sm <- smooth_estimates(m_biv, data = ds4)
r$>
I should probably do more to check user supplied data.
A couple of observations:
just in case this isn't a typo; your model is incorrect. For a factor by model you need to include the by
variable as a factor parametric effect also:
m_biv <- gam(y ~ g + # <----- very important! s(x, by = g) + s(z, by = g) + s(f), data = df, method = "REML")
this issue has gone unnoticed because I have functions to help prepare data slices, like evenly()
(which does work for factors), ref_level()
and level()
. So I would have created ds3
using
ds3 <- data_slice(m_biv, x = evenly(x, lower = 0, upper = 0.5, by = 0.05), # two continuous covariates, spaced out z = evenly(z, lower = 0, upper = 0.5, by = 0.05), g = evenly(g) # and the binary factor used in the s(var, by= 'fac'). BOTH levels specified )
which them works without error in smooth_estimates()
.
I've been thinking that evenly()
might not be the most intuitive name for something that applies to factors, so I should think about adding something specific for factors to return all the levels or a subset of levels of a factor from a GAM to complement ref_level()
and level()
which return the reference level or a specific level of a factor with all the levels of the original factor set as the level attribue.